Deploying Machine Learning Model on Docker

In this blog, we are going to deploy a Machine Learning Model on the top of Docker container. For this, we need various software as a prerequisite. So let's start this by installing Docker.

To install Docker first we need to configure yum for docker. So paste the below code in your repo dir cd /etc/yum.repos.d

yum install docker-ce --nobest
systemctl enable docker --now

These commands will install and start the container services.

After that, we need to launch a docker container on which we will run the Machine Learning model.

To run a container we can run the following command

docker run -it --name ml centos:latest

After running the docker run command we will directly land into the docker container. Now, we need to install some packages like CLI text editor- vim, and python3.

yum install vim python3-pip -y

After installing python3 we need to install some necessary py libraries to train our Machine Learning Model.

pip3 install numpy pandas sklearn joblib

After installing all the required libraries we need to copy the dataset which we are going to use to train our model.

docker cp SalaryData.csv os1:/

Now, we can write our Machine Learning code in our docker container using vim as a text editor.

vim ml.pyimport pandas 
ds= pandas.read_csv('SalaryData.csv')
from sklearn.linear_model import LinearRegression
mind= LinearRegression()
x= ds['YearsExperience'].values.reshape(30, 1)
y= ds['Salary'],y),y)
import joblib
joblib.dump(mind, 'model.pk1')

This line shows that our code is a success and our model is created( model.pk1).

We need another program to run the pre-created model. Hence, I have created another program for running the model.

vim final.pyimport joblib
mind= joblib.load('model.pk1')
x= input("Enter the experiance in years:")
op =mind.predict(x)

This code will first load our pre-trained model(model.pk1) and ask the number of years of experience then it will predict the estimation of the salary based on the years of experiance.





Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Hotel Image Categorization with Deep Learning

Should I use tensorflow or scikit learn?

Template Matching — Image Processing

All State Claim severity

Does TF-IDF work differently in textbooks and sklearn routine?

Bag of words representation of a document (Word Counts).

Building Machine Learning Pipelines using Pyspark

AISaturdayLagos: May the Tensor Flow with you…

Machine learning on a budget: Hit the ground running!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Tank

Gaurav Tank

More from Medium

Must Know Docker Commands

Predicting future terrorist targets with Amazon Neptune ML

BTS of Amazon S3

Deploying on AWS SageMaker for scheduled Batch Transform