Create ML model inside the docker container

niteshthapliyal

Nitesh Thapliyal

Posted on May 27, 2021

Create ML model inside the docker container

Hello everyone,

In this article we will be seeing how we can create a Machine learning model inside the docker container.

Before we start first let's discuss about the basic terminologies that will help us to understand article better.

What is Containerization?

Alt Text

Containerization is a technology that created a virtual OS environment where we can work like we usually do in our system. It launches the new OS , Install the OS and boot the OS in seconds and it saves lots of time and solve many use case.

The tool or software that works on Containerization is Docker.

Alt Text

Machine Learning

Alt Text

Machine Learning is a branch of Artificial Intelligence where we try to make machine intelligent by training them on historical data to gain experience so that it can take decision based on experience.
Here training a machine means creating a model that helps machine to take decisions.
ML behind the scene uses Mathematics to calculate the weight or coefficient based in historical data.
Mathematical formula that machine used to predict salary in this article is y = c + wx
here,

  • y = Independent Variable
  • c = Constant
  • w = weight/coefficient
  • x = Feature/independent variable

Not we know about required terminologies that will help us to understand article better so, let's begin...

Prerequisites:

  • Linux[here RHEL is used ]
  • Docker installed in your system

Start Docker

To start to docker use command systemctl start docker
and to check the status of docker i.e docker is started or not use command systemctl status docker

Alt Text

Lauch Docker image

To launch docker image use command docker run -it --name <name-of-os> <image-name:version>

Container

Now we are inside container to check that you are inside the os that you have launched, so for that use command
cat /etc/os-release
This will show the information about your OS.

Now we need to install the required packages to create model.

  1. Python
    To install python use command yum install python3

  2. Scikit-learn
    To install Scikit-learn use command yum install Scikit-learn

Now we need dataset to train our model.
To copy dataset from base OS to container use command docker cp <path-of-file> <container-id>: <path where you want to paste file in container?

now we have dataset, let's train the model.
In the code below model is created

Alt Text

To load the dataset in our code we use module called Pandas

Here x is variable that contains the Feature called Year of Experience that helps to predict the salary of the user.

x is called independent variable or a feature.

Here y is called dependent variable as it contains the target value to which x is dependent i.e Salary

Values is use to convert pandas into Numpy array.

reshape() is use to convert one dimension array to two dimension array.

We need to convert one dimension array to two dimension array because model does not support one dimensional array.

fit() is use to get the weight or coefficient that helps to predict the salary.

predict() is use to predict the salary with the help of weight or coefficient.

Now our model is crated we can use it to find the salary of a person based on its experience.

To watch the demo do watch the video mentioned below.


This video is unlisted so you will not find it in YouTube channel.

Thank you!!

💖 💪 🙅 🚩
niteshthapliyal
Nitesh Thapliyal

Posted on May 27, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related