MACHINE LEARNING MODEL ON DOCKER CONTAINER.

Harsh Jain
4 min readMay 27, 2021

--

Task 01 👨🏻‍💻

Task Description 📄

👉 Pull the Docker container image of CentOS image from DockerHub and create a new container

👉 Install the Python software on the top of docker container

👉 In Container you need to copy/create machine learning model which you have created in jupyter notebook

👉 Create a blog/article/video step by step you have done in completing this task.

👉 Submit the link of blog/article or video

pre-requisite : docker installed in your OS

To check whether docker is installed or not, use this command

docker info

To use docker , we have to first start docker services . And make it enable ,so that the services started permanently.

systemctl start docker

systemctl enable docker

to check status of docker, we use

systemctl status docker

Now , we pull image from DockerHub. Following command is used to download latest version of centos.

docker pull centos:latest

Note: you have an account on DockerHub and sign-in there.

Now, to run a docker container with name lwtask1, i have used this command

docker run -it --name lwtask1 centos

Now you are entered in your newly created container, and if you want to check , then you can run docker ps command in your base vm.

docker ps

Now we have to add some softwares in this container. So we will install python3 in this container using this command.

yum install python3 -y

After this, we have to install some libraries like pandas and scikit-learn for our machine learning model.

pandas is used to read the csv file and scikit-learn is used for linear regression and other functions.

pip3 install pandas

pip3 install scikit-learn

All installations are done, now moving to the main part

I have created a directory to maintain our task code with name “lwstask1”.

Now coming back to our base vm , from there we have to copy the dataset from base vm to container os, using the following command.

docker cp /root/Documents/Salary_Data.csv

lwtask1:/root/lwstask1/

Now the requires dataset is copied in docker container and now we can train our model using this code. I have created test.py file for it. Here is the code,

vi test.py #for creating new python file

# Load Dataset
import pandas
db=pandas.read_csv(“Salary_Data.csv”)
print(“Dataset Loaded Successfully”)

# Creating targets and features
# x = feature / independent variable
# y = target / dependent variable

x = db[“YearsExperience”]
y = db[“Salary”]

# Loading LinearRegression
from sklearn.linear_model import LinearRegression
model = LinearRegression()

x = db[“YearsExperience”].values.reshape(30,1)
model.fit(x,y)
print(“ Model Created Successfully”)

# Saving Trained Model
import joblib
joblib.dump(model,”salary_model.pk1")
print(“ Model Saved Successfully”)

model=joblib.load(“salary_model.pk1”)
# Let’s check this model

years = float(input(“ Enter Years of Experience: “))
predict = model.predict([[years]])
print(“ Your Predicted Salary is : “,round(predict[0],2),”INR”)

After this, run above code using command python3 test.py

After running this code your model will be trained and you will get your result.

Check prediction with our created model code.

Task done Successfully.

Thanks !!!!!!!!!!

--

--

No responses yet