Designing a Machine Learning model and Deploying it using Flask on Heroku
Model deployment is as important as model building. Machine Learning models are also effective when it is in production and actively in use by consumers. In this article, I am going to train Machine Learning model, create web application on it using Flask and deploy it using Heroku.
Random Forest is supervised machine learning algorithm which consists of many decision trees. The Random Forest combines hundreds or thousands of decision trees, train each on different subsets of samples, splitting is done considering limited features. The final output is obtained by averaging predictions of each tree. This concept is bootstrapping.
Dataset = Adult Census Income Dataset.
Attributes : There are 15 attributes, few of them are given below.
income : >50k, ≤50k
age : continuous
workclass : private, self-emp-not-inc, self-emp-inc, Federal-gov, local-gov, State-gov, Without-pay, Never-worked
fnlwgt : continuous
For details on attributes Click here.
Prediction task is to determine whether a person makes over 50k or less in a year.
Load Libraries
Load data
check for null values
looking at the data
some feature engineering
Encoding data
Correlated features
Removing correlated features and dividing data into train and test
Fitting model
With this we achieve accuracy of 85% approximately. We will save the model to predict our values later using pickle library in python.
Next step is to create simple web app using flask.
We will create form page to collect the data from people. The form will contain different options to select from each attribute. Code here
Creating virtual environment to run flask script.
mkdir income
cd income
python3 -m venv venv
#activating environment
source venv/bin/activate
pip install Flask
Creating folder “templates” which will contain html files (index.html, result.html).
Flask script
Running the script
export FLASK_APP = script.py
run flask
When form is submitted, value of income is predicted using model.pkl file we created before.
After creating web app, we will use heroku to deploy it.
Deploying flask app using Heroku
Heroku is a platform as a service (PaaS) that enable developers to build, run and operate applications in the cloud. Also, we need to install git and Heroku CLI in system. Go to Heroku and create your account.
Downloading gunicorn to virtual environment venv
pip install gunicorn
Initializing git repository as shown by commands below.
$git init
$git add.
$git commit -m ‘initial commit’
We need to tell heroku that project requires libraries like flask, gunicorn, sklearn etc. For this we create requirements text file.
pip freeze>requirements.txt
Next, procfile text file is created in root directory of application, to explicitly declare what command should be executed to start app.
Now project is ready to be deployed.
Open terminal and execute following commands —
heroku login
This will require login credentials we used to create our Heroku account before.
#creating heroku ap
heroku create
git push heroku master
heroku open
This will push entire app on heroku and open the url in the browser.
Task completed!
Hope my article makes sense especially to beginners.
Thank you