DEV Community

Cover image for Running Machine Learning Model On top of Docker Container.
Subhash Tyler 🚀
Subhash Tyler 🚀

Posted on

Running Machine Learning Model On top of Docker Container.

First build model in Jupyter or any other dev environment.
sample code:

import pandas
db = pandas.read_csv('marks.csv')
y = db["marks"]
x = db['hrs']
x = x.values
x = x.reshape(4,1)
from sklearn.linear_model import LinearRegression
mind = LinearRegression()
mind.fit( x, y)
mind.predict([[ 6 ]] )
mind.coef_

import joblib
mind = joblib.load('marks.pk1')
y = int(input("enter no. of hours: "))
output = mind.predict([[y]])
print(output[0])

save the model as savemodel.py file.

open WinSCP, to transfer these files(marks.csv,savemodel.py) from windows to VirtualBox Linux.

Now open your linux terminal.

systemctl start docker # this will start docker service

docker run -dit --name ML_model centos # this will pull and run docker with latest centos version and having a interactive terminal

docker attach ML_model # this will attach docker container running

we have to install some packages now:
yum install python3
pip install scikit-learn

now go to Base linux os for copying Ml model saved .pk1 file to running docker container.

docker cp savemodel.py ML_Model_check:/ # this will copy model file from base os to container

now come to container,

python3 savemodel.py # this will run the model file and ask for input, give input and get the prediction.

Top comments (0)