0% found this document useful (0 votes)
13 views10 pages

How To Easily Deploy Machine Learning Models Using Flask - by Abhinav Sagar - Towards Data Science

The document provides a guide on deploying machine learning models using Flask, focusing on a linear regression model to predict sales. It outlines the project structure, necessary tools, and steps for creating a web application that takes user input and returns predictions. The article emphasizes the importance of making machine learning models accessible in production environments.

Uploaded by

Lud Cam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

How To Easily Deploy Machine Learning Models Using Flask - by Abhinav Sagar - Towards Data Science

The document provides a guide on deploying machine learning models using Flask, focusing on a linear regression model to predict sales. It outlines the project structure, necessary tools, and steps for creating a web application that takes user input and returns predictions. The article emphasizes the importance of making machine learning models accessible in production environments.

Uploaded by

Lud Cam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science

How to Easily Deploy Machine Learning


Models Using Flask
And take your models from jupyter notebook to production

Abhinav Sagar Oct 2, 2019 · 5 min read

Photo by SpaceX on Unsplash

Stuck behind the paywall? Click here to read the full story with my Friend Link!

When a data scientist/machine learning engineer develops a machine learning model using
Scikit-Learn, TensorFlow, Keras, PyTorch etc, the ultimate goal is to make it available in
production. Often times when working on a machine learning project, we focus a lot on
Exploratory Data Analysis(EDA), Feature Engineering, tweaking with hyper-parameters etc.
But we tend to forget our main goal, which is to extract real value from the model predictions.

https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 1/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science

Deployment of machine learning models or putting models into production means making
your models available to the end users or systems. However, there is complexity in the
deployment of machine learning models. This post aims to make you get started with putting
your trained machine learning models into production using Flask API.

I will be using linear regression to predict the sales value in the third month using rate of
interest and sales of the first two months.

What is Linear Regression


The objective of a linear regression model is to find a relationship between one or more
features(independent variables) and a continuous target variable(dependent variable). When
there is only feature it is called Uni-variate Linear Regression and if there are multiple
features, it is called Multiple Linear Regression.

Hypothesis of Linear Regression


The linear regression model can be represented by the following equation

Y is the predicted value

θ₀ is the bias term.

θ₁,…,θₙ are the model parameters

x₁, x₂,…,xₙ are the feature values.

https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 2/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science

Linear regression illustrated

Why Flask?
Easy to use.

Built in development server and debugger.

Integrated unit testing support.

RESTful request dispatching.

Extensively documented.

Project Structure
This project has four parts :

1. model.py — This contains code for the machine learning model to predict sales in the
third month based on the sales in the first two months.

2. app.py — This contains Flask APIs that receives sales details through GUI or API calls,
computes the predicted value based on our model and returns it.

3. request.py — This uses requests module to call APIs defined in app.py and displays the
returned value.

4. HTML/CSS — This contains the HTML template and CSS styling to allow user to enter
sales detail and displays the predicted sales in the third month.

https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 3/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science

Pipeline for deployment of a Machine Learning model

Environment and tools


1. scikit-learn

2. pandas

3. numpy

4. flask

Where is the code?


Without much ado, let’s get started with the code. The complete project on github can be
found here.

Let’s get started with making the front end using HTML for the user to input the values. There
are three fields which need to be filled by the user — rate of interest, sales in first month and
sales in second month.

https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 4/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science

Next I did some styling using CSS for the input button, login buttons and the background.

https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 5/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science

I created a custom sales dataset for this project which has four columns — rate of interest,
sales in first month, sales in second month and sales in third month.

https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 6/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science

Let’s now make a machine learning model to predict sales in the third month. First let’s deal
with missing values using Pandas. Missing Data can occur when no information is provided
for one or more items. I filled the rate column with zero and sales in first month with mean of
that column if the value was not provided. I used linear regression as the machine learning
algorithm.

Serializing/De-Serializing
In simple words serializing is a way to write a python object on the disk that can be transferred
anywhere and later de-serialized (read) back by a python script.

Serialization, De-Serialization

I converted the model which is in the form of a python object into a character stream using
pickling. The idea is that this character stream contains all the information necessary to
reconstruct the object in another python script.

https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 7/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science

The next part was to make an API which receives sales details through GUI and computes the
predicted sales value based on our model. For this I de- serialized the pickled model in the
form of python object. I set the main page using index.html . On submitting the form values
using POST request to /predict , we get the predicted sales value.

The results can be shown by making another POST request to /results. It receives JSON
inputs, uses the trained model to make a prediction and returns that prediction in JSON format
which can be accessed through the API endpoint.

https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 8/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science

Finally I used requests module to call APIs defined in app.py . It displays the returned sales
value in the third month.

Results
Run the web application using this command.

$ python app.py

https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 9/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science

Open http://127.0.0.1:5000/ in your web-browser, and the GUI as shown below should appear.

Graphical user interface

Conclusions
This article demonstrated a very simple way to deploy machine learning models. I used linear
regression to predict sales value in the third month using rate of interest and sales in first two
months. One can use the knowledge gained in this blog to make some cool models and take
them into production so that others can appreciate their work.
https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 10/10

You might also like