2021 AI Invasion: Deploying Medical Insurance Model

Abideen Muhammed
2 min readJun 24, 2021

This is a continuation of our last article/class where I explained how to build a classification model using python. If you miss it, you can read it here.

The first thing you need to do to be able to deploy your model is to determine which API or back end language (like FLASK, Django, RESTful API) you want to use. Another thing is to determine the platform you want to host your model like Herokuapp, etc. There are other platform like Algorithmia that allows easy development of Machine Learning model with little coding, it requires that you have little background in Command Line Interface (CLI).

I used Flask to deployed the medical insurance model we built during the AI Invasion program. The steps involved are as follows:

Step 1: Serialiaze your model using either pickle or joblib. Serializing is like saying saving you model. If you are using pickle, you are to use .pkl as extension. Once you have determined the best model to deploy, in my own case, KNeasrest Neigbor, you apply the code below for it:

The next thing is to set up our file and folders. In order to work with flask, you need to create some statutory folders like templates and static. So you have the main folder and two subfolders now. Let us create our app.py file that will allow us to create flask object for the deployment and necessary URL. Once you have created the app.py, populate it with:

import numpy as np
from flask import Flask, request, jsonify, render_template
import pickle
app = Flask(__name__)
model = pickle.load(open(‘medical_insurance.pkl’, ‘rb’))

@app.route(‘/’)
@app.route(‘/home’)
def hello_world():
return render_template(‘index.html’, title= ‘home’)
@app.route(‘/about’)
def about():
return render_template(‘about.html’, title=’About’)
@app.route(‘/stories’)
def stories():
return render_template(‘stories.html’, title=’About’)

@app.route(‘/predict’,methods=[‘POST’])
def predict():
‘’’
For rendering results on HTML GUI
‘’’
int_features = [int(x) for x in request.form.values()]
final_features = [np.array(int_features)]
prediction = model.predict(final_features)

return render_template(‘index.html’, prediction_text=’The customer status is {}’.format(prediction))

if __name__ == ‘__main__’:
app.run(debug = True)

--

--

Abideen Muhammed

Data Analyst|Data Scientist|Business Analyst|Software Engineer|Machine Learning Enthusiast