How To Deploy Machine Learning Models - by Gurami Keretchashvili - Towards Data Science
How To Deploy Machine Learning Models - by Gurami Keretchashvili - Towards Data Science
Save
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
https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 1/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
∘ Introduction
∘ Fish Weight Prediction Application
∘ How was it done?
∘ Conclusion:
Here are the 7 steps to follow in order to build and deploy the ML project by
yourself.
https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 2/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
Step 3: Build the best machine learning model and Save it.
model.py 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")
https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 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.
prediction.py in this file, I test the loaded model and saved label-encoder classes as
a classes.npy file using.
np.save('classes.npy', label_encoder.classes_)
import streamlit as st
import pandas as pd
from sklearn.preprocessing import LabelEncoder
import xgboost as xgb
import numpy as np
encoder = LabelEncoder()
encoder.classes_ = np.load('classes.npy',allow_pickle=True)
# load model
best_xgboost_model = xgb.XGBRegressor()
best_xgboost_model.load_model("best_model.json")
https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 4/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
if st.checkbox('Show dataframe'):
data
select the name of the fish out of distinct values of all the fishes.
https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 5/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 6/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
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 st.button('Make Prediction'):
input_species = encoder.transform(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", np.squeeze(prediction, -1))
st.write(f"Your fish weight is: {np.squeeze(prediction, -1)}
Gram")
https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 7/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
You can use git to upload your local project or you can just drag and drop the folder
to an empty repository on GitHub.
The final step is to create an account and connect your GitHub repository by
clicking “From existing repo”
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.
https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 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
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.
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.
https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 9/10
16/10/2022, 13:20 How to Deploy Machine Learning Models | by Gurami Keretchashvili | Towards Data Science
https://towardsdatascience.com/how-to-deploy-machine-learning-models-601f8c13ff45 10/10