File size: 2,301 Bytes
0d4f97b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 pandas as pd
import numpy as np
import joblib
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from xgboost import XGBClassifier
from sklearn.metrics import classification_report

# ---------------- Load & preprocess ----------------
df = pd.read_csv('../city_day_clean_dated.csv', parse_dates=['Date']).sort_values('Date')

# Fill missing hours
full = pd.DataFrame(pd.date_range(df['Date'].min(), df['Date'].max(), freq='h'),  # 'H' → 'h'
                    columns=['DateTime'])
df = full.merge(df, left_on=full['DateTime'].dt.date,
                right_on=df['Date'].dt.date, how='left').ffill()
df['Date'] = df['DateTime']

# Filter numeric gas columns only (avoid 'DateTime', etc.)
gas_cols = ['alcohol', 'NH3', 'CO', 'CO2', 'Toluene', 'acetone', 'lpg', 'smoke']

# Create danger flags
danger_thresholds = {col: np.percentile(df[col].dropna(), 95) for col in gas_cols}
for col in gas_cols:
    df[f'{col}_danger'] = (df[col] > danger_thresholds[col]).astype(int)

df['Danger'] = df[[f'{col}_danger' for col in gas_cols]].max(axis=1)

# Time features
df['Hour']      = df['DateTime'].dt.hour
df['Weekday']   = df['DateTime'].dt.weekday
df['Month']     = df['DateTime'].dt.month
df['Afternoon'] = ((df['Hour'] >= 12) & (df['Hour'] <= 15)).astype(int)

# Simple demo features
features = ['Hour', 'Weekday', 'Month', 'Afternoon']
target   = 'Danger'

train = df[df['DateTime'] < '2020-01-01']
test  = df[df['DateTime'] >= '2020-01-01']

X_train, y_train = train[features], train[target]
X_test,  y_test  = test[features],  test[target]

# ---------------- Build model ----------------
model = make_pipeline(
    StandardScaler(),
    XGBClassifier(
        n_estimators=200,
        max_depth=5,
        learning_rate=0.1,
        subsample=0.8,
        colsample_bytree=0.8,
        scale_pos_weight=10,
        random_state=42,
        eval_metric='logloss'
    )
)
model.fit(X_train, y_train)

print('Training report:')
print(classification_report(y_train, model.predict(X_train)))
print('\nTest report:')
print(classification_report(y_test, model.predict(X_test)))

# ---------------- Save bundle ----------------
joblib.dump({'model': model, 'features': features}, 'gas_danger_model.pkl')
print('✔️ Model saved to gas_danger_model.pkl')