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 :~

After that, let’s create a test container for creating environment :~

In this container, python is not installed. Let’s patch that up

Now, let’s setup environment by install required libraries:
— scikit-learn
— pandas

Installing scikit-learn

Required libraries are installed :~

Now, let’s commit this container for base environment :~

Another way to create environment, and more suitable is Dockerfile. Just for sake, Dockerfile for same environment will somewhat look like :~

RUN yum install python36 -yRUN pip3 install scikit-learn pandasCMD /bin/bash

Now, moving further let’s launch our environment for executing ML program.

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 :~

dataset = pd.read_csv("SalaryData.csv")X = dataset["YearsExperience"].values.reshape(-1,1)
y = dataset["Salary"]
model = LinearRegression(),y)
joblib.dump(model, "salary_model.pk1")

Here, using joblib module, our model will be saved in salary_model.pk1 file.

Now finally we can use this model for prediction, For which I’ll use program

Executing this program will result as:

That’s it!!

Conclusion :~

— We’ve used containers to provision environment for executing and testing machine learning model.

Thank you.



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