Deploy A Machine Learning Model Using Flask - Towards Data Science
Deploy A Machine Learning Model Using Flask - Towards Data Science
https://towardsdatascience.com/deploy-a-machine-learning-model-using-flask-da580f84e60c 1/12
11/10/2019 Deploy a machine learning model using flask - Towards Data Science
source
In this article, we are going to use simple linear regression algorithm with
scikit-learn for simplicity, we will use Flask as it is a very light web
framework. We will create three files,
1. model.py
2. server.py
https://towardsdatascience.com/deploy-a-machine-learning-model-using-flask-da580f84e60c 2/12
11/10/2019 Deploy a machine learning model using flask - Towards Data Science
3. request.py
As I mentioned above, in this file we will develop our ML model and train it.
We will predict the salary of an employee based on his/her experience in
the field. You can find the dataset here.
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import pickle
import requests
import json
https://towardsdatascience.com/deploy-a-machine-learning-model-using-flask-da580f84e60c 3/12
11/10/2019 Deploy a machine learning model using flask - Towards Data Science
Importing the libraries that we are going to use to develop our model.
numpy and pandas to manipulate the matrices and data respectively,
sklearn.model_selection for splitting data into train and test set and
sklearn.linear_model to train our model using LinearRegression. pickle to
save our trained model to the disk, requests to send requests to the server
and json to print the result in our terminal.
dataset = pd.read_csv('Salary_Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values
We have imported the dataset using pandas and separated the features and
label from the dataset.
In this section, we have split our data into train and test size of 0.67 and
0.33 respectively using train_test_split from sklearn.
https://towardsdatascience.com/deploy-a-machine-learning-model-using-flask-da580f84e60c 4/12
11/10/2019 Deploy a machine learning model using flask - Towards Data Science
regressor = LinearRegression()
regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)
pickle.dump(regressor, open('model.pkl','wb'))
We will save our trained model to the disk using the pickle library. Pickle is
used to serializing and de-serializing a Python object structure. In which
python object is converted into the byte stream. dump() method dumps the
object into the file specified in the arguments.
In our case, we want to save our model so that it can be used by the server.
So we will save our object regressor to the file named model.pkl.
model = pickle.load(open('model.pkl','rb'))
print(model.predict([[1.8]]))
pickle.load() method loads the method and saves the deserialized bytes to
model. Predictions can be done using model.predict().
For example, we can predict the salary of the employee who has experience
of 1.8 years.
Here, our model.py is ready to train and save the model. The whole code of
model.py is as follows.
https://towardsdatascience.com/deploy-a-machine-learning-model-using-flask-da580f84e60c 6/12
11/10/2019 Deploy a machine learning model using flask - Towards Data Science
# Splitting the dataset into the Training set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size =
1/3, random_state = 0)
2. server.py
In this file, we will use the flask web framework to handle the POST
requests that we will get from the request.py.
Importing the methods and libraries that we are going to use in the code.
import numpy as np
from flask import Flask, request, jsonify
https://towardsdatascience.com/deploy-a-machine-learning-model-using-flask-da580f84e60c 7/12
11/10/2019 Deploy a machine learning model using flask - Towards Data Science
import pickle
Here we have imported numpy to create the array of requested data, pickle
to load our trained model to predict.
In the following section of the code, we have created the instance of the
Flask() and loaded the model into the model.
app = Flask(__name__)
model = pickle.load(open('model.pkl','rb'))
Here, we have bounded /api with the method predict(). In which predict
method gets the data from the json passed by the requestor. model.predict()
method takes input from the json and converts it into 2D numpy array the
results are stored into the variable named output and we return this variable
after converting it into the json object using flasks jsonify() method.
@app.route('/api',methods=['POST'])
def predict():
https://towardsdatascience.com/deploy-a-machine-learning-model-using-flask-da580f84e60c 8/12
11/10/2019 Deploy a machine learning model using flask - Towards Data Science
data = request.get_json(force=True)
prediction = model.predict([[np.array(data['exp'])]])
output = prediction[0]
return jsonify(output)
Finally, we will run our server by following code section. Here I have used
port 5000 and have set debug=True since if we get any error we can debug it
and solve it.
if __name__ == '__main__':
app.run(port=5000, debug=True)
Here, our server is ready to serve the requests. Here is the whole code of the
server.py.
# Import libraries
import numpy as np
from flask import Flask, request, jsonify
import pickle
app = Flask(__name__)
@app.route('/api',methods=['POST'])
def predict():
# Get the data from the POST request.
data = request.get_json(force=True)
# Make prediction using model loaded from disk as per the data.
prediction = model.predict([[np.array(data['exp'])]])
return jsonify(output)
if __name__ == '__main__':
app.run(port=5000, debug=True)
3. request.py
As I mentioned earlier that request.py is going to request the server for the
predictions.
import requests
url = 'http://localhost:5000/api'
https://towardsdatascience.com/deploy-a-machine-learning-model-using-flask-da580f84e60c 10/12
11/10/2019 Deploy a machine learning model using flask - Towards Data Science
r = requests.post(url,json={'exp':1.8,})
print(r.json())
Conclusion
We have created three files model.py, server.py and request.py to train and
save a model, to handle the request, to make a request to the server
respectively.
After coding all of these files, the sequence to execute the files should be
model.py, server.py(in a separate terminal) and at the end request.py.
You can compare the results of prediction with a model.py as we printing the
result at the end of the file.
https://towardsdatascience.com/deploy-a-machine-learning-model-using-flask-da580f84e60c 11/12
11/10/2019 Deploy a machine learning model using flask - Towards Data Science
Thank you :)
https://towardsdatascience.com/deploy-a-machine-learning-model-using-flask-da580f84e60c 12/12