File size: 4,290 Bytes
e3552a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import subprocess
import numpy as np
import pandas as pd
import gradio as gr
import zipfile
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestRegressor

# Ensure required packages are installed
try:
    import gradio as gr
except ImportError:
    subprocess.run(["pip", "install", "gradio"], check=True)
    import gradio as gr

# Define file paths
zip_file_path = "AI-powered Weather Forecasting.zip"
extract_folder = "weather_forecasting_dataset"

# Extract the ZIP file
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
    zip_ref.extractall(extract_folder)

# Locate CSV file
csv_file_path = os.path.join(extract_folder, "weatherHistory.csv")

# Load the dataset
df = pd.read_csv(csv_file_path)

# Convert 'Formatted Date' to datetime format
df['Formatted Date'] = pd.to_datetime(df['Formatted Date'], utc=True)
df.set_index('Formatted Date', inplace=True)

# βœ… Drop unnecessary columns
df.drop(columns=['Summary', 'Daily Summary', 'Apparent Temperature (C)'], inplace=True)

# βœ… Fill missing values in 'Precip Type'
df['Precip Type'].fillna("rain", inplace=True)

# βœ… Encode categorical variable 'Precip Type'
le = LabelEncoder()
df['Precip Type'] = le.fit_transform(df['Precip Type'])

# βœ… Feature engineering: Extract time-based features
df['Year'] = df.index.year
df['Month'] = df.index.month
df['Day'] = df.index.day
df['Hour'] = df.index.hour

# βœ… Print final feature names before training
print("βœ… Final Training Features:", df.columns.tolist())

# Define target variable (temperature prediction)
X = df.drop(columns=['Temperature (C)'])
y = df['Temperature (C)']

# βœ… Store feature names
feature_names = X.columns.tolist()
num_features = len(feature_names)

# βœ… Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# βœ… Train RandomForestRegressor
model = RandomForestRegressor(n_estimators=500, random_state=42)
model.fit(X_train, y_train)

# βœ… Debug: Print feature importance
feature_importance = model.feature_importances_
print("πŸ“Š Feature Importance:", dict(zip(feature_names, feature_importance)))

# βœ… Define Prediction Function
def predict_temperature(precip_type, humidity, wind_speed, wind_bearing, visibility, pressure, loud_cover, year, month, day, hour):
    try:
        # βœ… Encode categorical variable
        precip_type_encoded = le.transform([precip_type])[0]

        # βœ… Create correct sample data (Ensures 12 Features)
        sample_data = np.array([[precip_type_encoded, humidity, wind_speed, wind_bearing, visibility, pressure, loud_cover, year, month, day, hour]])

        # βœ… Debug: Print input features before prediction
        print("πŸ”Ή Prediction Input Features:", feature_names)
        print("πŸ”Ή Prediction Input Sample:", sample_data)

        # βœ… Fix feature mismatch by adding missing feature if needed
        if sample_data.shape[1] < num_features:
            missing_features = num_features - sample_data.shape[1]
            sample_data = np.hstack((sample_data, np.zeros((1, missing_features))))
            print(f"⚠️ Added {missing_features} missing features to match model training!")

        # βœ… Debug: Print adjusted sample data
        print("πŸ”Ή Adjusted Sample Data:", sample_data)

        # βœ… Predict temperature
        prediction = model.predict(sample_data)[0]

        # βœ… Debug: Print final prediction value
        print("πŸ”₯ Final Prediction:", prediction)

        return f"Predicted Temperature: {prediction:.2f}Β°C"
    except Exception as e:
        return f"Error: {e}"

# βœ… Gradio UI
inputs = [
    gr.Radio(["rain", "snow"], label="Precip Type"),
    gr.Number(label="Humidity"),
    gr.Number(label="Wind Speed (km/h)"),
    gr.Number(label="Wind Bearing (degrees)"),
    gr.Number(label="Visibility (km)"),
    gr.Number(label="Pressure (millibars)"),
    gr.Number(label="Loud Cover", value=0.0),  # Default to 0 if always 0
    gr.Number(label="Year"),
    gr.Number(label="Month"),
    gr.Number(label="Day"),
    gr.Number(label="Hour"),
]

demo = gr.Interface(fn=predict_temperature, inputs=inputs, outputs="text", title="AI-Powered Weather Forecasting")

demo.launch()