import streamlit as st import pandas as pd import numpy as np from sklearn.linear_model import LinearRegression import joblib # App Title st.title("Utrecht Pollution Prediction") # Load the trained model @st.cache(allow_output_mutation=True) def load_model(): try: # Try loading a pre-trained model model = joblib.load("path_to_your_model/linear_regression_model.pkl") except: # If the model is not available, train a simple Linear Regression model as a fallback st.write("No pre-trained model found. Training a new Linear Regression model...") # Fallback - Generate some random training data for demonstration purposes # In reality, replace this with your actual data np.random.seed(0) X_train = np.random.rand(100, 3) # 100 samples, 3 features y_train = 3*X_train[:, 0] + 2*X_train[:, 1] + X_train[:, 2] # Example: linear relationship # Train a linear regression model model = LinearRegression() model.fit(X_train, y_train) # Optionally, save the trained model to use later joblib.dump(model, "linear_regression_model.pkl") return model model = load_model() # Explain the app st.write(""" ### Predict Pollution Levels in Utrecht This app allows you to input environmental features to predict pollution levels using a simple Linear Regression model. """) # Input features needed for your model def get_user_input(): feature_1 = st.number_input('Temperature (°C)', min_value=-10.0, max_value=40.0, value=20.0) feature_2 = st.number_input('Wind Speed (km/h)', min_value=0.0, max_value=100.0, value=10.0) feature_3 = st.number_input('Humidity (%)', min_value=0.0, max_value=100.0, value=50.0) # Create a DataFrame with user inputs input_data = {'Temperature': feature_1, 'Wind Speed': feature_2, 'Humidity': feature_3} features = pd.DataFrame([input_data]) return features # Get user input input_df = get_user_input() # Display user input st.subheader('User Input:') st.write(input_df) # Make predictions using the linear regression model prediction = model.predict(input_df) # Display the prediction st.subheader('Prediction:') st.write(f'Predicted Pollution Level: {prediction[0]:.2f}')