Deploy Scikit-Learn Machine Learning Models with Flask

Hiran Hasanka
5 min readJan 14, 2021

--

Photo by Émile Perron from StockSnap

If you are a machine learning enthusiast and a python user, you definitely have heard of Scikit-Learn. It is an open source library for predictive data analysis which is built on famous python libraries and packages like Numpy, Scipy and Matplotlib. Scikit-Learn makes it easy for users to create, train and validate models easily.

Here, I won’t be covering advanced Scikit-Learn topics or data analysis with fancy plots. There are many useful and comprehensive tutorials and guides to learn in-depth about the library and the official documentation of Scikit-Learn is also good learning material for a novice. What I will talk about is creating a simple machine learning model for the Titanic dataset and a Flask app to serve the model as a microservice. In order to create a microservice, we need to go through following tasks:

  1. Get the training dataset.
  2. Create data transformation pipeline to prepare training data.
  3. Create and fit a machine learning model on the titanic dataset.
  4. Save the model and data transformation pipeline.
  5. Create a Flask app, load the model and pipeline to serve the model as a microservice.

1. Get the training dataset

You can get the Titanic dataset from the Kaggle Titanic Machine Learning Competition on Kaggle. The dataset comes as a .zip file. You need to extract it and then load train.csv file using Pandas.

You need to pass the path to the folder that contains the train.csv file when you’re calling load_dataset function.

2. Create data transformation pipeline to prepare training data

Now we need to create a data transformation pipeline to preprocess data. I’m not going in-depth on creating data transformation pipelines. If you’re not familiar with creating pipelines, refer to this article I wrote about creating pipelines.

Here, I’ll create a pipeline to process different kinds of data, numerical, categorical and useless data that should be dropped. I’ll also split the labels and attributes.

After creating the pipeline, first you need to fit the pipeline on train data and then transform the train data using the fitted pipeline. Thanks to the fit_transform of Scikit-Learn, both of those tasks can be done using one function. Most important point to remember is that you should fit the pipeline using only the train dataset. When you’re using the pipeline later, you only need to transform data using the fitted pipeline.

3. Create and fit a machine learning model on the titanic dataset.

After creating and fitting the pipeline and transforming training data, we need to create a machine learning model and train it on training data. Here, I will be using a Support Vector Machine Classifier model for the Titanic dataset.

After instantiating the SVM model, it should be trained using the transformed train dataset. Now we have a data transformation pipeline and a machine learning model, both trained and ready to go.

4. Save the model and data transformation pipeline

With everything done with the model and the pipeline, now it’s time to save them both so that they can be used in the server. Python’s ‘pickle’ module can be used to save these as binary files and to load them back up later.

Pickle will create two binary files model.pkl and pipeline.pkl in the directory you’ve advised it to save them. In this case, they will be created in the same directory as the code exists. After pickle creates the files, you can move them around to use them in any of your python scripts and you’ll have access to the model and the pipeline.

5. Create a Flask app

Now we’re in the final step of the process of creating a microservice. Now we have to create a simple Flask web app to serve the model. After creating the app, you will be able to send in requests and get predictions. I’m not going into detail on Flask here and if you’re not familiar with Flask or Flask_restful, I recommend you to refer this tutorial on Youtube to gain a better understanding of the framework.

Flask web app

This app will take HTTP PUT requests and JSON request data, transform it into a Pandas dataframe. Then it will use the pipeline(which was loaded from the binary file) to transform the data and the model to make predictions. Then the predictions will be sent out in JSON. Now we have a functional web app that we can use to make predictions.

Testing the app

In order to test the app, first run the python file that contains the Flask app. Make sure there are no errors of any kind. If all proceeds properly, the server should run on port 5000. Then, make a HTTP PUT request to http://localhost:5000/predict with a dictionary. Note that the dictionary should have keys whose name should be similar to the column names of original Titanic train set.

Here is an example to making a request using CURL:

curl -XPUT -H "Content-type: application/json" -d '{"PassengerId": 1987, "Pclass": 3, "Name": "Sharapova, Ms. Maria", "Sex": "female", "Age": 24, "SibSp": 0, "Parch": 0, "Ticket": "", "Fare": 112.0, "Cabin": "", "Embarked": "S"}' 'http://127.0.0.1:5000/predict'

The app should return something like the following:

{
"predictions": {
"1": 1
}
}

The model predicted that the imaginary person whose data we sent to it would’ve been survived from the Titanic disaster. Also note that the request dictionary object contains keys for every column in the original dataset.

Here’s another example on how you can get predictions for multiple records at once.

In order to do this, you need to send something like the following to the web app:

{
"PassengerId": [1987, 1123],
"Pclass": [3, 2],
"Name": ["Carey, Ms. Jenna", "Smith, Mr. Steven"],
"Sex": ["female", "male"],
"Age": [24, 23],
"SibSp": [0, 0],
"Parch": [0, 2],
"Ticket": ["S089", "SPG4"],
"Fare": [112.0, 67.0],
"Cabin": ["FG", "TYH"],
"Embarked": ["S", "Q"]
}

Then the web app will return something like this:

{
"predictions": {
"1": 1,
"2": 0
}
}

So the model predicted that the first person would’ve been survived from the Titanic where the second person wouldn’t have been so lucky.

This is a very basic program to enable users access to your trained model using a web application. By enhancing these simple steps, you can create more advanced models and web applications, even for your real-life projects that might involve clients accessing a model via the internet or you hosting a microservice in order to provide some functionality for one of your apps.

--

--

Hiran Hasanka
Hiran Hasanka

No responses yet