Training Machine Learning(ML) Model inside Docker Container
Docker On its Way

Training Machine Learning(ML) Model inside Docker Container

TASK DESCRIPTION

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

👉 Install Python on top of the docker container.

👉 In Container you need to copy/create a machine learning model which you have created in Jupyter Notebook.

NOTE: Before pulling any docker container image we have to make sure that docker is installed or not.

[root@localhost ~]# docker --version



We can check the installation using the above command. If this works fine we can go ahead.

No alt text provided for this image

This command shows the installed version of docker. In my case, it is Docker Version 20.10.6.

Now, we will start the docker service. For this, we have to run the below-mentioned command.

[root@localhost ~]# systemctl start docker

To check docker service has been started or not. We can go use this:-

[root@localhost ~]# systemctl status docker

No alt text provided for this image

As we see, the docker service is running perfectly, we will now pull the centos image from the docker hub.

To pull centos image

No alt text provided for this image

The latest version of Centos image has been downloaded. Now, we will create a container using this command

[root@localhost ~]# docker run -it --name MyOS centos:latest

  • -it option is used to launch the interactive terminal of the container created within seconds. Container here is referred to as Operating system.
  • --name: provides the name of our choice to the container otherwise docker will assign some default name.
No alt text provided for this image

Now, we need to proceed further to install python in our container.

To install any software in RHEL8 or CentOS we use the 'yum' command.

[root@99caa5f1428e /]# yum install python3

No alt text provided for this image
No alt text provided for this image

As we see, python is successfully installed in our container.

Now we will install certain libraries which will use to train our Machine Learning Model.

  • numpy: pip3 install numpy
  • pandas: pip3 install pandas
  • scikit-learn: pip3 install scikit-learn
No alt text provided for this image

We are ready with all the requirements. We can now proceed further.

Before proceeding, we need the dataset in our container. There can be n number of methods for the same. The one I am doing is downloading the dataset in my Linux System.

No alt text provided for this image

We are having our dataset file in the Downloads directory. From here we are going to copy this file into our container.

docker cp <sourcefile_path> <container_name>:<destination_path>

Using the above-mentioned command we can transfer our dataset to the docker container.

In this case, our <source_path> is /root/Downloads/salaryData.csv.

No alt text provided for this image

For <destination_path> , we have to create a directory.

mkdir/root/salaryPredictionModel

[root@99caa5f1428e /]# mkdir /root/salaryPredictionModel

No alt text provided for this image

Our <container_name> is MyOS and now our <destination_path> is /root/salaryPredictionModel

Now using this command, we can have our dataset in our docker container.

[root@localhost ~]# docker cp /root/Downloads/salaryData.csv      MyOS:/root/salaryPredictionModel/

No alt text provided for this image

We can use the ls command to see the copied file in our container.

No alt text provided for this image

Now, it's time to train our model and write the python program for the same.

No alt text provided for this image

We have written our python program in main.py using the vi editor.

No alt text provided for this image

Output:-

No alt text provided for this image

We can run the file using python3 main.py to check if it's working fine or not.

Now, our model has been created and saved. Now we can predict the salary based on the Years of Experience. For this, we will create a new file namely: prediction.py.

No alt text provided for this image

The above script will first the model we created in main.py and then it will predict the salary based on Years of experience. Now we will run python3 prediction.py

No alt text provided for this image
No alt text provided for this image

We have successfully created the model and done the prediction for the given dataset i.e. salaryData.csv

Thank you for reading 😃

To view or add a comment, sign in

More articles by Deepanshu Bhalla

Others also viewed

Explore content categories