File size: 2,984 Bytes
a9989d5
 
 
 
 
 
 
 
 
 
 
 
 
6f3f6de
a9989d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb03930
 
a9989d5
eb03930
 
 
 
a9989d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24398f9
a9989d5
 
 
 
 
 
 
eb03930
63b3322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import numpy as np

# Load sample data (replace with real pollution dataset)
def load_sample_data():
    data = {
        "Date": pd.date_range(start="2023-01-01", periods=100, freq="D"),
        "Location": np.random.choice(["Karachi", "Lahore", "Islamabad", "Peshawar", "Quetta"], size=100),
        "AQI": np.random.randint(50, 200, size=100),  # Random AQI values
        "Temperature": np.random.uniform(20, 35, size=100),
        "Humidity": np.random.uniform(30, 80, size=100),
    }
    return pd.DataFrame(data)

# Train a simple model to predict AQI
def train_model(data):
    X = data[["Temperature", "Humidity"]]
    y = data["AQI"]
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

    model = RandomForestRegressor(n_estimators=100, random_state=42)
    model.fit(X_train, y_train)

    y_pred = model.predict(X_test)
    mse = mean_squared_error(y_test, y_pred)
    return model, mse

# Predict AQI for a given input
def predict_aqi(model, temperature, humidity):
    prediction = model.predict([[temperature, humidity]])
    return round(prediction[0], 2)

# Visualization of historical trends
def plot_trends(data, location):
    filtered_data = data[data["Location"] == location]
    plt.figure(figsize=(10, 6))
    sns.lineplot(data=filtered_data, x="Date", y="AQI", label="AQI")
    sns.lineplot(data=filtered_data, x="Date", y="Temperature", label="Temperature")
    sns.lineplot(data=filtered_data, x="Date", y="Humidity", label="Humidity")
    plt.title(f"Historical Data Trends for {location}")
    plt.xlabel("Date")
    plt.ylabel("Values")
    plt.legend()
    plt.grid()
    plt.tight_layout()

    # Save the plot to a file
    plt.savefig("trends.png")
    return "trends.png"

# Load data and train model
data = load_sample_data()
model, mse = train_model(data)

# Streamlit app
st.title("🌍 Pollution Data Analysis Tool")
st.markdown(
    "This app predicts air pollution levels (AQI) based on temperature and humidity. "
    "It also provides a visualization of historical trends."
)

# Sidebar inputs
st.sidebar.header("Input Parameters")
location = st.sidebar.selectbox("Select Location", data["Location"].unique())
temperature = st.sidebar.slider("Temperature (°C)", 20, 40, 25)
humidity = st.sidebar.slider("Humidity (%)", 30, 90, 50)

# Prediction
st.subheader("Predicted AQI")
prediction = predict_aqi(model, temperature, humidity)
st.write(f"The predicted AQI for {location} is: {prediction}")

# Historical trends visualization
st.subheader("Historical Data Trends")
trends_image = plot_trends(data, location)
st.image(trends_image)

# Model performance
st.sidebar.subheader("Model Performance")
st.sidebar.write(f"Mean Squared Error: {mse:.2f}")