Streamlit: An Easy Framework for Data Science and Machine Learning

Streamlit: An Easy Framework for Data Science and Machine Learning

Hello everyone, I am Little Rabbit learning Python.

Today I want to introduce you to a super cool “tool”—Streamlit. It’s like a universal key, specially designed to open the door to easily building applications, making it unprecedentedly simple to implement data science projects!

Imagine this: you have painstakingly developed an amazing machine learning model that can accurately predict stock trends or intelligently recognize various objects in images. But if you can’t intuitively showcase this model to others and allow them to easily use it, that would be a real shame, like building a supercar that can only sit in the garage being admired alone. This is when Streamlit shines!

For example, you have a data analysis project studying the trends in urban air quality changes. You’ve already obtained some valuable conclusions through various complex data processing and modeling, such as the fluctuations in air quality index across different seasons and regions, and the relationship between pollutant concentration and meteorological conditions. In the past, presenting these results to non-technical personnel, such as decision-makers in government departments or volunteers in environmental organizations, was a huge challenge. You had to painstakingly write reports and create PPTs, and even then, they might not fully understand.

But with Streamlit, everything is different! Its usage is super simple, just like building with blocks. First, import the Streamlit library:

import streamlit as st

Next, you can easily create a web application interface with it. Want a clear and concise title to showcase your project name? No problem:

st.title("Urban Air Quality Insights")

Then, display the key charts obtained from your data analysis. Suppose you used matplotlib or seaborn to plot a line chart showing air quality changes by season, and a bar chart comparing pollutant concentrations across different regions. With Streamlit, you can embed these charts into the application with just a few lines of code:

import matplotlib.pyplot as plt
import seaborn as sns
# Assuming here we already have the chart objects fig1 (line chart of air quality changes by season) and fig2 (bar chart comparing pollutant concentrations)
st.pyplot(fig1)
st.pyplot(fig2)

This is like hanging your carefully crafted artwork in the most prominent position of the exhibition hall, allowing the audience to see the highlights at a glance. Moreover, it’s not just charts; you can also add textual explanations using simple and understandable language to clarify the meaning behind the data:

st.write("From the above charts, it is clear that air quality is relatively better in summer, while in winter, due to heating and other factors, pollutant concentrations significantly increase. In different regions, pollutant concentrations in industrial areas are generally higher than in residential areas.")

If you have a machine learning model and want users to input some data to get real-time predictions from the model, Streamlit can easily handle that too. For example, if you have a house price prediction model, users can input information like house area, age, and surrounding facilities to instantly get an estimated price. Here’s a sample code:

# Assuming the trained house price prediction model model has been loaded, and the feature column names list feature_cols
area = st.number_input("House Area (sq meters)")
age = st.number_input("House Age (years)")
facilities = st.selectbox("Surrounding Facilities", ["Complete", "Average", "Poor"])
# Organizing the user input data into a format acceptable to the model
input_data = [[area, age, facilities == "Complete", facilities == "Average"]]
# Making predictions
prediction = model.predict(input_data)
st.write(f"Predicted House Price: {prediction[0]} Ten Thousand Yuan")

In this process, users input data on a clean and friendly interface, click a button (which actually just triggers the execution of the prediction code), and instantly get results, just like having a personal smart real estate consultant.

The greatest charm of Streamlit lies in its ability to greatly simplify the transformation process from data science projects to interactive applications, allowing non-technical personnel to easily use the tools you developed. It packages and solves a series of complex problems such as front-end development and back-end interactions, allowing you to focus solely on your data and models to create powerful and user-friendly applications.

Leave a Comment