16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
Open in app Get started
Published in Towards Data Science
You have 2 free member-only stories left this month.
Sign up for Medium and get an extra one
Gurami Keretchashvili Follow
Dec 29, 2021 · 5 min read · · Listen
Save
How to Deploy Machine Learning Models
The easiest way to deploy machine learning models on the web
Introduction
I will introduce the easiest way to deploy machine learning applications on the web.
In the previous notebooks, I have built machine learning models using linear and
tree-based models. It turned out that hyper tuned XGboost model performed best.
That is why today we will build a Fish Weight Prediction web application using
XGboost. In general, there are different options to deploy ML models, such as Flask,
Django, Streamlit, etc. Today I will use Streamlit because it is the easiest and faster
way to do it and it does not require any web development knowledge.
179 1
[Link] 1/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
ML model deployment demo(Photo by the author)
Open in app Get started
∘ Introduction
∘ Fish Weight Prediction Application
∘ How was it done?
∘ Conclusion:
Fish Weight Prediction Application
You can see the Demo HERE.
You can see GitHub code HERE.
How was it done?
There are three main python files to build the application.
Photo by the author
1. [Link] — Build ML model and Save it.
2. [Link] — Test saved ML model.
3. [Link] — the main file to run the web app.
Here are the 7 steps to follow in order to build and deploy the ML project by
yourself.
Step 1: Create a new virtual environment using Pycharm IDE.
[Link] 2/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
Open in app Get started
Pycharm IDE, Photo by the author
Step 2: Install necessary libraries.
Here in [Link], I created all necessary libraries to install. You can
download the file and install it using:
pip install -r /path/to/[Link]
or you can install each library one by one using pip.
Step 3: Build the best machine learning model and Save it.
[Link] in this file, I created a machine learning model and saved it as a JSON file
best_model.json.
best_xgboost_model.save_model("best_model.json")
[Link] 3/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
You can see the detailed explanation of how ML models are built in my previous
Open in app Get started
article.
Step 4: Test the loaded model.
[Link] in this file, I test the loaded model and saved label-encoder classes as
a [Link] file using.
[Link]('[Link]', label_encoder.classes_)
Step 5: Create [Link] file
Step 5.1 Import libraries and dataset
import streamlit as st
import pandas as pd
from [Link] import LabelEncoder
import xgboost as xgb
import numpy as np
[Link]("Fish Weight Prediction App")
st.text_input("Enter your Name: ", key="name")
data = pd.read_csv("[Link]")
Step 5.2: Load saved label encoder classes
encoder = LabelEncoder()
encoder.classes_ = [Link]('[Link]',allow_pickle=True)
Step 5.3: Load saved the best model
# load model
best_xgboost_model = [Link]()
best_xgboost_model.load_model("best_model.json")
Step 5.4: Show dataframe on the web
[Link] 4/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
If the checkbox is checked, display training data
Open in app Get started
if [Link]('Show dataframe'):
data
Photo by the author
Step 5.5: Select Fish Species
select the name of the fish out of distinct values of all the fishes.
[Link]("Please select relevant features of your fish!")
left_column, right_column = [Link](2)
with left_column:
inp_species = [Link](
'Name of the fish:',
[Link](data['Species']))
[Link] 5/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
Open in app Get started
Photo by the author
Step 5.6: Select each value of features using a slider window
input_Length1 = [Link]('Vertical length(cm)', 0.0,
max(data["Length1"]), 1.0)
input_Length2 = [Link]('Diagonal length(cm)', 0.0,
max(data["Length2"]), 1.0)
input_Length3 = [Link]('Cross length(cm)', 0.0,
max(data["Length3"]), 1.0)
input_Height = [Link]('Height(cm)', 0.0, max(data["Height"]),
1.0)
input_Width = [Link]('Diagonal width(cm)', 0.0,
max(data["Width"]), 1.0)
[Link] 6/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
Open in app Get started
Photo by the author
Step 5.7: Make prediction button
Once a user clicks the button, the species input is transformed from text into a
relevant number, then all the input features are concatenated with a specific shape,
and model prediction is done!
if [Link]('Make Prediction'):
input_species = [Link](np.expand_dims(inp_species,
-1))
inputs = np.expand_dims(
[int(input_species), input_Length1, input_Length2,
input_Length3, input_Height, input_Width], 0)
prediction = best_xgboost_model.predict(inputs)
print("final pred", [Link](prediction, -1))
[Link](f"Your fish weight is: {[Link](prediction, -1)}
Gram")
[Link] 7/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
Open in app Get started
Photo by the author
Step 6: Upload local project to Github
You can use git to upload your local project or you can just drag and drop the folder
to an empty repository on GitHub.
Step 7: Create an account on Streamlit
The final step is to create an account and connect your GitHub repository by
clicking “From existing repo”
Streamlit, Photo by the author
Conclusion:
YES, it is free and easy. In general building, machine learning application consists
of mainly two parts. The first is building a model and the second is to deploy and
monitor it. Today we have done the deployment part using Streamlit. This simple
application just illustrates how the model is deployed and it can be a sample for
other big projects. In the following article, I will also try to deploy ML models using
another method as well.
[Link] 8/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
If you would like to learn more about applied data science here is my new YouTube
Open in app Get started
channel — AI Academy with Friends
AI Academy with Friends
Share your videos with friends, family, and the world
[Link]
I hope you enjoyed it and now can start creating beautiful apps yourself.
If you like my articles and want to see upcoming stories follow me on medium.
Gurami Keretchashvili - Medium
a Data Scientist is not the one who knows python, Sklearn,
TensorFlow, etc. But who is who knows how to play with the…
[Link]
Sign up for The Variable
By Towards Data Science
Every Thursday, the Variable delivers the very best of Towards Data Science: from hands-on tutorials and cutting-
edge research to original features you don't want to miss. Take a look.
By signing up, you will create a Medium account if you don’t already have one. Review
our Privacy Policy for more information about our privacy practices.
[Link] 9/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
Get this newsletter Get started
Open in app
About Help Terms Privacy
Get the Medium app
[Link] 10/10