-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_model.py
More file actions
34 lines (27 loc) · 925 Bytes
/
Copy pathexport_model.py
File metadata and controls
34 lines (27 loc) · 925 Bytes
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
import os
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
import joblib
print("Loading dataset...")
# Load dataset
if os.path.exists("city_day.csv"):
data = pd.read_csv("city_day.csv")
else:
print("Please download city_day.csv first!")
exit(1)
# Clean and prepare
features = ['PM2.5', 'PM10', 'NO2', 'SO2', 'O3', 'CO']
target = 'AQI'
df = data[features + [target]].copy().dropna(subset=[target])
df[features] = df[features].fillna(df[features].mean())
X = df[features]
y = df[target]
print("Training lightweight Random Forest model...")
model = RandomForestRegressor(n_estimators=15, max_depth=10, random_state=42)
model.fit(X, y)
# Create an api/ directory if it doesn't exist to save our model
os.makedirs("api", exist_ok=True)
# Save the model
model_path = os.path.join("api", "aqi_model.joblib")
joblib.dump(model, model_path)
print(f"Model successfully saved to {model_path}!")