Ask me anything
Ithy Logo
Sign in to save

Creating a Simple Streamlit App for Regression Analysis on Random Data

Build an interactive web app to perform and visualize linear regression effortlessly

linear regression graph

Key Takeaways

  • Interactive Data Generation: Easily customize the number of data points and noise level to see real-time effects on the regression model.
  • Comprehensive Model Evaluation: Access detailed performance metrics such as Mean Squared Error (MSE) and R² Score to assess your model's accuracy.
  • Dynamic Visualization: Visualize both training and testing data alongside the regression line for a clear understanding of model performance.

Introduction to Streamlit for Regression Analysis

Streamlit is an open-source Python library that makes it easy to create and share custom web apps for machine learning and data science. With its simple syntax and real-time interactivity, Streamlit is ideal for building applications that allow users to perform regression analysis on randomly generated data.

Prerequisites

Before diving into creating the Streamlit app, ensure you have the following installed:

  • Python 3.6 or higher
  • pip (Python package installer)

You can install the necessary Python libraries using the following command:

pip install streamlit numpy pandas scikit-learn matplotlib seaborn

Setting Up the Streamlit App

Step 1: Import Necessary Libraries

Start by importing all the required libraries. These include Streamlit for the web app framework, NumPy for numerical operations, Pandas for data manipulation, scikit-learn for regression analysis, and Matplotlib and Seaborn for visualization.

import streamlit as st
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt
import seaborn as sns

Step 2: Configure the Page

Set the configuration for the Streamlit page, including the title and layout.

st.set_page_config(
    page_title="Simple Regression Analysis App",
    layout="wide",
    initial_sidebar_state="expanded"
)

Step 3: Add Title and Description

Provide a clear title and a brief description of what the app does. This helps users understand the purpose of the application immediately.

st.title("Simple Regression Analysis")
st.write("This app performs linear regression on randomly generated data. Adjust the parameters using the sidebar to see how they affect the regression model.")

Creating Interactive Sidebar Controls

The sidebar allows users to input parameters that influence data generation and model training. This interactivity is central to exploring how different data characteristics impact regression results.

Number of Samples

Use a slider to let users select the number of data points. More data points can provide a better understanding of the underlying relationship.

n_samples = st.sidebar.slider("Number of Samples", min_value=50, max_value=1000, value=200, step=50)

Noise Level

Another slider allows users to adjust the noise level in the data. Higher noise levels make the data points more scattered, affecting the model's ability to capture the true relationship.

noise_level = st.sidebar.slider("Noise Level", min_value=0.0, max_value=5.0, value=0.5, step=0.1)

Random Seed

Including a seed ensures reproducibility. Users can set this if they wish to generate the same data across different runs.

random_seed = st.sidebar.number_input("Random Seed", min_value=0, max_value=1000, value=42, step=1)

Generating Random Data

With the user-defined parameters, generate a dataset that follows a linear relationship with added noise.

Data Generation Function

Create a function to generate the data. This function will produce input features (X) and target values (y) based on a linear model with noise.

def generate_data(n_samples, noise, seed):
    np.random.seed(seed)
    X = np.random.randn(n_samples, 1) * 2  # Random X values scaled by 2
    y = 3 * X.squeeze() + 2 + np.random.randn(n_samples) * noise  # Linear relationship with noise
    return X, y

Generating and Splitting the Data

Generate the data and split it into training and testing sets. This split is essential for evaluating the model's performance on unseen data.

X, y = generate_data(n_samples, noise_level, random_seed)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=random_seed)

Building and Training the Linear Regression Model

With the data prepared, the next step is to train a linear regression model using the training data.

Initializing and Fitting the Model

Instantiate the Linear Regression model and fit it to the training data.

model = LinearRegression()
model.fit(X_train, y_train)

Making Predictions

Use the trained model to make predictions on both the training and testing datasets. This helps in evaluating how well the model has learned the underlying relationship.

y_pred_train = model.predict(X_train)
y_pred_test = model.predict(X_test)

Displaying Model Coefficients

Show the intercept and slope (coefficient) of the regression line. These values define the linear relationship captured by the model.

st.subheader("Model Parameters")
st.write(f"<b>Intercept:</b> {model.intercept_:.4f}")
st.write(f"<b>Slope (Coefficient):</b> {model.coef_[0]:.4f}")

Evaluating Model Performance

Assess the model's accuracy using common metrics such as Mean Squared Error (MSE) and R² Score. These metrics provide insights into the model's predictive capabilities.

Calculating Performance Metrics

Compute MSE and R² for both training and testing datasets.

# Performance on Training Data
mse_train = mean_squared_error(y_train, y_pred_train)
r2_train = r2_score(y_train, y_pred_train)

# Performance on Testing Data
mse_test = mean_squared_error(y_test, y_pred_test)
r2_test = r2_score(y_test, y_pred_test)

Displaying the Metrics

Present the calculated metrics in a clear and organized manner.

st.subheader("Model Performance Metrics")
performance_data = {
    "Dataset": ["Training", "Testing"],
    "Mean Squared Error (MSE)": [f"{mse_train:.4f}", f"{mse_test:.4f}"],
    "R² Score": [f"{r2_train:.4f}", f"{r2_test:.4f}"]
}
performance_df = pd.DataFrame(performance_data)
st.table(performance_df)

Performance Metrics Table

Dataset Mean Squared Error (MSE) R² Score
Training 0.1234 0.9876
Testing 0.4567 0.8765

Visualizing the Regression Results

Visualization is a powerful tool to understand how well the regression model fits the data. We'll create scatter plots for both training and testing data, overlayed with the regression line.

Scatter Plot with Regression Line

Create a scatter plot that displays the actual data points and the predicted regression line. Different colors represent training and testing data for clarity.

fig, ax = plt.subplots(figsize=(10, 6))
# Scatter plot for Training Data
ax.scatter(X_train, y_train, color='blue', alpha=0.5, label='Training Data')
# Scatter plot for Testing Data
ax.scatter(X_test, y_test, color='green', alpha=0.5, label='Testing Data')
# Regression Line
X_all = np.vstack((X_train, X_test))
y_all_pred = model.predict(X_all)
ax.plot(X_all, y_all_pred, color='red', linewidth=2, label='Regression Line')
ax.set_xlabel('X')
ax.set_ylabel('y')
ax.set_title('Linear Regression Results')
ax.legend()
ax.grid(True)
st.pyplot(fig)

Enhanced Visualization with Seaborn

For more advanced visualizations, Seaborn can be utilized to create aesthetically pleasing plots that provide deeper insights.

# Seaborn regression plot
fig, ax = plt.subplots(figsize=(10, 6))
sns.regplot(x=X_train.squeeze(), y=y_train, scatter=True, color='blue', label='Training Data', ax=ax)
sns.regplot(x=X_test.squeeze(), y=y_test, scatter=True, color='green', label='Testing Data', ax=ax)
ax.set_xlabel('X')
ax.set_ylabel('y')
ax.set_title('Linear Regression with Seaborn')
ax.legend()
ax.grid(True)
st.pyplot(fig)

Sample Results Display

Provide a snapshot of the actual and predicted values to give users a tangible sense of the model's performance.

Creating a Results DataFrame

Combine the input features, actual target values, and predicted values into a single DataFrame for easy viewing.

results_df = pd.DataFrame({
    'X': X.squeeze(),
    'y_actual': y,
    'y_predicted': model.predict(X)
})
st.subheader("Sample Results")
st.dataframe(results_df.head())

Sample Results Table

X y_actual y_predicted
-0.1234 1.2345 1.5678
2.3456 8.7654 8.9932
-1.2345 0.5678 0.4321
3.4567 13.8901 13.6789
0.5678 4.5678 4.7890

Additional Features and Enhancements

To further enrich the app, consider incorporating additional features that provide deeper insights or enhance user experience.

Downloadable Data

Allow users to download the generated dataset along with the model's predictions. This can be useful for offline analysis or reporting.

def convert_df(df):
    return df.to_csv(index=False).encode('utf-8')

csv = convert_df(results_df)
st.download_button(
    label="Download data as CSV",
    data=csv,
    file_name='regression_results.csv',
    mime='text/csv',
)

Equation of the Regression Line

Display the mathematical equation of the regression line using MathJax for better readability and understanding.

slope = model.coef_[0]
intercept = model.intercept_

st.markdown(r"""
### Regression Line Equation
$$
y = {:.4f}x + {:.4f}
$$
""".format(slope, intercept), unsafe_allow_html=True)

Interactive Parameter Adjustments

Enhance interactivity by allowing users to adjust additional parameters, such as the proportion of the test dataset or the color schemes of the plots.

test_size = st.sidebar.slider("Test Size (%)", min_value=10, max_value=50, value=20, step=5)
plt_color_train = st.sidebar.color_picker("Training Data Color", "#0000FF")
plt_color_test = st.sidebar.color_picker("Testing Data Color", "#00FF00")
plt_color_line = st.sidebar.color_picker("Regression Line Color", "#FF0000")
# Update train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size/100, random_state=random_seed)
# Update plotting colors in visualization sections accordingly

Conclusion

This comprehensive guide walked you through creating an interactive Streamlit app for performing linear regression analysis on randomly generated data. By adjusting parameters such as the number of samples and noise levels, users can visualize how these factors influence the regression model's performance. The app not only displays critical metrics but also provides dynamic visualizations to facilitate a deeper understanding of linear relationships in data.

References


Last updated January 18, 2025

AI-assisted research. Sources, methodology and corrections · Source-checked guides and tools · Explore published answers

Ask Ithy AI