build machine learning model using streamit

Build Machine Learning Model Using Streamlit

Streamlit Python Tutorial: Build Machine Learning Model

A Machine Learning model is undone if it just remains in Jupyter Notebook. Streamlit provides a faster way to build and share data web apps. It turns Jupyter Notebook Python Scripts into a great-looking Web App. And still, it just takes a few minutes. The only pre-requisites to build machine learning model using Streamlit is Python Programming. And we got you covered.

Check the detailed Anime Vyuh Python Tutorials Series, Python made easy.

Why Streamlit?

Streamlit makes the frontend looks simple. It is compatible with basically everything, that includes Tensorflow, Keras, PyTorch, OpenCV, and so on. Building Machine Learning models is a great way to analyze and visualize your Python scripts and later it is even shareable. Streamlit even helps to deploy your web app with very simple steps to follow. We shall see that in the next article.

Streamlit vs Flask vs Django

Flask and Django are widely used Python Web Development Frameworks. Even though these two are easy to program, Streamlit is even much easier. Streamlit doesn’t require HTML, CSS code snippets, define routes, and handle HTTP requests. It has got everything covered for you. You just need to use the in-built widget to build an end-end Web Application.

Streamlit’s open-source app framework is a cakewalk to get started with:

pip install streamlit

Caution: Before proceeding with the working of the Streamlit web app, it is recommended to create a Virtual Environment. You can check this article on👉 how to create a Virtual Environment for Python programs.

Streamlit Widgets

Widgets make our tasks easier to build machine learning using Streamlit. Streamlit provides a wide range of widgets and using this widget is just easier as naming variables. I mean it when I say it is super easy. You can build Streamlit Web App in a few minutes.

The first step imports the necessary modules.

import streamlit as st
import pandas as pd
import seaborn as sb
import matplotlib.pyplot as plt
import plotly.express as px

title

Just like HTML tags, here the title is the text with the h1 tag.

st.title("Predict Medical Charges")

Assuming you have saved your Python file name as app.py, to run the streamlit program run the following command:

streamlit run app.py

header

The header is h2 text used to provide subheadings in the web app.

Syntax:
st.header(message)
st.header("Machine Learning: Regression")

write

write method is used to add normal paragraph text.

Syntax: 
st.write("para")
st.write("Provided Medical Charges dataset, we shall predict price using Regression ML Algorithms")

markdown

A Markdown is a text-to-HTML conversion tool for web writers. It allows you to write using an easy-to-read, easy-to-write plain text format, then converts it to structurally valid XHTML (or HTML). The files written in markdown are saved with the .md extension.

Basic Syntax of Markdown:

#H1 or st.title()
## H2 or st.header()
### H3
#### H4
##### H5
###### H6
**bold**
_italic_
[text](link)

Learn more about Markdown: Markdown Tutorial

Syntax to add Hyperlink in Markdown:

st.sidebar.markdown("[Anime Vyuh](https://animevyuh.org/)")

st.sidebar.markdown("[Check Tutorial](https://animevyuh.org/build-machine-learning-model-using-streamlit/)")

Analyze the Data

DataFrame is a 2-dimensional representation of a dataset. A DataFrame representation looks like rows and columns of the excel sheet.

Syntax: 
st.dataframe(pd.DataFrame(data))

You can download the dataset: Medical Charges Kaggle

data = pd.read_csv('medical.csv')
st.write("100 Sample Data:")
st.dataframe(data.sample(100))

By using describe command you can check the overview description of the dataset.

st.markdown("### Description Of Data")
st.write(data.describe())

Next check if your data has empty data values.

st.markdown("### Check for Empty Data")
st.write(data.isnull().sum())

Visualize The Data

How To Plot Graphs On Streamlit

A Machine Learning model is incomplete without visualization of data. Python supports Matplotlib, Seaborn, and Plotly modules to deal with Data Visualization.

To plot Plotly graph on Streamlit Web app:

fig = px.histogram(data,marginal='box',x="age",color="smoker")
fig.update_layout(bargap=0.3)
st.plotly_chart(fig)

In the above code, we have created a histogram using Plotly, the first argument marginal is again used to plot the boxplot above the histogram.

Next, you can now plot seaborn plot on the Streamlit web app. Heatmap is used to plot the correlation of the numeric value. Correlation is a technique that is used to check the relationship between two or more variables.

fig,axes = plt.subplots()
sb.heatmap(data.corr(),annot=True)
st.pyplot(fig)

Last but not least Matplotlib can also be implemented on Streamlit web app.

fig2,axes2 = plt.subplots()
plt.pie(data['region'].value_counts(),labels=['southwest','southeast','northwest','northeast'],autopct="%0.2f%%",colors=["purple","blue",'green','red'],explode=[0.01,0.02,0,0])

st.pyplot(fig2)

Prediction Of The Model

First let us import the necessary modules required to predict the Machine Learning model. AdaBoostRegressor is the Machine Learning algorithm used for predicting regression based models.

from sklearn.ensemble import AdaBoostRegressor
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_absolute_error,mean_squared_error

Data Preprocessing

Data Preprocessing is a technique where we scale, and encode the numeric and category data(features). Numeric data vary in large range, this range is scaled in between 0 to 1. Categorial data are basically object data types, using LabelEncoder we shall encode the categorial data into numeric data.

category = list()
numerical = list()
for features in x.columns:
    if x[features].dtype == 'O':
        category.append(features)
    else:
        numerical.append(features)

scale = MinMaxScaler()
x[numerical] = scale.fit_transform(x[numerical])

encode = LabelEncoder()
x[category] = encode.fit_transform(x[category])
x = x[numerical+category]
y = data['charges']

st.markdown("### After scaling, encoding and Feature Selection")
st.markdown("#### Features")
st.dataframe(x)

st.markdown("#### Label")
st.dataframe(y)

Caution: This article is just use to build Machine Learning Model Using Streamlit. You can optimize the model using Hyperparameter Tuning and Cross Validation. In this article we shall not look into it.

Verifying Metrics Of The Model

Metrics is a technique is used to evaluate the performance of ML algorithms. Usually we use error metrics for Regression models and accuracy score, recall and precision for classification for Classification models.

Slider Widget

Learning Rate is a hyperparameter, which means we need to observe the performance of the model by varying it. Thus we use the slider widget to check model performance for different learning rate.

Syntax for Slider widget:
st.slider("text",min_value=<numeric>,max_value=<numeric>,step=<numeric>) 
#min_value<max_value
st.markdown("#### Change Learning Rate And Notice the Error And Score")
lr = st.slider("Learning rate",min_value=0.03,max_value=0.1,step=0.01)

Once you initialize the model it is time to fit and predict our model.

x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=42)
model = AdaBoostRegressor(n_estimators=1000,random_state=42,learning_rate=lr)
model = model.fit(x_train,y_train)
predict = model.predict(x_test)

We use mean absolute error and root mean squared error metrics to check the performance of our model.

st.write(f"MAE: {mean_absolute_error(predict,y_test)}")
st.write(f"RMSE: {mean_squared_error(predict,y_test,squared=False)}")
st.write(f"Score:{model.score(x_train,y_train)*100}%")

Streamlit works on localhost:8051 once you run this command: streamlit run app.py. Whenever the changes are made make sure to hit ctrl+r to re-run the web application.

Conclusion

Streamlit is the best way to analyze and visualize the Machine Learning model. The main advantage is you can share your model, instead of running 100 lines of scripts.

Let us get to the subsequent tier and deploy this Machine Learning model on Heroku. Check the next article to deploy Streamlit App To Heroku.

Newsletter: https://animevyuh.org/newsletter/

If you like our content support us: Buy Me A CoffeeKo-Fi

1 thought on “Build Machine Learning Model Using Streamlit”

  1. Pingback: Deploy Streamlit App To Heroku -

Comments are closed.