Streamlit is a fast, lightweight Python framework used to create interactive web apps for data science and machine learning. It enables quick deployment of Scikit-learn models with a simple UI for real-time predictions.
Key Characteristics
- No HTML/CSS/JavaScript knowledge required
- Rapid prototyping of ML interfaces
- Simple Python scripts render into interactive apps
- Integrated with Scikit-learn, NumPy, Pandas, and Matplotlib
Basic Rules
- Save your model using
joblib
orpickle
- Use Streamlit widgets (
st.text_input
,st.slider
, etc.) for user input - Load and predict with your Scikit-learn model inside the app
- Run app with
streamlit run app.py
Syntax Table
SL NO | Task | Syntax Example | Description |
---|---|---|---|
1 | Import Streamlit | import streamlit as st |
Loads Streamlit library |
2 | Load Model | model = joblib.load('model.pkl') |
Load pre-trained model |
3 | User Input | st.text_input("Enter value") |
Creates input field |
4 | Predict Result | model.predict([inputs]) |
Predict with model |
5 | Show Output | st.write(f"Prediction: {result}") |
Displays prediction result |
Syntax Explanation
1. Import Streamlit
What is it?
Loads the Streamlit module to build the UI.
Syntax:
import streamlit as st
Explanation:
- Required to access all Streamlit components
- Import once at the top of the script
2. Load Model
What is it?
Imports a pre-trained Scikit-learn model for use.
Syntax:
from joblib import load
model = load('model.pkl')
Explanation:
- Load your model once globally to avoid reloading on each input
- Make sure to keep the
.pkl
file in the same folder or provide a valid path
3. User Input
What is it?
Widgets for taking user input in the Streamlit app.
Syntax:
val = st.text_input("Enter feature value")
Explanation:
- Creates a text box in the UI
- Accepts numeric or string input depending on use case
- Can be extended with
st.slider
,st.selectbox
, etc.
4. Predict Result
What is it?
Generates prediction using Scikit-learn model.
Syntax:
prediction = model.predict([[val1, val2, val3]])
Explanation:
- Input must be reshaped into a 2D array (list of lists)
- Convert all text input to appropriate data type (float/int)
- Can wrap in
try/except
for error handling
5. Show Output
What is it?
Displays prediction result in the Streamlit interface.
Syntax:
st.write("Prediction:", prediction[0])
Explanation:
st.write()
outputs text, numbers, tables, etc.- Used to display dynamic results in app
Real-Life Project: Iris Species Predictor
Project Overview
Build an app that predicts Iris species based on 4 input features using a trained classifier.
Code Example
import streamlit as st
from joblib import load
import numpy as np
# Load model
model = load('iris_model.pkl')
# Title
st.title("Iris Flower Species Predictor")
# Inputs
sepal_length = st.number_input("Sepal Length")
sepal_width = st.number_input("Sepal Width")
petal_length = st.number_input("Petal Length")
petal_width = st.number_input("Petal Width")
# Prediction
if st.button("Predict"):
inputs = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
result = model.predict(inputs)
st.write(f"Prediction: {result[0]}")
Expected Output
- A simple interactive web UI
- User inputs feature values and receives model predictions instantly
Common Mistakes to Avoid
- β Not converting string inputs to float
- β Model not in same directory or incorrect path
- β Forgetting to format input into 2D array for
.predict()
Further Reading Recommendation
π Hands-On Python and Scikit-Learn: A Practical Guide to Machine Learning by Sarful Hassan
π Available on Amazon