Deploy a Machine Learning Model using Flask: Step-By-Step

Claudio Sabato
6 min readApr 15

Are you trying to deploy a machine learning model and wondering how? One way to do it is by using Flask.

Recently, there has been an increase in the use of Machine Learning, a fast-developing technology.

Predictive models powered by machine learning help resolve complex problems like projecting sales, predicting customer attrition, and performing sentiment analysis of text documents.

The challenge, however, is far from over after creating a machine-learning model. The model must be also made available for users to access it.

Deploying machine learning models is possible with Flask, a popular Python web framework.

This tutorial will show how to deploy machine learning models using Flask.

Which Python Modules to Use For Machine Learning?

Before starting, you must install a few dependencies on your computer to train a machine learning model and use Flask to communicate with the trained model.

Install the following dependencies:

  • Python
  • Flask
  • Scikit-learn
  • pickle

Visit Python’s official website, download it, and install it on your computer. To install Flask, Scikit-learn, and the pickle module use the following commands in your command line interface:

pip install Flask
pip install scikit-learn
pip install pickle

How to Build a Machine Learning Model Using Python

We will start by training a machine learning model using the famous Iris dataset. We will train a classification model which predicts the species of this flower.

Here is what the CSV dataset looks like:

sepal.length,sepal.width,petal.length,petal.width,species
5.1,3.5,1.4,0.2,Setosa
4.9,3,1.4,0.2,Setosa
4.7,3.2,1.3,0.2,Setosa
4.6,3.1,1.5,0.2,Setosa
5,3.6,1.4,0.2,Setosa

To train the model, we will use the scikit-learn library.

Here is the complete code to obtain a pickled model from the dataset.

Claudio Sabato

I’m a Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!