Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,6 @@ import streamlit as st
|
|
| 9 |
from streamlit_autorefresh import st_autorefresh
|
| 10 |
from sklearn.metrics import mean_absolute_error
|
| 11 |
import plotly.express as px
|
| 12 |
-
import plotly.graph_objects as go
|
| 13 |
|
| 14 |
# Auto-refresh every 5 seconds
|
| 15 |
st_autorefresh(interval=5000, key="refresh")
|
|
@@ -81,9 +80,7 @@ def engineer(df):
|
|
| 81 |
df["hour_sin"] = np.sin(2 * np.pi * df["hour_of_day"] / 24)
|
| 82 |
df["hour_cos"] = np.cos(2 * np.pi * df["hour_of_day"] / 24)
|
| 83 |
|
| 84 |
-
|
| 85 |
-
if "property_type" in df.columns and "region" in df.columns:
|
| 86 |
-
df = pd.get_dummies(df, columns=["property_type", "region"], drop_first=False)
|
| 87 |
|
| 88 |
expected_features = [
|
| 89 |
'lag_30min', 'lag_1h', 'rolling_avg_1h', 'rolling_avg_2h',
|
|
@@ -99,21 +96,33 @@ def engineer(df):
|
|
| 99 |
|
| 100 |
return df
|
| 101 |
|
| 102 |
-
# Forecast ahead logic
|
| 103 |
def forecast_next(df, model, steps=5):
|
| 104 |
forecasts = []
|
| 105 |
df_copy = df.copy()
|
|
|
|
|
|
|
| 106 |
for i in range(steps):
|
| 107 |
df_copy = engineer(df_copy).dropna()
|
| 108 |
-
|
|
|
|
| 109 |
y_pred = model.predict(input_row)[0]
|
| 110 |
-
|
| 111 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
new_row = df_copy.iloc[-1].copy()
|
| 113 |
new_row["power_consumption_kwh"] = y_pred
|
| 114 |
-
new_row["timestamp"] =
|
|
|
|
|
|
|
| 115 |
df_copy = pd.concat([df_copy, pd.DataFrame([new_row])], ignore_index=True)
|
| 116 |
-
forecasts.append({"timestamp":
|
|
|
|
| 117 |
return pd.DataFrame(forecasts)
|
| 118 |
|
| 119 |
# UI Layout
|
|
@@ -129,7 +138,14 @@ if not new_row.empty:
|
|
| 129 |
st.session_state.history = pd.concat([st.session_state.history, new_row], ignore_index=True)
|
| 130 |
df_feat = engineer(st.session_state.history).dropna()
|
| 131 |
if not df_feat.empty:
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
prediction = model.predict(latest_input)[0]
|
| 134 |
actual = new_row["power_consumption_kwh"].values[0]
|
| 135 |
mae = mean_absolute_error([actual], [prediction])
|
|
@@ -156,15 +172,15 @@ if not new_row.empty:
|
|
| 156 |
for region in ["east", "west", "north", "south"]:
|
| 157 |
regional = df_feat[df_feat[f"region_{region}"] == 1]
|
| 158 |
if not regional.empty:
|
| 159 |
-
|
| 160 |
-
st.write(f"**{region.title()} Region**: Avg Forecast: {np.mean(
|
| 161 |
|
| 162 |
st.subheader("🏠 Property Type Forecast")
|
| 163 |
for region in ["east", "west", "north", "south"]:
|
| 164 |
for ptype in ["commercial", "residential"]:
|
| 165 |
filtered = df_feat[(df_feat[f"region_{region}"] == 1) & (df_feat[f"property_type_{ptype}"] == 1)]
|
| 166 |
if not filtered.empty:
|
| 167 |
-
preds = model.predict(filtered[
|
| 168 |
st.write(f"{region.title()} / {ptype.title()}: {np.mean(preds):.2f} kWh")
|
| 169 |
else:
|
| 170 |
st.success("✅ All data processed.")
|
|
|
|
| 9 |
from streamlit_autorefresh import st_autorefresh
|
| 10 |
from sklearn.metrics import mean_absolute_error
|
| 11 |
import plotly.express as px
|
|
|
|
| 12 |
|
| 13 |
# Auto-refresh every 5 seconds
|
| 14 |
st_autorefresh(interval=5000, key="refresh")
|
|
|
|
| 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',
|
|
|
|
| 96 |
|
| 97 |
return df
|
| 98 |
|
| 99 |
+
# Forecast ahead logic (5 steps, 30min intervals)
|
| 100 |
def forecast_next(df, model, steps=5):
|
| 101 |
forecasts = []
|
| 102 |
df_copy = df.copy()
|
| 103 |
+
expected_features = model.feature_names_in_.tolist()
|
| 104 |
+
|
| 105 |
for i in range(steps):
|
| 106 |
df_copy = engineer(df_copy).dropna()
|
| 107 |
+
# Ensure columns are aligned to model input
|
| 108 |
+
input_row = df_copy.iloc[[-1]][expected_features]
|
| 109 |
y_pred = model.predict(input_row)[0]
|
| 110 |
+
|
| 111 |
+
# Update the last row's power consumption with prediction
|
| 112 |
+
df_copy.at[df_copy.index[-1], "power_consumption_kwh"] = y_pred
|
| 113 |
+
|
| 114 |
+
# Prepare the next timestamp: add 1800 seconds (30 mins)
|
| 115 |
+
next_timestamp = df_copy.iloc[-1]["timestamp"] + 1800
|
| 116 |
+
|
| 117 |
+
# Prepare new row: copy last row features but update timestamp and power consumption
|
| 118 |
new_row = df_copy.iloc[-1].copy()
|
| 119 |
new_row["power_consumption_kwh"] = y_pred
|
| 120 |
+
new_row["timestamp"] = next_timestamp
|
| 121 |
+
|
| 122 |
+
# Append new row safely
|
| 123 |
df_copy = pd.concat([df_copy, pd.DataFrame([new_row])], ignore_index=True)
|
| 124 |
+
forecasts.append({"timestamp": next_timestamp, "forecast_kwh": y_pred})
|
| 125 |
+
|
| 126 |
return pd.DataFrame(forecasts)
|
| 127 |
|
| 128 |
# UI Layout
|
|
|
|
| 138 |
st.session_state.history = pd.concat([st.session_state.history, new_row], ignore_index=True)
|
| 139 |
df_feat = engineer(st.session_state.history).dropna()
|
| 140 |
if not df_feat.empty:
|
| 141 |
+
# Align features exactly to what model expects
|
| 142 |
+
expected_features = model.feature_names_in_.tolist()
|
| 143 |
+
for col in expected_features:
|
| 144 |
+
if col not in df_feat.columns:
|
| 145 |
+
df_feat[col] = 0
|
| 146 |
+
|
| 147 |
+
latest_input = df_feat[expected_features].iloc[[-1]]
|
| 148 |
+
|
| 149 |
prediction = model.predict(latest_input)[0]
|
| 150 |
actual = new_row["power_consumption_kwh"].values[0]
|
| 151 |
mae = mean_absolute_error([actual], [prediction])
|
|
|
|
| 172 |
for region in ["east", "west", "north", "south"]:
|
| 173 |
regional = df_feat[df_feat[f"region_{region}"] == 1]
|
| 174 |
if not regional.empty:
|
| 175 |
+
preds = model.predict(regional[expected_features])
|
| 176 |
+
st.write(f"**{region.title()} Region**: Avg Forecast: {np.mean(preds):.3f} kWh")
|
| 177 |
|
| 178 |
st.subheader("🏠 Property Type Forecast")
|
| 179 |
for region in ["east", "west", "north", "south"]:
|
| 180 |
for ptype in ["commercial", "residential"]:
|
| 181 |
filtered = df_feat[(df_feat[f"region_{region}"] == 1) & (df_feat[f"property_type_{ptype}"] == 1)]
|
| 182 |
if not filtered.empty:
|
| 183 |
+
preds = model.predict(filtered[expected_features])
|
| 184 |
st.write(f"{region.title()} / {ptype.title()}: {np.mean(preds):.2f} kWh")
|
| 185 |
else:
|
| 186 |
st.success("✅ All data processed.")
|