PROJECT 047 · PYTHON SOURCE
Daily household energy totals
Study-specific code. Shared modules, dependency versions, and reproduction instructions are included in all project files.
Download Python file ↓from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
import numpy as np
import pandas as pd
from scipy.stats import spearmanr
from sklearn.decomposition import PCA
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, precision_score, recall_score
from portfolio.data import load
from portfolio.constants import *
from portfolio.methods import result, sql, rates, associations, distribution, regression, classification, cluster
from portfolio.engine import execute
META = {'id': 47, 'dataset': 'energy', 'title': 'Daily household energy totals', 'question': 'How variable is daily appliance use when only complete days are counted?', 'method': 'Aggregate 144 ten-minute intervals into daily kWh and exclude partial days.', 'action': 'Use complete-day totals for reporting before considering a tariff or conservation study.', 'limitations': 'Measurements come from one home over a limited period. Energy is Wh per recorded 10-minute interval. This is not a representative household sample; tariffs and occupancy labels are unavailable. '}
def analyze():
df = load('energy').copy()
daily=df.groupby(df.date.dt.strftime('%Y-%m-%d')).agg(intervals=('appliances','size'),appliance_wh=('appliances','sum'),lighting_wh=('lights','sum')).reset_index()
complete=daily[daily.intervals.eq(144)].copy();complete['appliance_kwh']=complete.appliance_wh/1000
t=distribution(complete.appliance_kwh,'daily_kwh')
out=result(t,'statistic','daily_kwh','Appliance kWh per complete day','Lighting is separate. A complete day requires 144 records; missing or partial days are excluded.',extra={'daily_energy':complete})
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)