Machine Learning over Container
— Launching python environment with docker, to run M.L models
Containers creates an isolated environment for tasks we need to perform. Implementing machine learning algorithms to train models requires some libraries (using python), such as scikit-learn, pandas. But sometimes when using other algorithms or may be other tasks the version of libraries of python may conflict. Thus it’s very ideal to work in isolated environment for every tasks.
The same facility Containers provides.
Thus in this article, I’ll be deploying a container with required libraries to execute machine learning programs which will train models for us. Let’s begin
Firstly, we need to create environment with installing python and required libraries, i.e. scikit-learn & pandas for my example.
For base image, I’ll use
centos:7 . For that let’s pull this image :~
docker pull centos:7
After that, let’s create a test container for creating environment :~
docker run -it --name creating_py_env centos:7
In this container, python is not installed. Let’s patch that up
yum install python36 -y
Now, let’s setup environment by install required libraries:
Required libraries are installed :~
Now, let’s commit this container for base environment :~
docker commit creating_py_env ml_python:v1
Another way to create environment, and more suitable is Dockerfile. Just for sake, Dockerfile for same environment will somewhat look like :~
FROM centos:7RUN yum install python36 -yRUN pip3 install scikit-learn pandasCMD /bin/bash
Now, moving further let’s launch our environment for executing ML program.
docker run -it --name ml_exec_env ml_python:v1
For machine learning, I’ll be using simple example of Linear Regression, using dataset called,
SalaryData.csv , which looks like
Which is used by further program :~
import pandas as pd
from sklearn.linear_model import LinearRegression
import joblibdataset = pd.read_csv("SalaryData.csv")X = dataset["YearsExperience"].values.reshape(-1,1)
y = dataset["Salary"]model = LinearRegression()
Here, using joblib module, our model will be saved in
Now finally we can use this model for prediction, For which I’ll use
Executing this program will result as:
— We’ve used containers to provision environment for executing and testing machine learning model.