Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- app.py +28 -23
- energy_pred_df.pkl +2 -2
- gas_model.pkl +2 -2
app.py
CHANGED
|
@@ -6,6 +6,7 @@ Created on Sun Dec 11 15:09:37 2022
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import streamlit as st
|
|
|
|
| 9 |
import pandas as pd
|
| 10 |
import pickle as pkl
|
| 11 |
from datetime import datetime, timedelta
|
|
@@ -25,8 +26,8 @@ with open("energy_pred_df.pkl", "rb") as file1:
|
|
| 25 |
with open("energy_model.pkl", "rb") as file2:
|
| 26 |
emodel = pkl.load(file2)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
|
| 32 |
st.title("Electricity & Gas Utility Expense Forecast Tool")
|
|
@@ -44,10 +45,10 @@ fh_months = fh_years * 12+2
|
|
| 44 |
|
| 45 |
st.caption("The default for the below is to use the historical 5 year trend. Please make adjustments as necessary")
|
| 46 |
Unit_Count = st.slider(label = "All same store unit totals", min_value=18000, max_value=30000, step = 100, value = 20500)
|
| 47 |
-
MN_units = st.slider(label = "Minnesota units annual growth rate", min_value=-5.0, max_value=10.0, step = 0.1, value = 2.8, format = "%d%%")/100
|
| 48 |
-
TX_units = st.slider(label = "Texas units annual growth rate", min_value=-5.0, max_value=10.0, step = 0.1, value = 2.3, format = "%d%%")/100
|
| 49 |
-
FL_units = st.slider(label = "Florida units annual growth rate", min_value=-5.0, max_value=10.0, step = 0.1, value = 1.9, format = "%d%%")/100
|
| 50 |
-
GA_units = st.slider(label = "Georgia units annual growth rate", min_value=-5.0, max_value=10.0, step = 0.1, value = 2.7, format = "%d%%")/100
|
| 51 |
|
| 52 |
annual_cpi = st.slider(label = "The expected CPI rate is energy specific.", min_value=-20.0, max_value=35.0, step = 0.5, value = 31.0, format = "%i%%")/100
|
| 53 |
|
|
@@ -125,8 +126,12 @@ for t in fcst_period:
|
|
| 125 |
lst = [units,cpi,electric,Gas,mn_unit,tx_unit,fl_unit,ga_unit,other_units,mn_temp,tx_temp,fl_temp,ga_temp,price,year,month,w_temp]
|
| 126 |
forecast.loc[t] = lst
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
st.subheader("Same Store Unit Growth 2011-{}".format(int(max_year)))
|
| 132 |
units_chart = go.Figure()
|
|
@@ -157,38 +162,38 @@ st.dataframe(data = units_df.T)
|
|
| 157 |
|
| 158 |
|
| 159 |
st.subheader("Net Electric Expense with Forecast")
|
| 160 |
-
electric_df = df[['Electricity_Expense','CPI_Energy','Unit_Count']]
|
|
|
|
| 161 |
electric_df['next_year'] = electric_df.index - DateOffset(years=-1)
|
| 162 |
-
s1 = electric_df.groupby('next_year').agg({'Electricity_Expense':'max', 'CPI_Energy':'max', 'Unit_Count':'max'})
|
| 163 |
-
s1.columns = ['LY_expense','LY_cpi','LY_units']
|
| 164 |
new_df = electric_df.join(s1, how ='outer')
|
| 165 |
new_df['simple_fcst'] = new_df.LY_expense/new_df.LY_units * (new_df.CPI_Energy/new_df.LY_cpi) * new_df.Unit_Count
|
| 166 |
-
# new_df['ml_forecast']= emodel.predict(start=new_df.index[12],end=new_df.index[-1],dynamic=True) * new_df.Unit_Count
|
| 167 |
new_df['simp_per_error'] = abs((new_df.simple_fcst-new_df.Electricity_Expense)/new_df.Electricity_Expense)
|
| 168 |
new_df['simp_sq_error'] = (new_df.simple_fcst-new_df.Electricity_Expense) **2
|
| 169 |
-
|
| 170 |
-
|
| 171 |
new_df['Year'] = pd.DatetimeIndex(new_df.index).year
|
| 172 |
|
| 173 |
elec_chart = go.Figure()
|
| 174 |
-
elec_chart.add_trace(go.Scatter(x=new_df.index, y=new_df.Electricity_Expense, name='Actual',mode='lines'))
|
| 175 |
-
elec_chart.add_trace(go.Scatter(x=new_df.index, y=new_df.simple_fcst, name = 'Simple Forecast', mode='lines', line = dict(color = 'red', dash = 'dash')))
|
| 176 |
-
|
| 177 |
st.plotly_chart(elec_chart, theme="streamlit")
|
| 178 |
|
| 179 |
elec_df = new_df.groupby('Year').agg(
|
| 180 |
ElecExp = pd.NamedAgg('Electricity_Expense','sum'),
|
| 181 |
SimpFcst = pd.NamedAgg('simple_fcst','sum'),
|
| 182 |
simpMAPE = pd.NamedAgg('simp_per_error','mean'),
|
| 183 |
-
simpRMSE = pd.NamedAgg('simp_sq_error','mean')
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
elec_df = elec_df[1:-1]
|
| 189 |
elec_df.simpRMSE = elec_df.simpRMSE.apply(lambda x: math.sqrt(x))
|
| 190 |
# elec_df.MRMSE = elec_df.MRMSE.apply(lambda x: math.sqrt(x))
|
| 191 |
-
elec_df.columns = ['Actual Electric Expense', 'Simple Forecast', 'Simple Forecast - MAPE', 'Simple Forecast - RMSE']
|
| 192 |
st.dataframe(data = elec_df.T)
|
| 193 |
|
| 194 |
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import streamlit as st
|
| 9 |
+
import numpy as np
|
| 10 |
import pandas as pd
|
| 11 |
import pickle as pkl
|
| 12 |
from datetime import datetime, timedelta
|
|
|
|
| 26 |
with open("energy_model.pkl", "rb") as file2:
|
| 27 |
emodel = pkl.load(file2)
|
| 28 |
|
| 29 |
+
with open("gas_model.pkl", "rb") as file3:
|
| 30 |
+
gmodel = pkl.load(file3)
|
| 31 |
|
| 32 |
|
| 33 |
st.title("Electricity & Gas Utility Expense Forecast Tool")
|
|
|
|
| 45 |
|
| 46 |
st.caption("The default for the below is to use the historical 5 year trend. Please make adjustments as necessary")
|
| 47 |
Unit_Count = st.slider(label = "All same store unit totals", min_value=18000, max_value=30000, step = 100, value = 20500)
|
| 48 |
+
MN_units = st.slider(label = "Minnesota units annual growth rate", min_value=-5.0, max_value=10.0, step = 0.1, value = 2.8, format = "%d.0%%")/100
|
| 49 |
+
TX_units = st.slider(label = "Texas units annual growth rate", min_value=-5.0, max_value=10.0, step = 0.1, value = 2.3, format = "%d.0%%")/100
|
| 50 |
+
FL_units = st.slider(label = "Florida units annual growth rate", min_value=-5.0, max_value=10.0, step = 0.1, value = 1.9, format = "%d.0%%")/100
|
| 51 |
+
GA_units = st.slider(label = "Georgia units annual growth rate", min_value=-5.0, max_value=10.0, step = 0.1, value = 2.7, format = "%d.0%%")/100
|
| 52 |
|
| 53 |
annual_cpi = st.slider(label = "The expected CPI rate is energy specific.", min_value=-20.0, max_value=35.0, step = 0.5, value = 31.0, format = "%i%%")/100
|
| 54 |
|
|
|
|
| 126 |
lst = [units,cpi,electric,Gas,mn_unit,tx_unit,fl_unit,ga_unit,other_units,mn_temp,tx_temp,fl_temp,ga_temp,price,year,month,w_temp]
|
| 127 |
forecast.loc[t] = lst
|
| 128 |
|
| 129 |
+
X = pd.get_dummies(forecast.Month)
|
| 130 |
+
X.columns = ['Month_1','Month_2','Month_3','Month_4','Month_5','Month_6','Month_7','Month_8','Month_9','Month_10','Month_11','Month_12']
|
| 131 |
+
X = X.join(forecast[['Unit_Count', 'CPI_Energy','MN_units', 'TX_units', 'FL_units', 'GA_units', 'weighted_avg_temp']])
|
| 132 |
+
forecast['ml_forecast_e'] = emodel.predict(X)
|
| 133 |
+
X = X.join(forecast.Price)
|
| 134 |
+
forecast['Gas_Expense'] = gmodel.predict(X)
|
| 135 |
|
| 136 |
st.subheader("Same Store Unit Growth 2011-{}".format(int(max_year)))
|
| 137 |
units_chart = go.Figure()
|
|
|
|
| 162 |
|
| 163 |
|
| 164 |
st.subheader("Net Electric Expense with Forecast")
|
| 165 |
+
electric_df = df[['Electricity_Expense','CPI_Energy','Unit_Count','ml_forecast_e']]
|
| 166 |
+
electric_df = pd.concat([electric_df,forecast[['Electricity_Expense','CPI_Energy','Unit_Count','ml_forecast_e']]])
|
| 167 |
electric_df['next_year'] = electric_df.index - DateOffset(years=-1)
|
| 168 |
+
s1 = electric_df.groupby('next_year').agg({'Electricity_Expense':'max', 'CPI_Energy':'max', 'Unit_Count':'max', 'ml_forecast_e':'max'})
|
| 169 |
+
s1.columns = ['LY_expense','LY_cpi','LY_units','LY_mlF']
|
| 170 |
new_df = electric_df.join(s1, how ='outer')
|
| 171 |
new_df['simple_fcst'] = new_df.LY_expense/new_df.LY_units * (new_df.CPI_Energy/new_df.LY_cpi) * new_df.Unit_Count
|
|
|
|
| 172 |
new_df['simp_per_error'] = abs((new_df.simple_fcst-new_df.Electricity_Expense)/new_df.Electricity_Expense)
|
| 173 |
new_df['simp_sq_error'] = (new_df.simple_fcst-new_df.Electricity_Expense) **2
|
| 174 |
+
new_df['ml_per_error'] = abs((new_df.ml_forecast_e-new_df.Electricity_Expense)/new_df.Electricity_Expense)
|
| 175 |
+
new_df['ml_sq_error'] = (new_df.ml_forecast_e-new_df.Electricity_Expense) **2
|
| 176 |
new_df['Year'] = pd.DatetimeIndex(new_df.index).year
|
| 177 |
|
| 178 |
elec_chart = go.Figure()
|
| 179 |
+
elec_chart.add_trace(go.Scatter(x=new_df.index[:-12], y=new_df.Electricity_Expense[:-12], name='Actual',mode='lines'))
|
| 180 |
+
elec_chart.add_trace(go.Scatter(x=new_df.index[:-12], y=new_df.simple_fcst[:-12], name = 'Simple Forecast', mode='lines', line = dict(color = 'red', dash = 'dash')))
|
| 181 |
+
elec_chart.add_trace(go.Scatter(x=new_df.index[:-12], y=new_df.ml_forecast[:-12], name = 'ML Forecast', mode='lines', line = dict(color = 'green', dash = 'dash')))
|
| 182 |
st.plotly_chart(elec_chart, theme="streamlit")
|
| 183 |
|
| 184 |
elec_df = new_df.groupby('Year').agg(
|
| 185 |
ElecExp = pd.NamedAgg('Electricity_Expense','sum'),
|
| 186 |
SimpFcst = pd.NamedAgg('simple_fcst','sum'),
|
| 187 |
simpMAPE = pd.NamedAgg('simp_per_error','mean'),
|
| 188 |
+
simpRMSE = pd.NamedAgg('simp_sq_error','mean'),
|
| 189 |
+
MLFcst = pd.NamedAgg('ml_forecast_e','sum'),
|
| 190 |
+
MMAPE = pd.NamedAgg('ml_per_error','mean'),
|
| 191 |
+
MRMSE = pd.NamedAgg('ml_sq_error','mean'))
|
| 192 |
+
|
| 193 |
elec_df = elec_df[1:-1]
|
| 194 |
elec_df.simpRMSE = elec_df.simpRMSE.apply(lambda x: math.sqrt(x))
|
| 195 |
# elec_df.MRMSE = elec_df.MRMSE.apply(lambda x: math.sqrt(x))
|
| 196 |
+
elec_df.columns = ['Actual Electric Expense', 'Simple Forecast', 'Simple Forecast - MAPE', 'Simple Forecast - RMSE', 'Machine Learning Forecast', 'ML Forecast - MAPE', 'ML Forecast - RMSE']
|
| 197 |
st.dataframe(data = elec_df.T)
|
| 198 |
|
| 199 |
|
energy_pred_df.pkl
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2634ee445ed86a6a86366b3c5f416bd74e0f13f5d7e8abb518a4cc51b1ea43ca
|
| 3 |
+
size 24368
|
gas_model.pkl
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:29888362d5e2ba537a844e7150add32e5fac6795350f90020125f3e6e0d5a70b
|
| 3 |
+
size 194093
|