|
import streamlit as st |
|
import pandas as pd |
|
import numpy as np |
|
from sklearn.linear_model import LinearRegression |
|
import joblib |
|
|
|
|
|
st.title("Utrecht Pollution Prediction") |
|
|
|
|
|
@st.cache(allow_output_mutation=True) |
|
def load_model(): |
|
try: |
|
|
|
model = joblib.load("path_to_your_model/linear_regression_model.pkl") |
|
except: |
|
|
|
st.write("No pre-trained model found. Training a new Linear Regression model...") |
|
|
|
|
|
|
|
np.random.seed(0) |
|
X_train = np.random.rand(100, 3) |
|
y_train = 3*X_train[:, 0] + 2*X_train[:, 1] + X_train[:, 2] |
|
|
|
|
|
model = LinearRegression() |
|
model.fit(X_train, y_train) |
|
|
|
|
|
joblib.dump(model, "linear_regression_model.pkl") |
|
|
|
return model |
|
|
|
model = load_model() |
|
|
|
|
|
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. |
|
""") |
|
|
|
|
|
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) |
|
|
|
|
|
input_data = {'Temperature': feature_1, |
|
'Wind Speed': feature_2, |
|
'Humidity': feature_3} |
|
|
|
features = pd.DataFrame([input_data]) |
|
return features |
|
|
|
|
|
input_df = get_user_input() |
|
|
|
|
|
st.subheader('User Input:') |
|
st.write(input_df) |
|
|
|
|
|
prediction = model.predict(input_df) |
|
|
|
|
|
st.subheader('Prediction:') |
|
st.write(f'Predicted Pollution Level: {prediction[0]:.2f}') |