7

Machine Learning Model Deployment as REST API in Four Easy Steps

 2 years ago
source link: https://towardsdatascience.com/machine-learning-model-deployment-as-rest-api-in-four-easy-steps-e65764f7c5c
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Machine Learning Model Deployment as REST API in Four Easy Steps

Deploy your machine learning model in four steps

In my previous blog on Machine Learning Ensemble modeling, we developed a classification model to classify patients as diabetic or not based on medical history and diagnostics like the number of pregnancies the patient has had, their BMI, insulin level, age, and so on.

Below is the link to the article:

Below is the data source and summary of exercise in that blog to create an optimal model:

Data Source:

Below is the Python code of the model:

import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier# Read data into pandas dataframe
df=pd.read_csv(r'C:\Machinelearning\diabetes.csv')
#Define Feature Matrix (X) and Label Array (y)
X=df.drop(['Outcome'],axis=1)
y=df['Outcome']lr=RandomForestClassifier(n_estimators=53, n_jobs=1,random_state=8)
lr.fit(X,y)

Okay, we got the model ready. Now it’s time to deploy it.

In real-life projects, machine learning models are used by other applications. These applications can be anything, it can be a customer-facing operational application like company website or back-office application like ERP or it can be a data analytics application. These applications should be able to send feature variable values to the model and get a prediction.

While there are various ways to achieve it, in this blog I am going to take you through steps of deploying machine learning models through REST API. Once the model can interact through the REST API, any application can seek prediction from the model by passing feature values through API call.

The below figure shows steps we are going to follow. Step 1 is already done.

1*7SMWIjw2Y5Y5MEMfhDx5WQ.png?q=20
machine-learning-model-deployment-as-rest-api-in-four-easy-steps-e65764f7c5c

Step2: Serialized the Model

Run below lines of Python code to complete step 2.

#Serialize the model and save
from sklearn.externals import joblib
joblib.dump(lr, 'randomfs.pkl')
print("Random Forest Model Saved")#Load the model
lr = joblib.load('randomfs.pkl')# Save features from training
rnd_columns = list(X_train.columns)
joblib.dump(rnd_columns, 'rnd_columns.pkl')
print("Random Forest Model Colums Saved")

Output:

Random Forest Model Saved
Random Forest Model Colums Saved

Now you should find below two files created on your system. If you are using the Windows operating system and have not set up Python path to something else, you will find these files in C:\Users\<your userid> folder.

1*HGcTcLUu7coqJg1n0V9kKA.png?q=20
machine-learning-model-deployment-as-rest-api-in-four-easy-steps-e65764f7c5c

So far so good!

Make sure to combine python code in Step 1 and Step2 in one file and store it in the same directory as above. Let’s name this file randomfs.py

1*_jrqyFOM8-11ZWbbNnkSmQ.png?q=20
machine-learning-model-deployment-as-rest-api-in-four-easy-steps-e65764f7c5c

In summary, below is the content of file randomfs.py

import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier# Read data into pandas dataframe
df=pd.read_csv(r'C:\Machinelearning\diabetes.csv')
#Define Feature Matrix (X) and Label Array (y)
X=df.drop(['Outcome'],axis=1)
y=df['Outcome']lr=RandomForestClassifier(n_estimators=53, n_jobs=1,random_state=8)
lr.fit(X,y)
#Serialize the model and save
from sklearn.externals import joblib
joblib.dump(lr, 'randomfs.pkl')
print("Random Forest Model Saved")#Load the model
lr = joblib.load('randomfs.pkl')# Save features from training
rnd_columns = list(X_train.columns)
joblib.dump(rnd_columns, 'rnd_columns.pkl')
print("Random Forest Model Colums Saved")

Step3: Web Service

In case you are wondering what is web service, here’s a short and simple explanation from Wikipedia

a Web service is a server running on a computer device, listening for requests at a particular port over a network, serving web documents (HTML, JSON, XML, Images), and creating web applications services, which serve in solving specific domain problems over the web (www, internet, HTTP).

In case you are more confused now, I will say, don’t worry too much about it for now. Just consider it as a server on which website runs. Web sites can talk to other applications through these services called web service.

Okay, now that we got this part out of our way, let’s understand what we need to do in step 3.

Python has a very useful library Flask, which is a web service development framework. If you are using Anaconda distribution, Flask is already installed for you.

Now comes the real action. Write below the line of code in a notepad of your choice and save it as a .py file. Do not use the Jupyter notebook for this. You might get errors if you use a Jupyter notebook for this.

#Install Libraries
from flask import Flask, request, jsonify
from sklearn.externals import joblib
import traceback
import pandas as pd
import numpy as npapplication = Flask(__name__)@application.route(‘/prediction’, methods=[‘POST’])#define functiondef predict():
if lr:
try:
json_ = request.json
print(json_)
query = pd.get_dummies(pd.DataFrame(json_))
query = query.reindex(columns=rnd_columns, fill_value=0)predict = list(lr.predict(query))return jsonify({‘prediction’: str(predict)})except:return jsonify({‘trace’: traceback.format_exc()})
else:
print (‘Model not good’)
return (‘Model is not good’)if __name__ == ‘__main__’:
try:
port = int(sys.argv[1])
except:
port = 12345 lr = joblib.load(“randomfs.pkl”)
print (‘Model loaded’)
rnd_columns = joblib.load(“rnd_columns.pkl”) # Load “rnd_columns.pkl”
print (‘Model columns loaded’)app.run(port=port, debug=True)

I saved it as rndfsapi.py in the same directory where the other three files are stored.

1*J512hmoz5rhnDbEyS9JIFg.png?q=20
machine-learning-model-deployment-as-rest-api-in-four-easy-steps-e65764f7c5c
1*_zJj6tB2Djq7Sgp2dxLehQ.png?q=20
machine-learning-model-deployment-as-rest-api-in-four-easy-steps-e65764f7c5c

Now open Anaconda prompt and run command python rndsfapi.py. This will compile the file and start Web service at mentioned port 12345.

1*z8Bz3DvxjiUL1_16bJVpqg.png?q=20
machine-learning-model-deployment-as-rest-api-in-four-easy-steps-e65764f7c5c

We have completed activities in this step. Let’s move on to the next step.

Step 4: Test REST API

You will need a REST API testing tool like Postman for this step. You can download Postman from the below link.

Once downloaded, start postman. Enter the show URL beside POST action and click on the Send button. You will see something like below.

1*51u3M5o-BK_Ilg1r7_VBkA.png?q=20
machine-learning-model-deployment-as-rest-api-in-four-easy-steps-e65764f7c5c

Now in the body section enter the following feature values. This is how the calling application has to send the feature values for which it needs prediction.

[
{“Pregnancies”:7,”Glucose”:120,”BloodPressure”:90,”SkinThickness”:23,”Insulin”:135,”BMI”:28,”DiabetesPedigreeFunction”:0.5,”Age”:45}
]

Enter these feature values in body and click on Send button.

1*sqNsdTeacKBdvdW7AJ160w.png?q=20
machine-learning-model-deployment-as-rest-api-in-four-easy-steps-e65764f7c5c

You can view the prediction below in the highlighted Body section. This patient is non-diabetic.

Congratulations!

Now you know how to deploy a machine learning model as REST API.

References:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK