Deploy Machine Learning Model using Flask to Heroku — Beginners(Part 1)

Joan Ngugi
Analytics Vidhya
Published in
4 min readFeb 9, 2021

You have just learned how to create machine learning models. You model is working as you would like it to. But then you have one dilemma, how to take your model from your notebook and package it as a product that people can use with ease.

This tutorial aims to give an introduction to Machine Learning beginners on how to create and deploy machine learning models.

  1. The first part will be a walkthrough of the machine learning model. This tutorial however assumes that you have basic knowledge of machine learning models, and therefore the notebook has not covered in depth aspects of data pre-processing and model implementation. The notebook and the rest of the flask, html code can be found on GitHub using this link https://github.com/Jnjerry/diabetespredictor.
  2. The second part will be a guide on how to embed your machine learning model into a flask application for the model to be used as a web application.
  3. The third part will be a guide on how to deploy your machine learning to Heroku via GitHub. You can find the final output here https://joan-diabetes-predictor.herokuapp.com/.

The model predicts the likelihood of a patient having diabetes by measuring several medical predictors.

Part 1: Model Implementation

The very first part of any machine learning problem is usually the importation of libraries. I have also included the dataset description for better understanding of the data. The datasets consists of several medical predictors, the independent variables and one target dependent variable, Outcome. Outcome 1 indicates that the patient had diabetes and Outcome 0 indicates the patient did not have diabetes after the medical test.

Loading your dataset and exploring it usually the next step.

We then proceed to explore the shape of our dataset, the type and if there are any missing values. Luckily, there are no missing values in any column and therefore this doesn’t require any further action from us.

We then proceed to identify and see the correlation between the features via a heatmap as show below.

Our heatmap shows strong correlation between a few columns. Insulin is highly correlated with SkinThickness. SkinThickness is also highly correlated with BMI. Age is highly correlated with Pregnancies.

Machine Learning

The Machine learning aspect of this guide begins from here. We use a logistic regression model to determine the likelihood of someone having diabetes based on historical medical predictors of other patients who were found to have diabetes after tests.

We begin by importing our machine learning libraries. We then proceed to specify our target variable y, Outcome, and our independent variables X, which are every other column in our data set except Outcome.

We have then split our dataset into training and testing sets in a ration 70:30 respectively. After that we call our Logistic Regression model and label it lg and then proceed to fit the model in our training dataset.

We have then predicted our model and used the output to print out a classification report score of the precision, recall, f1-score.

We’ve then proceeded to predict using our model given our medical predictors. We can see the output is 0, meaning the patient is unlikely to have diabetes.

That will be all for part one. The next part is about using your model in a flask application and displaying the output from a web page.

--

--