Machine Learning Model Inside docker

Subhashyadav
4 min readMay 30, 2021

What is docker?

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. Docker enables us to boot , start and run new OS(container) within a second.

we are going to use Docker to launch an centos OS. AND inside that container we will build and run simple Machine learning Model. We will create a model of Linear regression model using a data set (SalaryData.csv).

Here one column is yearsExperience and other is Salary. we will predict salary using this data set.

I have Used Winscp to transfer file from windows to Linux. Now WE have our data set inside our DOCKER Host .

Now to launch centos docker container . We will start docker service.

Command: systemctl start docker

and will check if it is running or not.

Command: systemctl status docker

Now I will pull centos image from docker hub.

Command: docker pull centos:latest

In my Case it is already pulled.

Now we will name a new OS with a name ML_Task .

Command :docker run -it —name ML_Task centos

Now we will Install python inside container to train our Machine learning model.

command:[ yum install python3]

Now we will copy our dataset from host machine to docker container using docker cp command.

command: docker cp SalaryData.csv ML_Task:/

Command: pip3 install sklearn

Now we will build our model inside python .

This is python code of simple Linear Regression .

Here , I will discuss about the code.

import pandas

// This line includes panda library to our code.

data = pandas.read_csv(‘SalaryData.csv’)

// Here panda is helping to load our data set to our dataset.

x = data[‘YearsExperience’]

// here we are taking our feature [year of experience ] in x variable

x=x.values.reshape(-1,1)

// this changes the dimension of x variable .since machine learning model does not take one dimension data. we can see the shape of x variable using x.shape command.

y = data[‘Salary’]

// here we are taking our predication or output in y variable

from sklearn.linear_model import LinearRegression

// this line helps us to include our regression algorithm to our program

model = LinearRegression()

// here we are calling linear regression algorithm .

model.fit(x,y)

// here we are fedding our data to algorithm

model.predict([[1]])

// Here we are predicting our output for 1 year exprience

import joblib

joblib.dump(model,”project1.pki”)

// This code help to save our predicted model for use in webapp or mobile app. And we can load this code in backend app using joblib library and load() function.

app

Thank you. This is all from this articles. I hope you liked it.

Please do share and like this.

--

--