OlamideKayode commited on
Commit
4f21f44
·
verified ·
1 Parent(s): f44602f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -32
app.py CHANGED
@@ -63,38 +63,40 @@ def get_next_row():
63
  return row
64
  return pd.DataFrame()
65
 
66
- # Feature engineering
67
- def engineer(df):
68
- if pd.api.types.is_numeric_dtype(df["timestamp"]):
69
- df["datetime"] = pd.to_datetime(df["timestamp"], unit="s")
70
- else:
71
- df["datetime"] = pd.to_datetime(df["timestamp"])
72
-
73
- df = df.sort_values("datetime")
74
- df["hour_of_day"] = df["datetime"].dt.hour
75
- df["lag_30min"] = df["power_consumption_kwh"].shift(1)
76
- df["lag_1h"] = df["power_consumption_kwh"].shift(2)
77
- df["rolling_avg_1h"] = df["power_consumption_kwh"].rolling(2).mean()
78
- df["rolling_avg_2h"] = df["power_consumption_kwh"].rolling(4).mean()
79
- df["is_weekend"] = df["datetime"].dt.weekday >= 5
80
- df["hour_sin"] = np.sin(2 * np.pi * df["hour_of_day"] / 24)
81
- df["hour_cos"] = np.cos(2 * np.pi * df["hour_of_day"] / 24)
82
-
83
- df = pd.get_dummies(df, columns=["property_type", "region"], drop_first=False)
84
-
85
- expected_features = [
86
- 'lag_30min', 'lag_1h', 'rolling_avg_1h', 'rolling_avg_2h',
87
- 'hour_of_day', 'is_weekend', 'hour_sin', 'hour_cos',
88
- 'temperature_c', 'ev_owner', 'solar_installed',
89
- 'property_type_commercial', 'property_type_residential',
90
- 'region_north', 'region_south', 'region_east', 'region_west'
91
- ]
92
-
93
- for col in expected_features:
94
- if col not in df.columns:
95
- df[col] = 0
96
-
97
- return df
 
 
98
 
99
  # Forecast ahead logic (5 steps, 30min intervals)
100
  def forecast_next(df, model, steps=5):
 
63
  return row
64
  return pd.DataFrame()
65
 
66
+
67
+ def forecast_next(df, model, steps=5):
68
+ forecasts = []
69
+ df_copy = df.copy()
70
+ expected_features = model.feature_names_in_.tolist()
71
+
72
+ for i in range(steps):
73
+ df_copy = engineer(df_copy).dropna()
74
+
75
+ # Ensure all expected features are present
76
+ for col in expected_features:
77
+ if col not in df_copy.columns:
78
+ df_copy[col] = 0
79
+
80
+ # Select and order input features
81
+ input_row = df_copy.iloc[[-1]][expected_features]
82
+
83
+ y_pred = model.predict(input_row)[0]
84
+
85
+ # Prepare next row
86
+ next_timestamp = df_copy.iloc[-1]["timestamp"] + 1800
87
+ new_row = df_copy.iloc[-1].copy()
88
+ new_row["power_consumption_kwh"] = y_pred
89
+ new_row["timestamp"] = next_timestamp
90
+
91
+ df_copy = pd.concat([df_copy, pd.DataFrame([new_row])], ignore_index=True)
92
+ forecasts.append({"timestamp": next_timestamp, "forecast_kwh": y_pred})
93
+
94
+ return pd.DataFrame(forecasts)
95
+
96
+
97
+
98
+
99
+
100
 
101
  # Forecast ahead logic (5 steps, 30min intervals)
102
  def forecast_next(df, model, steps=5):