File size: 2,324 Bytes
8497557 675bb54 8497557 675bb54 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
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}') |