Streamlit Tutorial with Machine Learning Project

Streamlit is a powerful open-source framework that allows you to create web applications for data science and machine learning with just a few lines of Python code.

It is simple, intuitive, and requires no frontend experience, making it a great tool for both beginners and experienced developers who want to quickly deploy machine learning models.

In this blog, I’ll guide you through a step-by-step process to build a basic Streamlit app and a machine learning project using the Iris dataset with a RandomForestClassifier.

Getting Started with Streamlit

Before we jump into the project, let’s walk through some basic Streamlit functionality to get comfortable with the framework. You can install Streamlit using the following command:

pip install streamlit
pip install streamlit
pip install streamlit

Enter fullscreen mode Exit fullscreen mode

Once installed, you can start your first Streamlit app by creating a Python file, say app.py, and running it using:

streamlit run app.py
streamlit run app.py
streamlit run app.py

Enter fullscreen mode Exit fullscreen mode

Now, let’s explore the core features of Streamlit:

1. Writing Titles and Displaying Text

import streamlit as st
# Writing a title
st.title("Hello World")
# Display simple text
st.write("Displaying a simple text")
import streamlit as st

# Writing a title
st.title("Hello World")

# Display simple text
st.write("Displaying a simple text")
import streamlit as st # Writing a title st.title("Hello World") # Display simple text st.write("Displaying a simple text")

Enter fullscreen mode Exit fullscreen mode

2. Displaying DataFrames

import pandas as pd
# Creating a DataFrame
df = pd.DataFrame({
"first column": [1, 2, 3, 4],
"second column": [5, 6, 7, 8]
})
# Display the DataFrame
st.write("Displaying a DataFrame")
st.write(df)
import pandas as pd

# Creating a DataFrame
df = pd.DataFrame({
    "first column": [1, 2, 3, 4],
    "second column": [5, 6, 7, 8]
})

# Display the DataFrame
st.write("Displaying a DataFrame")
st.write(df)
import pandas as pd # Creating a DataFrame df = pd.DataFrame({ "first column": [1, 2, 3, 4], "second column": [5, 6, 7, 8] }) # Display the DataFrame st.write("Displaying a DataFrame") st.write(df)

Enter fullscreen mode Exit fullscreen mode

3. Visualizing Data with Charts

import numpy as np
# Generating random data
chart_data = pd.DataFrame(
np.random.randn(20, 4), columns=['a', 'b', 'c', 'd']
)
# Display the line chart
st.line_chart(chart_data)
import numpy as np

# Generating random data
chart_data = pd.DataFrame(
    np.random.randn(20, 4), columns=['a', 'b', 'c', 'd']
)

# Display the line chart
st.line_chart(chart_data)
import numpy as np # Generating random data chart_data = pd.DataFrame( np.random.randn(20, 4), columns=['a', 'b', 'c', 'd'] ) # Display the line chart st.line_chart(chart_data)

Enter fullscreen mode Exit fullscreen mode

4. User Interaction: Text Input, Sliders, and Select Boxes
Streamlit enables interactive widgets like text inputs, sliders, and select boxes that update dynamically based on user input.

# Text input
name = st.text_input("Your Name Is:")
if name:
st.write(f'Hello, {name}')
# Slider
age = st.slider("Select Your Age:", 0, 100, 25)
if age:
st.write(f'Your Age Is: {age}')
# Select Box
choices = ["Python", "Java", "Javascript"]
lang = st.selectbox('Favorite Programming Language', choices)
if lang:
st.write(f'Favorite Programming Language is {lang}')
# Text input
name = st.text_input("Your Name Is:")
if name:
    st.write(f'Hello, {name}')

# Slider
age = st.slider("Select Your Age:", 0, 100, 25)
if age:
    st.write(f'Your Age Is: {age}')

# Select Box
choices = ["Python", "Java", "Javascript"]
lang = st.selectbox('Favorite Programming Language', choices)
if lang:
    st.write(f'Favorite Programming Language is {lang}')
# Text input name = st.text_input("Your Name Is:") if name: st.write(f'Hello, {name}') # Slider age = st.slider("Select Your Age:", 0, 100, 25) if age: st.write(f'Your Age Is: {age}') # Select Box choices = ["Python", "Java", "Javascript"] lang = st.selectbox('Favorite Programming Language', choices) if lang: st.write(f'Favorite Programming Language is {lang}')

Enter fullscreen mode Exit fullscreen mode

5. File Upload
You can allow users to upload files and display their contents dynamically in your Streamlit app:

# File uploader for CSV files
file = st.file_uploader('Choose a CSV file', 'csv')
if file:
data = pd.read_csv(file)
st.write(data)
# File uploader for CSV files
file = st.file_uploader('Choose a CSV file', 'csv')

if file:
    data = pd.read_csv(file)
    st.write(data)
# File uploader for CSV files file = st.file_uploader('Choose a CSV file', 'csv') if file: data = pd.read_csv(file) st.write(data)

Enter fullscreen mode Exit fullscreen mode

Building a Machine Learning Project with Streamlit

Now that you’re familiar with the basics, let’s dive into creating a machine learning project. We will use the famous Iris dataset and build a simple classification model using RandomForestClassifier from scikit-learn.

Project Structure :

  • Load the dataset.
  • Train a RandomForestClassifier.
  • Allow users to input features using sliders.
  • Predict the species based on the input features.

1. Install necessary dependencies
First, let’s install the necessary libraries:

pip install streamlit scikit-learn numpy pandas
pip install streamlit scikit-learn numpy pandas
pip install streamlit scikit-learn numpy pandas

Enter fullscreen mode Exit fullscreen mode

2. Import Libraries and Load Data
Let’s import the necessary libraries and load the Iris dataset:

import streamlit as st
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
# Cache data for efficient loading
@st.cache_data
def load_data():
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df["species"] = iris.target
return df, iris.target_names
df, target_name = load_data()
import streamlit as st
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier

# Cache data for efficient loading
@st.cache_data
def load_data():
    iris = load_iris()
    df = pd.DataFrame(iris.data, columns=iris.feature_names)
    df["species"] = iris.target
    return df, iris.target_names

df, target_name = load_data()
import streamlit as st import pandas as pd from sklearn.datasets import load_iris from sklearn.ensemble import RandomForestClassifier # Cache data for efficient loading @st.cache_data def load_data(): iris = load_iris() df = pd.DataFrame(iris.data, columns=iris.feature_names) df["species"] = iris.target return df, iris.target_names df, target_name = load_data()

Enter fullscreen mode Exit fullscreen mode

3. Train the Machine Learning Model
Once we have the data, we will train a RandomForestClassifier to predict the species of a flower based on its features:

# Train RandomForestClassifier
model = RandomForestClassifier()
model.fit(df.iloc[:, :-1], df["species"])
# Train RandomForestClassifier
model = RandomForestClassifier()
model.fit(df.iloc[:, :-1], df["species"])
# Train RandomForestClassifier model = RandomForestClassifier() model.fit(df.iloc[:, :-1], df["species"])

Enter fullscreen mode Exit fullscreen mode

4. Creating the Input Interface
Now, we’ll create sliders in the sidebar to allow users to input features for making predictions:

# Sidebar for user input
st.sidebar.title("Input Features")
sepal_length = st.sidebar.slider("Sepal length", float(df['sepal length (cm)'].min()), float(df['sepal length (cm)'].max()))
sepal_width = st.sidebar.slider("Sepal width", float(df['sepal width (cm)'].min()), float(df['sepal width (cm)'].max()))
petal_length = st.sidebar.slider("Petal length", float(df['petal length (cm)'].min()), float(df['petal length (cm)'].max()))
petal_width = st.sidebar.slider("Petal width", float(df['petal width (cm)'].min()), float(df['petal width (cm)'].max()))
# Sidebar for user input
st.sidebar.title("Input Features")
sepal_length = st.sidebar.slider("Sepal length", float(df['sepal length (cm)'].min()), float(df['sepal length (cm)'].max()))
sepal_width = st.sidebar.slider("Sepal width", float(df['sepal width (cm)'].min()), float(df['sepal width (cm)'].max()))
petal_length = st.sidebar.slider("Petal length", float(df['petal length (cm)'].min()), float(df['petal length (cm)'].max()))
petal_width = st.sidebar.slider("Petal width", float(df['petal width (cm)'].min()), float(df['petal width (cm)'].max()))
# Sidebar for user input st.sidebar.title("Input Features") sepal_length = st.sidebar.slider("Sepal length", float(df['sepal length (cm)'].min()), float(df['sepal length (cm)'].max())) sepal_width = st.sidebar.slider("Sepal width", float(df['sepal width (cm)'].min()), float(df['sepal width (cm)'].max())) petal_length = st.sidebar.slider("Petal length", float(df['petal length (cm)'].min()), float(df['petal length (cm)'].max())) petal_width = st.sidebar.slider("Petal width", float(df['petal width (cm)'].min()), float(df['petal width (cm)'].max()))

Enter fullscreen mode Exit fullscreen mode

5. Predicting the Species
After getting the user inputs, we will make a prediction using the trained model:

# Prepare the input data
input_data = [[sepal_length, sepal_width, petal_length, petal_width]]
# Prediction
prediction = model.predict(input_data)
prediction_species = target_name[prediction[0]]
# Display the prediction
st.write("Prediction:")
st.write(f'Predicted species is {prediction_species}')
# Prepare the input data
input_data = [[sepal_length, sepal_width, petal_length, petal_width]]

# Prediction
prediction = model.predict(input_data)
prediction_species = target_name[prediction[0]]

# Display the prediction
st.write("Prediction:")
st.write(f'Predicted species is {prediction_species}')
# Prepare the input data input_data = [[sepal_length, sepal_width, petal_length, petal_width]] # Prediction prediction = model.predict(input_data) prediction_species = target_name[prediction[0]] # Display the prediction st.write("Prediction:") st.write(f'Predicted species is {prediction_species}')

Enter fullscreen mode Exit fullscreen mode

This will looks like:

Github Link: https://github.com/Jagroop2001/streamlit-tutorial

Finally,Streamlit makes it incredibly easy to create and deploy machine learning web interface with minimal effort. In just a few lines of code, we built an interactive app that allows users to input features and predict the species of a flower using a machine learning model. 🤖

Happy coding!

原文链接:Streamlit Tutorial with Machine Learning Project

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
Nothing in the world is difficult if you put your heart into it.
世上无难事,只怕有心人
评论 抢沙发

请登录后发表评论

    暂无评论内容