-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
244 lines (199 loc) · 7.25 KB
/
app.py
File metadata and controls
244 lines (199 loc) · 7.25 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import math
from pathlib import Path
import numpy as np
import pandas as pd
import streamlit as st
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error, mean_squared_error
# ---------- Data loading & preparation ----------
@st.cache_data
def load_raw_data(filepath: str) -> pd.DataFrame:
"""
Load the original UCI dataset (.txt) or a CSV version.
Create a proper datetime index and clean Global_active_power.
"""
path = Path(filepath)
if path.suffix == ".txt":
# Original UCI format
df = pd.read_csv(
path,
sep=";",
na_values="?",
low_memory=False,
)
if "Date" in df.columns and "Time" in df.columns:
df["datetime"] = pd.to_datetime(
df["Date"] + " " + df["Time"], dayfirst=True
)
else:
raise ValueError("Expected 'Date' and 'Time' columns in txt dataset.")
else:
# CSV version
df = pd.read_csv(path)
if "datetime" in df.columns:
df["datetime"] = pd.to_datetime(df["datetime"])
elif "Date" in df.columns and "Time" in df.columns:
df["datetime"] = pd.to_datetime(
df["Date"] + " " + df["Time"], dayfirst=True
)
else:
raise ValueError(
"CSV must contain either 'datetime' or 'Date' + 'Time' columns."
)
# Make sure Global_active_power is numeric
df["Global_active_power"] = pd.to_numeric(
df["Global_active_power"], errors="coerce"
)
# Basic cleaning
df = df.dropna(subset=["Global_active_power"])
df = df.set_index("datetime").sort_index()
return df
@st.cache_data
def prepare_hourly_data(_df: pd.DataFrame):
"""
Resample to hourly data and create simple calendar features.
Returns:
data - full hourly dataframe with features
X - feature matrix
y - target series
cols - feature column names
"""
df = _df.copy()
# Hourly mean
hourly = df["Global_active_power"].resample("1h").mean()
hourly_df = pd.DataFrame({"Global_active_power": hourly}).dropna()
data = hourly_df.copy()
data["hour"] = data.index.hour
data["day_of_week"] = data.index.dayofweek
data["month"] = data.index.month
X = data[["hour", "day_of_week", "month"]]
y = data["Global_active_power"]
return data, X, y, ["hour", "day_of_week", "month"]
def train_model(X, y, split_date="2010-09-01"):
"""
Time-based train/test split and Random Forest training.
Baseline = simple mean of the training target.
"""
split_ts = pd.Timestamp(split_date)
# If split_date is outside data range, fall back to 80/20 split
if split_ts <= X.index.min() or split_ts >= X.index.max():
split_ts = X.index[int(len(X) * 0.8)]
X_train = X.loc[X.index < split_ts]
X_test = X.loc[X.index >= split_ts]
y_train = y.loc[y.index < split_ts]
y_test = y.loc[y.index >= split_ts]
model = RandomForestRegressor(
n_estimators=200,
random_state=42,
n_jobs=-1,
)
model.fit(X_train, y_train)
# Baseline: constant prediction = train mean
baseline_pred = np.full(len(y_test), y_train.mean(), dtype=float)
model_pred = model.predict(X_test)
return X_train, X_test, y_train, y_test, baseline_pred, model_pred, model
def compute_metrics(y_true, y_pred):
mae = mean_absolute_error(y_true, y_pred)
rmse = math.sqrt(mean_squared_error(y_true, y_pred))
return mae, rmse
# ---------- Streamlit app ----------
def main():
st.title("🔋 Energy Usage Forecasting Dashboard")
st.write(
"This app uses historical household electricity consumption data to "
"forecast hourly energy usage and compare a Random Forest model "
"to a simple baseline."
)
full_data = Path("data/household_power_consumption.txt")
sample_data = Path("data/sample_household_power_consumption.csv")
if full_data.exists():
data_path = full_data
st.info("Using full local dataset (`household_power_consumption.txt`).")
elif sample_data.exists():
data_path = sample_data
st.warning(
"Using sample dataset (`sample_household_power_consumption.csv`) – demo mode."
)
else:
st.error(
"No dataset found.\n\n"
"Please either:\n"
"• Download `household_power_consumption.txt` into the `data/` folder, or\n"
"• Include `sample_household_power_consumption.csv` in `data/`."
)
st.stop()
# ---- Load & prepare data ----
with st.spinner("Loading and preparing data..."):
raw_df = load_raw_data(str(data_path))
data, X, y, feature_cols = prepare_hourly_data(raw_df)
(
X_train,
X_test,
y_train,
y_test,
baseline_pred,
model_pred,
model,
) = train_model(X, y)
# ---- 1. Data overview ----
st.subheader("1. Data Overview")
st.write("**Hourly date range:**", data.index.min(), "to", data.index.max())
st.write("**Total hourly records:**", len(data))
st.write("Sample of the hourly data:")
st.dataframe(
data[["Global_active_power", "hour", "day_of_week", "month"]].head(200)
)
# Safe line chart: make sure it's a float Series
st.write("Hourly Global Active Power (kW):")
st.line_chart(
data["Global_active_power"]
.astype(float)
.rename("Global_active_power (kW)")
)
# ---- 2. Model performance ----
st.subheader("2. Model Performance")
baseline_mae, baseline_rmse = compute_metrics(y_test, baseline_pred)
model_mae, model_rmse = compute_metrics(y_test, model_pred)
col1, col2 = st.columns(2)
with col1:
st.markdown("**Baseline (train-mean prediction)**")
st.write(f"MAE: {baseline_mae:.4f}")
st.write(f"RMSE: {baseline_rmse:.4f}")
with col2:
st.markdown("**Random Forest model**")
st.write(f"MAE: {model_mae:.4f}")
st.write(f"RMSE: {model_rmse:.4f}")
# ---- 3. Actual vs predicted ----
st.subheader("3. Actual vs Predicted (Test Period)")
results = pd.DataFrame(
{
"Actual": y_test.astype(float),
"Baseline": baseline_pred.astype(float),
"RandomForest": model_pred.astype(float),
},
index=y_test.index,
)
min_date = results.index.min()
max_date = results.index.max()
st.write("Select a date range from the test period:")
date_range = st.slider(
"Date range",
min_value=min_date.to_pydatetime(),
max_value=max_date.to_pydatetime(),
value=(
min_date.to_pydatetime(),
(min_date + pd.Timedelta(days=7)).to_pydatetime(),
),
format="YYYY-MM-DD",
)
start, end = date_range
mask = (results.index >= start) & (results.index <= end)
plot_data = results.loc[mask].astype(float)
st.line_chart(plot_data)
st.caption(
"This dashboard trains a Random Forest regressor on historical hourly energy usage, "
"compares it to a simple baseline (train-set average), and visualises predictions "
"over a selected test period."
)
if __name__ == "__main__":
main()