Evan Marie online: | EvanMarie.com| EvanMarie@Proton.me | Linked In | GitHub | Hugging Face | Mastadon | Jovian.ai | TikTok | CodeWars | Discord ⇨ ✨ EvanMarie ✨#6114 | |
from helpers_elegant import *
import_all()
plt.style.use('elegant.mplstyle')
import seaborn as sns
from prophet import Prophet
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import mean_absolute_error as mae
from sklearn.metrics import mean_absolute_percentage_error as mape
import warnings
from tqdm import tqdm
warnings.filterwarnings("ignore")
%matplotlib inline
colors = sns.color_palette("husl", 20)
clr = list(map(mpl.colors.rgb2hex, colors))
from random import choice as rc
import yfinance as yf
%%html
<style>
a:link {color: pink !important; font-weight: 600 !important;}
a:visited {color: pink !important; font-weight: 600 !important;}
</style>
Forecasting Apple Stock Prices | ||
---|---|---|
Dataset: Apple stock, ticker symbol "AAPL" |
---|
data = yf.download('AAPL')
[*********************100%***********************] 1 of 1 completed
data.Close.plot(figsize = (17, 7), color = 'yellow')
plt.title('Apple Stock: Entire Date Range')
plt.xlabel('Date'); plt.ylabel('Stock Price in Dollars')
plt.show()
df_overview(data, 'Imported Apple Stock Data: ')
Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|
datatype | float64 | float64 | float64 | float64 | float64 | int64 |
missing values | 0 | 0 | 0 | 0 | 0 | 0 |
count | 10,631.00 | 10,631.00 | 10,631.00 | 10,631.00 | 10,631.00 | 10,631.00 |
mean | 16.79 | 16.99 | 16.60 | 16.80 | 16.13 | 327,001,433.92 |
std | 35.65 | 36.09 | 35.23 | 35.67 | 35.36 | 337,655,771.97 |
min | 0.05 | 0.05 | 0.05 | 0.05 | 0.04 | 0.00 |
25% | 0.29 | 0.30 | 0.28 | 0.29 | 0.24 | 120,993,600.00 |
50% | 0.49 | 0.50 | 0.48 | 0.49 | 0.40 | 214,244,800.00 |
75% | 16.35 | 16.45 | 16.19 | 16.31 | 14.09 | 406,478,800.00 |
max | 182.63 | 182.94 | 179.12 | 182.01 | 180.96 | 7,421,640,800.00 |
total rows | 10,631 |
---|---|
total columns | 6 |
column names | Open, High, Low, Close, Adj Close, Volume |
index start | 1980-12-12 00:00:00 |
index end | 2023-02-10 00:00:00 |
total missing values | 0 |
Imported Apple Stock Data: Head and Tail |
Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|
Date | ||||||
1980-12-12 | 0.13 | 0.13 | 0.13 | 0.13 | 0.10 | 469,033,600 |
1980-12-15 | 0.12 | 0.12 | 0.12 | 0.12 | 0.09 | 175,884,800 |
1980-12-16 | 0.11 | 0.11 | 0.11 | 0.11 | 0.09 | 105,728,000 |
Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|
Date | ||||||
2023-02-08 | 153.88 | 154.58 | 151.17 | 151.92 | 151.92 | 64,120,100 |
2023-02-09 | 153.78 | 154.33 | 150.42 | 150.87 | 150.87 | 55,921,800 |
2023-02-10 | 149.46 | 151.34 | 149.22 | 150.65 | 150.65 | 29,856,213 |
data = data[['Close']]
data.columns = ['close']
df_overview(data, 'Close Data Only')
close | |
---|---|
datatype | float64 |
missing values | 0 |
count | 10,631.00 |
mean | 16.80 |
std | 35.67 |
min | 0.05 |
25% | 0.29 |
50% | 0.49 |
75% | 16.31 |
max | 182.01 |
total rows | 10,631 |
---|---|
total columns | 1 |
column names | close |
index start | 1980-12-12 00:00:00 |
index end | 2023-02-10 00:00:00 |
total missing values | 0 |
Close Data Only Head and Tail |
close | |
---|---|
Date | |
1980-12-12 | 0.13 |
1980-12-15 | 0.12 |
1980-12-16 | 0.11 |
close | |
---|---|
Date | |
2023-02-08 | 151.92 |
2023-02-09 | 150.87 |
2023-02-10 | 150.65 |
Initial Data Visualization: 2014 to 2023 | ||
---|---|---|
apple_zoom_in = data.loc['2014': '2023']
fix, axes = plt.subplots(4, 1, figsize = (12, 17))
apple_zoom_in.plot(ax = axes[0], title = 'Apple Stock Prices', ms=.5, c = rc(clr))
apple_zoom_in.resample('W').mean().plot(ax = axes[1],
title = 'Apple Stock Prices: Weekly Avg', c = rc(clr))
apple_zoom_in.resample('M').mean().plot(ax = axes[2],
title = 'Apple Stock Prices: Monthly Avg', c = rc(clr))
apple_zoom_in.resample('A').mean().plot(ax = axes[3],
title = 'Apple Stock Prices: Yearly Avg', c = rc(clr))
axes[0].legend().remove(); axes[1].legend().remove();
axes[2].legend().remove(); axes[3].legend().remove();
plt.tight_layout()
plt.show()
Apple Prophet Model | ||
---|---|---|
- Prophet requires the datetime index to be a column named ds and the metric or target column to be named y |
Train-Test Split |
---|
split_date = '1-Jan-2013'
train_data = data.loc[data.index <= split_date].copy()
test_data = data.loc[data.index > split_date].copy()
fig, ax = plt.subplots(figsize = (17, 8))
train_data['close'].plot(ax = ax, c = 'cyan', ms = 4, style='.', logy=True);
test_data['close'].plot(ax = ax, c = 'yellow', ms = 4, style='.', logy=True);
plt.title('Train-Test Split');
plt.legend(labels = ['training', 'testing']);
prophet_data = data.reset_index().rename(columns = {'Date': 'ds', 'close': 'y'})
head_tail_horz(prophet_data, 5, 'Data Prepped for Prophet')
Data Prepped for Prophet |
ds | y | |
---|---|---|
0 | 1980-12-12 | 0.13 |
1 | 1980-12-15 | 0.12 |
2 | 1980-12-16 | 0.11 |
3 | 1980-12-17 | 0.12 |
4 | 1980-12-18 | 0.12 |
ds | y | |
---|---|---|
10626 | 2023-02-06 | 151.73 |
10627 | 2023-02-07 | 154.65 |
10628 | 2023-02-08 | 151.92 |
10629 | 2023-02-09 | 150.87 |
10630 | 2023-02-10 | 150.65 |
Documentation on Prophet |
---|
# help(Prophet)
Prepping the testing and training data |
---|
prophet_train = train_data.reset_index()\
.rename(columns = {'Date': 'ds', 'close': 'y'})
prophet_test = test_data.reset_index()\
.rename(columns = {'Date': 'ds', 'close': 'y'})
Fitting the model |
---|
%%time
model = Prophet()
model.fit(prophet_train)
13:30:42 - cmdstanpy - INFO - Chain [1] start processing 13:30:43 - cmdstanpy - INFO - Chain [1] done processing
CPU times: user 293 ms, sys: 33.7 ms, total: 327 ms Wall time: 2.04 s
<prophet.forecaster.Prophet at 0x7fc9c91dcdf0>
Forecasting |
---|
prophet_forecast = model.predict(prophet_test)
head_tail_vert(prophet_forecast, 5, title = "Prophet Forecast")
Prophet Forecast: head(5) |
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2013-01-02 | 18.14 | 16.51 | 19.45 | 18.14 | 18.14 | -0.10 | -0.10 | -0.10 | -0.01 | -0.01 | -0.01 | -0.09 | -0.09 | -0.09 | 0.00 | 0.00 | 0.00 | 18.04 |
1 | 2013-01-03 | 18.15 | 16.48 | 19.65 | 18.15 | 18.15 | -0.08 | -0.08 | -0.08 | -0.00 | -0.00 | -0.00 | -0.08 | -0.08 | -0.08 | 0.00 | 0.00 | 0.00 | 18.06 |
2 | 2013-01-04 | 18.15 | 16.52 | 19.56 | 18.15 | 18.15 | -0.09 | -0.09 | -0.09 | -0.01 | -0.01 | -0.01 | -0.08 | -0.08 | -0.08 | 0.00 | 0.00 | 0.00 | 18.07 |
3 | 2013-01-07 | 18.18 | 16.65 | 19.56 | 18.18 | 18.18 | -0.08 | -0.08 | -0.08 | -0.01 | -0.01 | -0.01 | -0.07 | -0.07 | -0.07 | 0.00 | 0.00 | 0.00 | 18.09 |
4 | 2013-01-08 | 18.18 | 16.45 | 19.61 | 18.18 | 18.18 | -0.08 | -0.08 | -0.08 | -0.01 | -0.01 | -0.01 | -0.07 | -0.07 | -0.07 | 0.00 | 0.00 | 0.00 | 18.10 |
Prophet Forecast: tail(5) |
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2541 | 2023-02-06 | 45.76 | 38.85 | 51.97 | 39.09 | 51.97 | -0.12 | -0.12 | -0.12 | -0.01 | -0.01 | -0.01 | -0.11 | -0.11 | -0.11 | 0.00 | 0.00 | 0.00 | 45.63 |
2542 | 2023-02-07 | 45.76 | 38.69 | 52.00 | 39.09 | 51.98 | -0.12 | -0.12 | -0.12 | -0.01 | -0.01 | -0.01 | -0.11 | -0.11 | -0.11 | 0.00 | 0.00 | 0.00 | 45.64 |
2543 | 2023-02-08 | 45.77 | 38.53 | 51.65 | 39.09 | 51.99 | -0.11 | -0.11 | -0.11 | -0.01 | -0.01 | -0.01 | -0.10 | -0.10 | -0.10 | 0.00 | 0.00 | 0.00 | 45.66 |
2544 | 2023-02-09 | 45.78 | 39.09 | 51.95 | 39.09 | 52.00 | -0.10 | -0.10 | -0.10 | -0.00 | -0.00 | -0.00 | -0.10 | -0.10 | -0.10 | 0.00 | 0.00 | 0.00 | 45.68 |
2545 | 2023-02-10 | 45.79 | 38.70 | 51.96 | 39.10 | 52.02 | -0.10 | -0.10 | -0.10 | -0.01 | -0.01 | -0.01 | -0.09 | -0.09 | -0.09 | 0.00 | 0.00 | 0.00 | 45.68 |
Customized Prophet Plot Function (adapted from Prophet documentation) |
---|
def customized_plot(model,
forecast,
ax=None,
uncertainty=True,
plot_cap=True,
xlabel='datetime',
ylabel='target metric',
logy = False,
):
if ax is None:
fig = plt.figure()
ax = fig.add_subplot(111)
else:
fig = ax.get_figure()
fcst_t = forecast['ds'].dt.to_pydatetime()
ax.plot(model.history['ds'].dt.to_pydatetime(), model.history['y'], c=rc(clr))
ax.plot(fcst_t, forecast['yhat'], ls='-', c=rc(clr))
if logy:
ax.set_yscale('log')
if 'cap' in forecast and plot_cap:
ax.plot(fcst_t, forecast['cap'], ls='--', c=rc(clr))
if model.logistic_floor and 'floor' in forecast and plot_cap:
ax.plot(fcst_t, forecast['floor'], ls='--', c=rc(clr))
if uncertainty:
ax.fill_between(fcst_t, forecast['yhat_lower'], forecast['yhat_upper'],
color=rc(clr), alpha=0.4)
ax.grid(True, which='major', c=rc(clr), ls='-', lw=1, alpha=0.4)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
fig.tight_layout()
return fig
fig, ax = plt.subplots(figsize=(10, 5))
fig = customized_plot(model = model, forecast = prophet_forecast, ax=ax);
ax.set_title('Prophet Forecast');
plt.show()
Confidence interval becomes wider the farther a prediction is into the future | ||
---|---|---|
(Source) Confidence Intervals are estimates that are calculated from sample data to determine ranges likely to contain the population parameter(mean, standard deviation)of interest. For example, if our population is (2,6), a confidence interval of the mean suggests that the population mean is likely between 2 and 6. And how confidently can we say this? Obviously 100%, right? Because we know all the values and we can calculate it very easily. But in real-life problems, this is not the case. It is not always feasible or possible to study the whole population. So what do we do? We take sample data. But can we rely on one sample? No, because different samples from the same data will produce different mean. So we take numerous random samples (from the same population) and calculate confidence intervals for each sample and a certain percentage of these ranges will contain the true population parameter. This certain percentage is called the confidence level. A 95% confidence level means that out of 100 random samples taken, I expect 95 of the confidence intervals to contain the true population parameter. |
Visualizing Model Components |
---|
import matplotlib
matplotlib.rcParams.update(matplotlib.rcParamsDefault)
fig = model.plot_components(prophet_forecast)
plt.show()
Apple Model Evaluation |
---|
fig, ax = plt.subplots()
ax.scatter(test_data.index, test_data['close'], color='blue', label = 'close')
customized_plot(model = model, forecast = prophet_forecast, ax=ax)
plt.legend()
plt.show()
Zoom in to date range within predictions and actual values |
---|
def zoom_in_prophet(forecast,
test_data,
start_date,
end_date,
title,
column,
legend_loc = 1
):
forecast_segment = forecast.set_index('ds')
forecast_segment = forecast_segment.loc[start_date : end_date]
test_segment = test_data.loc[start_date : end_date]
plt.style.use('elegant.mplstyle')
fig, ax = plt.subplots(figsize = (20, 4))
ax.scatter(test_segment.index,
test_segment[column],
color = 'yellow',
label = "Actual Values",
s = 20)
ax.scatter(forecast_segment.index,
forecast_segment['yhat'],
color = 'deeppink',
label = 'Predicted Values',
s = 20)
plt.title(title + ' Actual Values vs Predicted');
plt.xticks(rotation = 40)
plt.legend(loc = legend_loc)
plt.show()
Actual vs Predicted: 4 Year Increments |
---|
zoom_in_prophet(prophet_forecast, test_data, '2013-01-01', '2018-01-01',
'Apple Stock 2013-2017:', column = 'close', legend_loc = 2)
zoom_in_prophet(prophet_forecast, test_data, '2018-01-01', '2023-02-09',
'Apple Stock 2018-2022:', column = 'close', legend_loc = 2)
Error Metric Evaluation |
---|
actual_values_apple1 = test_data.close
predictions_apple1 = prophet_forecast.yhat
pretty(np.sqrt(mse(actual_values_apple1, predictions_apple1)),
'Mean Squared Error Score:')
pretty(mae(actual_values_apple1, predictions_apple1), 'Mean Absolute Error Score:')
pretty(f'{mape(actual_values_apple1, predictions_apple1)*100:.3f}%',
'Mean Absolute Percenage Error:')
Mean Squared Error Score: |
52.498785 |
Mean Absolute Error Score: |
32.313079 |
Mean Absolute Percenage Error: |
32.968% |
Prophet Model: Apple Stock, with holidays | ||
---|---|---|
- cutting the dataset down to just the years 2014 up to February of 2023 to see if this helps the model find trends more easily by focusing on training data that more exhibits the trends that we see in the testing data near the end of the Apple stock data |
prophet_part_2 = data.loc['2014-01-01':'2023-02-09']
train_length = round(len(prophet_part_2) * 0.8)
pretty(f'{train_length:,}', 'Length of new training data')
pretty(f'{(len(prophet_part_2) - train_length):,}',
'Length of new testing data')
Length of new training data |
1,834 |
Length of new testing data |
459 |
prophet_part_2 = prophet_part_2.reset_index()
prophet_part_2.columns = ['ds', 'y']
prophet_train = prophet_part_2.iloc[:train_length]
prophet_test = prophet_part_2.iloc[train_length:]
fig, ax = plt.subplots(figsize = (17, 6))
prophet_train['y'].plot(ax = ax, c = 'cyan', ms = 4);
prophet_test['y'].plot(ax = ax, c = 'yellow', ms = 4);
plt.xlabel('Number of Training & Testing Samples')
plt.ylabel('Stock Price')
plt.title('Train-Test Split');
plt.legend(labels = ['training', 'testing']);
plt.show()
holiday_model = Prophet()
holiday_model.add_country_holidays(country_name='US')
holiday_model = holiday_model.fit(prophet_train)
13:46:14 - cmdstanpy - INFO - Chain [1] start processing 13:46:15 - cmdstanpy - INFO - Chain [1] done processing
help(holiday_model.add_country_holidays)
Help on method add_country_holidays in module prophet.forecaster: add_country_holidays(country_name) method of prophet.forecaster.Prophet instance Add in built-in holidays for the specified country. These holidays will be included in addition to any specified on model initialization. Holidays will be calculated for arbitrary date ranges in the history and future. See the online documentation for the list of countries with built-in holidays. Built-in country holidays can only be set for a single country. Parameters ---------- country_name: Name of the country, like 'UnitedStates' or 'US' Returns ------- The prophet object.
pretty('US Holidays from Prophet')
pd.DataFrame(holiday_model.train_holiday_names).style\
.hide(axis = 'index')\
.hide(axis = 'columns')
US Holidays from Prophet |
New Year's Day |
Martin Luther King Jr. Day |
Washington's Birthday |
Memorial Day |
Independence Day |
Labor Day |
Columbus Day |
Veterans Day |
Thanksgiving |
Christmas Day |
Christmas Day (Observed) |
New Year's Day (Observed) |
Veterans Day (Observed) |
Independence Day (Observed) |
Juneteenth National Independence Day |
Juneteenth National Independence Day (Observed) |
Apple Stock Predictions: Accounting for holidays |
---|
holidays_forecast = holiday_model.predict(df = prophet_test)
head_tail_vert(holidays_forecast, 5, 'Holidays Forecast')
Holidays Forecast: head(5) |
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | Christmas Day | Christmas Day_lower | Christmas Day_upper | Christmas Day (Observed) | Christmas Day (Observed)_lower | Christmas Day (Observed)_upper | Columbus Day | Columbus Day_lower | Columbus Day_upper | Independence Day | Independence Day_lower | Independence Day_upper | Independence Day (Observed) | Independence Day (Observed)_lower | Independence Day (Observed)_upper | Juneteenth National Independence Day | Juneteenth National Independence Day_lower | Juneteenth National Independence Day_upper | Juneteenth National Independence Day (Observed) | Juneteenth National Independence Day (Observed)_lower | Juneteenth National Independence Day (Observed)_upper | Labor Day | Labor Day_lower | Labor Day_upper | Martin Luther King Jr. Day | Martin Luther King Jr. Day_lower | Martin Luther King Jr. Day_upper | Memorial Day | Memorial Day_lower | Memorial Day_upper | New Year's Day | New Year's Day_lower | New Year's Day_upper | New Year's Day (Observed) | New Year's Day (Observed)_lower | New Year's Day (Observed)_upper | Thanksgiving | Thanksgiving_lower | Thanksgiving_upper | Veterans Day | Veterans Day_lower | Veterans Day_upper | Veterans Day (Observed) | Veterans Day (Observed)_lower | Veterans Day (Observed)_upper | Washington's Birthday | Washington's Birthday_lower | Washington's Birthday_upper | additive_terms | additive_terms_lower | additive_terms_upper | holidays | holidays_lower | holidays_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2021-04-16 | 139.27 | 131.50 | 142.11 | 139.27 | 139.27 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -2.70 | -2.70 | -2.70 | 0.00 | 0.00 | 0.00 | -0.21 | -0.21 | -0.21 | -2.49 | -2.49 | -2.49 | 0.00 | 0.00 | 0.00 | 136.57 |
1 | 2021-04-19 | 139.73 | 132.03 | 142.86 | 139.73 | 139.73 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -2.44 | -2.44 | -2.44 | 0.00 | 0.00 | 0.00 | -0.15 | -0.15 | -0.15 | -2.30 | -2.30 | -2.30 | 0.00 | 0.00 | 0.00 | 137.28 |
2 | 2021-04-20 | 139.88 | 132.16 | 142.80 | 139.88 | 139.88 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -2.28 | -2.28 | -2.28 | 0.00 | 0.00 | 0.00 | -0.04 | -0.04 | -0.04 | -2.24 | -2.24 | -2.24 | 0.00 | 0.00 | 0.00 | 137.60 |
3 | 2021-04-21 | 140.03 | 132.54 | 143.99 | 140.03 | 140.03 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -2.25 | -2.25 | -2.25 | 0.00 | 0.00 | 0.00 | -0.05 | -0.05 | -0.05 | -2.19 | -2.19 | -2.19 | 0.00 | 0.00 | 0.00 | 137.78 |
4 | 2021-04-22 | 140.18 | 132.26 | 143.51 | 140.18 | 140.18 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -2.24 | -2.24 | -2.24 | 0.00 | 0.00 | 0.00 | -0.09 | -0.09 | -0.09 | -2.15 | -2.15 | -2.15 | 0.00 | 0.00 | 0.00 | 137.94 |
Holidays Forecast: tail(5) |
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | Christmas Day | Christmas Day_lower | Christmas Day_upper | Christmas Day (Observed) | Christmas Day (Observed)_lower | Christmas Day (Observed)_upper | Columbus Day | Columbus Day_lower | Columbus Day_upper | Independence Day | Independence Day_lower | Independence Day_upper | Independence Day (Observed) | Independence Day (Observed)_lower | Independence Day (Observed)_upper | Juneteenth National Independence Day | Juneteenth National Independence Day_lower | Juneteenth National Independence Day_upper | Juneteenth National Independence Day (Observed) | Juneteenth National Independence Day (Observed)_lower | Juneteenth National Independence Day (Observed)_upper | Labor Day | Labor Day_lower | Labor Day_upper | Martin Luther King Jr. Day | Martin Luther King Jr. Day_lower | Martin Luther King Jr. Day_upper | Memorial Day | Memorial Day_lower | Memorial Day_upper | New Year's Day | New Year's Day_lower | New Year's Day_upper | New Year's Day (Observed) | New Year's Day (Observed)_lower | New Year's Day (Observed)_upper | Thanksgiving | Thanksgiving_lower | Thanksgiving_upper | Veterans Day | Veterans Day_lower | Veterans Day_upper | Veterans Day (Observed) | Veterans Day (Observed)_lower | Veterans Day (Observed)_upper | Washington's Birthday | Washington's Birthday_lower | Washington's Birthday_upper | additive_terms | additive_terms_lower | additive_terms_upper | holidays | holidays_lower | holidays_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
454 | 2023-02-03 | 239.30 | 214.34 | 263.39 | 214.88 | 262.41 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.48 | 0.48 | 0.48 | 0.00 | 0.00 | 0.00 | -0.21 | -0.21 | -0.21 | 0.68 | 0.68 | 0.68 | 0.00 | 0.00 | 0.00 | 239.77 |
455 | 2023-02-06 | 239.75 | 215.23 | 262.77 | 215.25 | 262.98 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.42 | 0.42 | 0.42 | 0.00 | 0.00 | 0.00 | -0.15 | -0.15 | -0.15 | 0.57 | 0.57 | 0.57 | 0.00 | 0.00 | 0.00 | 240.17 |
456 | 2023-02-07 | 239.90 | 214.57 | 264.92 | 215.34 | 263.24 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.48 | 0.48 | 0.48 | 0.00 | 0.00 | 0.00 | -0.04 | -0.04 | -0.04 | 0.51 | 0.51 | 0.51 | 0.00 | 0.00 | 0.00 | 240.38 |
457 | 2023-02-08 | 240.06 | 214.77 | 264.22 | 215.46 | 263.50 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.40 | 0.40 | 0.40 | 0.00 | 0.00 | 0.00 | -0.05 | -0.05 | -0.05 | 0.45 | 0.45 | 0.45 | 0.00 | 0.00 | 0.00 | 240.45 |
458 | 2023-02-09 | 240.21 | 214.36 | 264.07 | 215.58 | 263.76 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.29 | 0.29 | 0.29 | 0.00 | 0.00 | 0.00 | -0.09 | -0.09 | -0.09 | 0.38 | 0.38 | 0.38 | 0.00 | 0.00 | 0.00 | 240.50 |
header_text('Apple Stock Forecast with Holidays')
customized_plot(model = holiday_model,
forecast = holidays_forecast)
Apple Stock Forecast with Holidays |
Visualizing Components: Incorporating Holidays |
---|
import matplotlib; matplotlib.rcParams.update(matplotlib.rcParamsDefault)
fig = holiday_model.plot_components(holidays_forecast)
plt.show()
Apple Stock Actual vs Predicted with Holidays: Approximately 1 Year Increments |
---|
zoom_in_prophet(holidays_forecast, test_data,'2021-04-16', '2022-04-16',
'Apr 2021 to Apr 2022:', column = 'close', legend_loc = 2)
zoom_in_prophet(holidays_forecast, test_data, '2022-04-16', '2023-02-09',
'Apr 2022 to Feb 2023:', column = 'close', legend_loc = 'center right')
Error Metrics: Apple Stock Data 2014-2023 with holidays |
---|
actual_values_apple2 = prophet_test.y
predictions_apple2 = holidays_forecast.yhat
pretty('Error Metrics: Prophet - Apple Stock 2014-2023, holidays included')
pretty(np.sqrt(mse(actual_values_apple2, predictions_apple2)),
'Mean Squared Error Score:')
pretty(mae(actual_values_apple2, predictions_apple2),
'Mean Absolute Error Score:')
pretty(f'{mape(actual_values_apple2, predictions_apple2)*100:.3f}%',
'Mean Absolute Percenage Error:')
Error Metrics: Prophet - Apple Stock 2014-2023, holidays included |
Mean Squared Error Score: |
50.269655 |
Mean Absolute Error Score: |
39.430486 |
Mean Absolute Percenage Error: |
27.060% |
pretty('Error Metrics: Prophet - Apple Stock 1980-2023, all data, no holidays')
pretty(np.sqrt(mse(actual_values_apple1, predictions_apple1)),
'Mean Squared Error Score:')
pretty(mae(actual_values_apple1, predictions_apple1), 'Mean Absolute Error Score:')
pretty(f'{mape(actual_values_apple1, predictions_apple1)*100:.3f}%',
'Mean Absolute Percenage Error:')
Error Metrics: Prophet - Apple Stock 1980-2023, all data, no holidays |
Mean Squared Error Score: |
52.498785 |
Mean Absolute Error Score: |
32.313079 |
Mean Absolute Percenage Error: |
32.968% |
Future Predictions | ||
---|---|---|
Retraining the model on the entirety of the data |
---|
future_model = Prophet()
future_model = future_model.fit(prophet_data)
14:54:36 - cmdstanpy - INFO - Chain [1] start processing 14:54:39 - cmdstanpy - INFO - Chain [1] done processing
future = future_model.make_future_dataframe(periods = 365*24,
freq='h',
include_history=False)
future_forecast = future_model.predict(future)
head_tail_vert(future_forecast, 5, 'Future Prediction Forecast')
Future Prediction Forecast: head(5) |
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2023-02-10 | 146.11 | 134.75 | 159.95 | 146.11 | 146.11 | 0.41 | 0.41 | 0.41 | -0.08 | -0.08 | -0.08 | 0.49 | 0.49 | 0.49 | 0.00 | 0.00 | 0.00 | 146.52 |
1 | 2023-02-10 | 146.11 | 134.31 | 159.11 | 146.11 | 146.11 | 0.41 | 0.41 | 0.41 | -0.08 | -0.08 | -0.08 | 0.49 | 0.49 | 0.49 | 0.00 | 0.00 | 0.00 | 146.52 |
2 | 2023-02-10 | 146.11 | 134.36 | 159.11 | 146.11 | 146.11 | 0.41 | 0.41 | 0.41 | -0.07 | -0.07 | -0.07 | 0.49 | 0.49 | 0.49 | 0.00 | 0.00 | 0.00 | 146.52 |
3 | 2023-02-10 | 146.11 | 134.37 | 159.05 | 146.11 | 146.11 | 0.42 | 0.42 | 0.42 | -0.07 | -0.07 | -0.07 | 0.49 | 0.49 | 0.49 | 0.00 | 0.00 | 0.00 | 146.53 |
4 | 2023-02-10 | 146.12 | 134.09 | 158.84 | 146.12 | 146.12 | 0.42 | 0.42 | 0.42 | -0.07 | -0.07 | -0.07 | 0.49 | 0.49 | 0.49 | 0.00 | 0.00 | 0.00 | 146.53 |
Future Prediction Forecast: tail(5) |
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
8755 | 2024-02-09 | 163.28 | 151.66 | 175.72 | 162.29 | 164.20 | 0.46 | 0.46 | 0.46 | -0.02 | -0.02 | -0.02 | 0.49 | 0.49 | 0.49 | 0.00 | 0.00 | 0.00 | 163.75 |
8756 | 2024-02-09 | 163.29 | 150.60 | 176.27 | 162.29 | 164.21 | 0.47 | 0.47 | 0.47 | -0.02 | -0.02 | -0.02 | 0.49 | 0.49 | 0.49 | 0.00 | 0.00 | 0.00 | 163.75 |
8757 | 2024-02-09 | 163.29 | 151.82 | 175.76 | 162.29 | 164.21 | 0.47 | 0.47 | 0.47 | -0.02 | -0.02 | -0.02 | 0.49 | 0.49 | 0.49 | 0.00 | 0.00 | 0.00 | 163.76 |
8758 | 2024-02-09 | 163.29 | 151.87 | 176.41 | 162.29 | 164.21 | 0.47 | 0.47 | 0.47 | -0.02 | -0.02 | -0.02 | 0.49 | 0.49 | 0.49 | 0.00 | 0.00 | 0.00 | 163.76 |
8759 | 2024-02-10 | 163.29 | 151.16 | 176.41 | 162.29 | 164.21 | 0.47 | 0.47 | 0.47 | -0.01 | -0.01 | -0.01 | 0.49 | 0.49 | 0.49 | 0.00 | 0.00 | 0.00 | 163.77 |
future_forecast.yhat.plot(c='cyan', title = 'Future Predictions')
plt.show()
Forecasting Ecommerce Sales in the UK | ||
---|---|---|
- Data covers ecommerce sales in the UK over a slightly longer than 1 year period around 2011< |
df = pd.read_csv('ecommerce.csv')
Sales Data Overview |
---|
df_overview(df[['Quantity', 'UnitPrice']], 'Ecommerce Sales UK')
Quantity | UnitPrice | |
---|---|---|
datatype | int64 | float64 |
missing values | 0 | 0 |
count | 541,909.00 | 541,909.00 |
mean | 9.55 | 4.61 |
std | 218.08 | 96.76 |
min | -80,995.00 | -11,062.06 |
25% | 1.00 | 1.25 |
50% | 3.00 | 2.08 |
75% | 10.00 | 4.13 |
max | 80,995.00 | 38,970.00 |
total rows | 541,909 |
---|---|
total columns | 2 |
column names | Quantity, UnitPrice |
index start | 0 |
index end | 541908 |
total missing values | 0 |
Ecommerce Sales UK Head and Tail |
Quantity | UnitPrice | |
---|---|---|
0 | 6 | 2.55 |
1 | 6 | 3.39 |
2 | 8 | 2.75 |
Quantity | UnitPrice | |
---|---|---|
541906 | 4 | 4.15 |
541907 | 4 | 4.15 |
541908 | 3 | 4.95 |
Consolidating to unique invoices |
---|
data = df[['InvoiceNo', 'UnitPrice']].groupby('InvoiceNo').sum()
dates = df[['InvoiceNo', 'InvoiceDate']]\
.drop_duplicates(subset = 'InvoiceNo')['InvoiceDate']
data = data.reset_index()
dates = pd.DataFrame(dates).reset_index()
data['date'] = dates['InvoiceDate']
data['date'] = pd.to_datetime(data['date'])
data = data.set_index('date')
data = pd.DataFrame(data['UnitPrice'])
data.columns = ['sales']
data_individual_invoices = data.copy()
head_tail_horz(data_individual_invoices, 5, 'Data Condensed to Unique Invoices')
Data Condensed to Unique Invoices |
sales | |
---|---|
date | |
2010-12-01 | 27.37 |
2010-12-01 | 3.70 |
2010-12-01 | 58.24 |
2010-12-01 | 19.10 |
2010-12-01 | 5.95 |
sales | |
---|---|
date | |
2011-12-09 | 2.08 |
2011-12-09 | 2.78 |
2011-12-09 | 224.69 |
2011-12-09 | 10.95 |
2011-12-09 | 2.50 |
Ecommerce: Sales Visualizations | ||
---|---|---|
data_day = pd.DataFrame(data['sales'].resample('D').mean())
data_week = pd.DataFrame(data['sales'].resample('W').mean())
data_month = pd.DataFrame(data['sales'].resample('M').mean())
data_individual_invoices.plot(title = 'Sales by Individual Invoice',
c = rc(clr), figsize = (17, 6));
data_day.plot(title = 'Average Daily Sales', c = rc(clr), figsize = (17, 5));
data_week.plot(title = 'Average Weekly Sales', c = rc(clr), figsize = (17, 5))
data_month.plot(title = 'Average Monthly Sales', c = rc(clr), figsize = (17, 5))
plt.show()
Adding datetime features for visualization purposes |
---|
featurized_sales = featurize_dt_index(data_individual_invoices)
Sales by Months of the Year |
---|
featurized_sales.groupby('month')['sales']\
.mean()\
.plot(kind = 'bar', color = rc(clr), figsize = (17, 6))
plt.ylim(0, 140)
plt.ylabel('Average Cost per Sale')
plt.title('Average Sale Amount by Month')
plt.show()
featurized_sales.month.value_counts().plot(kind = 'bar', color = rc(clr),
figsize = (17, 6))
plt.ylabel('Number of Sales'); plt.xlabel('month')
plt.title('Number of Sales by Month (descending)')
plt.show()
Number of Sales by Quarter |
---|
featurized_sales.quarter.value_counts().plot(kind = 'bar', color = rc(clr),
figsize = (17, 6))
plt.ylabel('Number of Sales'); plt.xlabel('quarter')
plt.title('Number of Sales by Quarter (descending)')
plt.show()
Number of Sales by Day of the Week |
---|
featurized_sales.weekday_name.value_counts().plot(kind = 'bar', color = rc(clr),
figsize = (17, 6))
plt.ylabel('Number of Sales'); plt.xlabel('weekday')
plt.title('Number of Sales by Weekday (descending)')
plt.show()
Number of Sales by Time of Day |
---|
featurized_sales.time_of_day.value_counts().plot(kind = 'bar', color = rc(clr),
figsize = (17, 6))
plt.ylabel('Number of Sales'); plt.xlabel('time_of_day')
plt.title('Number of Sales by Time of Day (descending)')
plt.show()
Prophet Model: UK Ecommerce Sales | ||
---|---|---|
Ecommerce: Train-Test Split |
---|
unique_dates = pd.Series(data.index.date).nunique()
get_dates = pd.DataFrame(data).reset_index()
get_dates.date = get_dates.date.dt.date
get_dates = get_dates.drop_duplicates(subset = 'date').reset_index()
split_point = int(len(data) * 0.8)
pretty(f'{split_point:,}', 'Training Data Length')
pretty(f'{len(data) - split_point:,}', 'Testing Data Length')
Training Data Length |
20,720 |
Testing Data Length |
5,180 |
prophet_data = data.reset_index().rename(columns = {'date': 'ds', 'sales': 'y'})
training = prophet_data.iloc[:split_point]
testing = prophet_data.iloc[split_point:]
fig, ax = plt.subplots(figsize = (17, 6))
training.y.plot(ax = ax, c = 'cyan', ms = 5, style='.', logy = True);
testing.y.plot(ax = ax, c = 'yellow', ms = 5, style='.', logy = True);
plt.title('Ecommerce: Train-Test Split');
plt.legend(labels = ['training', 'testing']);
plt.show()
Fitting the model |
---|
%%time
ecommerce_prophet = Prophet()
ecommerce_prophet.fit(training)
21:38:48 - cmdstanpy - INFO - Chain [1] start processing 21:38:50 - cmdstanpy - INFO - Chain [1] done processing
CPU times: user 469 ms, sys: 44.6 ms, total: 513 ms Wall time: 2.34 s
<prophet.forecaster.Prophet at 0x7faa336fab60>
Incorporating Holidays |
---|
ecommerce_prophet = Prophet()
ecommerce_prophet.add_country_holidays(country_name='GB')
ecommerce_prophet = ecommerce_prophet.fit(training)
21:38:51 - cmdstanpy - INFO - Chain [1] start processing 21:38:52 - cmdstanpy - INFO - Chain [1] done processing
pretty('UK Holidays from Prophet')
pd.DataFrame(holiday_model.train_holiday_names).style\
.hide(axis = 'index')\
.hide(axis = 'columns')
UK Holidays from Prophet |
New Year's Day |
Martin Luther King Jr. Day |
Washington's Birthday |
Memorial Day |
Independence Day |
Labor Day |
Columbus Day |
Veterans Day |
Thanksgiving |
Christmas Day |
Christmas Day (Observed) |
New Year's Day (Observed) |
Veterans Day (Observed) |
Independence Day (Observed) |
Juneteenth National Independence Day |
Juneteenth National Independence Day (Observed) |
Ecommerce Forecast |
---|
ecommerce_forecast = ecommerce_prophet.predict(testing)
head_tail_vert(ecommerce_forecast, 5, title = "Ecommerce Prophet Forecast")
Ecommerce Prophet Forecast: head(5) |
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | Battle of the Boyne [Northern Ireland] | Battle of the Boyne [Northern Ireland]_lower | Battle of the Boyne [Northern Ireland]_upper | Boxing Day | Boxing Day_lower | Boxing Day_upper | Boxing Day (Observed) | Boxing Day (Observed)_lower | Boxing Day (Observed)_upper | Christmas Day | Christmas Day_lower | Christmas Day_upper | Christmas Day (Observed) | Christmas Day (Observed)_lower | Christmas Day (Observed)_upper | Easter Monday [England/Wales/Northern Ireland] | Easter Monday [England/Wales/Northern Ireland]_lower | Easter Monday [England/Wales/Northern Ireland]_upper | Good Friday | Good Friday_lower | Good Friday_upper | Late Summer Bank Holiday [England/Wales/Northern Ireland] | Late Summer Bank Holiday [England/Wales/Northern Ireland]_lower | Late Summer Bank Holiday [England/Wales/Northern Ireland]_upper | May Day | May Day_lower | May Day_upper | New Year Holiday [Scotland] | New Year Holiday [Scotland]_lower | New Year Holiday [Scotland]_upper | New Year Holiday [Scotland] (Observed) | New Year Holiday [Scotland] (Observed)_lower | New Year Holiday [Scotland] (Observed)_upper | New Year's Day | New Year's Day_lower | New Year's Day_upper | New Year's Day (Observed) | New Year's Day (Observed)_lower | New Year's Day (Observed)_upper | Spring Bank Holiday | Spring Bank Holiday_lower | Spring Bank Holiday_upper | St. Andrew's Day [Scotland] | St. Andrew's Day [Scotland]_lower | St. Andrew's Day [Scotland]_upper | St. Patrick's Day [Northern Ireland] | St. Patrick's Day [Northern Ireland]_lower | St. Patrick's Day [Northern Ireland]_upper | Summer Bank Holiday [Scotland] | Summer Bank Holiday [Scotland]_lower | Summer Bank Holiday [Scotland]_upper | Wedding of William and Catherine | Wedding of William and Catherine_lower | Wedding of William and Catherine_upper | additive_terms | additive_terms_lower | additive_terms_upper | daily | daily_lower | daily_upper | holidays | holidays_lower | holidays_upper | weekly | weekly_lower | weekly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2011-10-24 | 234.63 | -218.67 | 442.83 | 234.63 | 234.63 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -133.82 | -133.82 | -133.82 | -145.06 | -145.06 | -145.06 | 0.00 | 0.00 | 0.00 | 11.24 | 11.24 | 11.24 | 0.00 | 0.00 | 0.00 | 100.81 |
1 | 2011-10-24 | 234.63 | -213.87 | 460.57 | 234.63 | 234.63 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -133.82 | -133.82 | -133.82 | -145.06 | -145.06 | -145.06 | 0.00 | 0.00 | 0.00 | 11.24 | 11.24 | 11.24 | 0.00 | 0.00 | 0.00 | 100.81 |
2 | 2011-10-24 | 234.63 | -269.19 | 465.86 | 234.63 | 234.63 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -133.87 | -133.87 | -133.87 | -145.10 | -145.10 | -145.10 | 0.00 | 0.00 | 0.00 | 11.23 | 11.23 | 11.23 | 0.00 | 0.00 | 0.00 | 100.76 |
3 | 2011-10-24 | 234.63 | -240.91 | 434.52 | 234.63 | 234.63 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -134.24 | -134.24 | -134.24 | -145.45 | -145.45 | -145.45 | 0.00 | 0.00 | 0.00 | 11.21 | 11.21 | 11.21 | 0.00 | 0.00 | 0.00 | 100.38 |
4 | 2011-10-24 | 234.63 | -238.05 | 425.02 | 234.63 | 234.63 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -134.33 | -134.33 | -134.33 | -145.53 | -145.53 | -145.53 | 0.00 | 0.00 | 0.00 | 11.20 | 11.20 | 11.20 | 0.00 | 0.00 | 0.00 | 100.30 |
Ecommerce Prophet Forecast: tail(5) |
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | Battle of the Boyne [Northern Ireland] | Battle of the Boyne [Northern Ireland]_lower | Battle of the Boyne [Northern Ireland]_upper | Boxing Day | Boxing Day_lower | Boxing Day_upper | Boxing Day (Observed) | Boxing Day (Observed)_lower | Boxing Day (Observed)_upper | Christmas Day | Christmas Day_lower | Christmas Day_upper | Christmas Day (Observed) | Christmas Day (Observed)_lower | Christmas Day (Observed)_upper | Easter Monday [England/Wales/Northern Ireland] | Easter Monday [England/Wales/Northern Ireland]_lower | Easter Monday [England/Wales/Northern Ireland]_upper | Good Friday | Good Friday_lower | Good Friday_upper | Late Summer Bank Holiday [England/Wales/Northern Ireland] | Late Summer Bank Holiday [England/Wales/Northern Ireland]_lower | Late Summer Bank Holiday [England/Wales/Northern Ireland]_upper | May Day | May Day_lower | May Day_upper | New Year Holiday [Scotland] | New Year Holiday [Scotland]_lower | New Year Holiday [Scotland]_upper | New Year Holiday [Scotland] (Observed) | New Year Holiday [Scotland] (Observed)_lower | New Year Holiday [Scotland] (Observed)_upper | New Year's Day | New Year's Day_lower | New Year's Day_upper | New Year's Day (Observed) | New Year's Day (Observed)_lower | New Year's Day (Observed)_upper | Spring Bank Holiday | Spring Bank Holiday_lower | Spring Bank Holiday_upper | St. Andrew's Day [Scotland] | St. Andrew's Day [Scotland]_lower | St. Andrew's Day [Scotland]_upper | St. Patrick's Day [Northern Ireland] | St. Patrick's Day [Northern Ireland]_lower | St. Patrick's Day [Northern Ireland]_upper | Summer Bank Holiday [Scotland] | Summer Bank Holiday [Scotland]_lower | Summer Bank Holiday [Scotland]_upper | Wedding of William and Catherine | Wedding of William and Catherine_lower | Wedding of William and Catherine_upper | additive_terms | additive_terms_lower | additive_terms_upper | daily | daily_lower | daily_upper | holidays | holidays_lower | holidays_upper | weekly | weekly_lower | weekly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
5175 | 2011-12-09 | 237.38 | -243.97 | 460.90 | 228.75 | 245.34 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -142.20 | -142.20 | -142.20 | -146.86 | -146.86 | -146.86 | 0.00 | 0.00 | 0.00 | 4.66 | 4.66 | 4.66 | 0.00 | 0.00 | 0.00 | 95.18 |
5176 | 2011-12-09 | 237.38 | -238.42 | 455.34 | 228.75 | 245.34 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -142.11 | -142.11 | -142.11 | -146.73 | -146.73 | -146.73 | 0.00 | 0.00 | 0.00 | 4.62 | 4.62 | 4.62 | 0.00 | 0.00 | 0.00 | 95.27 |
5177 | 2011-12-09 | 237.38 | -263.67 | 422.22 | 228.74 | 245.34 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -141.84 | -141.84 | -141.84 | -146.35 | -146.35 | -146.35 | 0.00 | 0.00 | 0.00 | 4.51 | 4.51 | 4.51 | 0.00 | 0.00 | 0.00 | 95.54 |
5178 | 2011-12-09 | 237.38 | -233.43 | 448.45 | 228.74 | 245.35 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -140.97 | -140.97 | -140.97 | -145.16 | -145.16 | -145.16 | 0.00 | 0.00 | 0.00 | 4.19 | 4.19 | 4.19 | 0.00 | 0.00 | 0.00 | 96.41 |
5179 | 2011-12-09 | 237.38 | -251.93 | 441.95 | 228.74 | 245.35 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -140.92 | -140.92 | -140.92 | -145.09 | -145.09 | -145.09 | 0.00 | 0.00 | 0.00 | 4.17 | 4.17 | 4.17 | 0.00 | 0.00 | 0.00 | 96.46 |
header_text('Ecommerce Forecast')
customized_plot(model = ecommerce_prophet,
forecast = ecommerce_forecast,
logy = True)
Ecommerce Forecast |
Ecommerce: Visualizing Components |
---|
fig = ecommerce_prophet.plot_components(ecommerce_forecast)
plt.show()
Ecommerce: Weekly Actual Values vs Predicted |
---|
def compare_predictions(forecast, test_data,
start_date, end_date,
title, legend_loc = 1,
logy = False):
plt.style.use('elegant.mplstyle')
actuals = pd.DataFrame(test_data.set_index('ds').loc[start_date:
end_date].y)
predictions = pd.DataFrame(forecast.set_index('ds').loc[start_date:
end_date].yhat)
comparison = pd.concat([actuals, predictions], axis = 1)
fig, ax = plt.subplots(figsize = (20, 5))
if logy:
ax.set_yscale('log')
plt.ylabel('log values')
ax.scatter(comparison.index,
comparison.y,
color = 'yellow',
label = "Actual Values",
s = 20)
ax.scatter(comparison.index,
comparison.yhat,
color = 'deeppink',
label = 'Predicted Values',
s = 20)
plt.title(title + ' Actual Values vs Predicted');
plt.xticks(rotation = 40)
plt.legend(labels = ['Actual', 'Predicted'], loc = legend_loc)
plt.show()
compare_predictions(ecommerce_forecast, testing,
'2011-10-24', '2011-10-31', 'October 24 to 31 2011:',
logy = True)
compare_predictions(ecommerce_forecast, testing,
'2011-10-31', '2011-11-06', 'Oct 31 to Nov 06, 2011:',
logy = True)
compare_predictions(ecommerce_forecast, testing,
'2011-11-06', '2011-11-13', 'November 06 to 13 2011:',
logy = True)
compare_predictions(ecommerce_forecast, testing,
'2011-11-13', '2011-11-20', 'November 13 to 20 2011:',
logy = True)
compare_predictions(ecommerce_forecast, testing,
'2011-11-20', '2011-11-27', 'November 20 to 27 2011:',
logy = True)
compare_predictions(ecommerce_forecast, testing,
'2011-11-27', '2011-12-03', 'Nov 27 to Dec 03 2011:',
logy = True)
compare_predictions(ecommerce_forecast, testing,
'2011-12-03', '2011-12-09', 'December 03 to 09 2011:',
logy = True)
actuals = pd.DataFrame(testing.set_index('ds')).y
predictions = pd.DataFrame(ecommerce_forecast.set_index('ds')).yhat
pretty('Error Metrics: Prophet - UK Ecommerce')
pretty(np.sqrt(mse(actuals, predictions)), 'Mean Squared Error Score:')
pretty(mae(actuals, predictions), 'Mean Absolute Error Score:')
Error Metrics: Prophet - UK Ecommerce |
Mean Squared Error Score: |
962.350572 |
Mean Absolute Error Score: |
176.208444 |
ecom_predictions = pd.concat([actuals, predictions], axis = 1)
ecom_results = get_accuracy(ecom_predictions, 'yhat', 'y')
Average RMSE: 962.35 | Average sharpe ratio: 0.18 |
Average absolute accuracy: -inf% | Average relative accuracy: 99.65% |
accuracies = []
for idx, row in ecom_results.iterrows():
accuracy = min(row.y, row.yhat) / max(row.y, row.yhat) * 100
accuracies.append(accuracy)
absolute_accuracy = pd.Series(accuracies).mean()
pretty(f'{absolute_accuracy:.3f}%', 'Ecommerce: Absolute Accuracy')
Ecommerce: Absolute Accuracy |
14.819% |
Energy Consumption in the Eastern US | ||
---|---|---|
- states include: Delaware, Illinois, Indiana, Kentucky, Maryland, Michigan, New Jersey, North Carolina, Ohio, Pennsylvania, Tennessee, Virginia, West Virginia and the District of Columbia |
data = pd.read_csv('pjme.csv', index_col=[0], parse_dates=[0])
data.columns = ['energy_consumption']
df_overview(data, 'imported data')
energy_consumption | |
---|---|
datatype | float64 |
missing values | 0 |
count | 145,366.00 |
mean | 32,080.22 |
std | 6,464.01 |
min | 14,544.00 |
25% | 27,573.00 |
50% | 31,421.00 |
75% | 35,650.00 |
max | 62,009.00 |
total rows | 145,366 |
---|---|
total columns | 1 |
column names | energy_consumption |
index start | 2002-12-31 01:00:00 |
index end | 2018-01-02 00:00:00 |
total missing values | 0 |
imported data Head and Tail |
energy_consumption | |
---|---|
Datetime | |
2002-12-31 | 26,498.00 |
2002-12-31 | 25,147.00 |
2002-12-31 | 24,574.00 |
energy_consumption | |
---|---|
Datetime | |
2018-01-01 | 42,402.00 |
2018-01-01 | 40,164.00 |
2018-01-02 | 38,608.00 |
Energy Consumption: Initial Visualization | ||
---|---|---|
fix, axes = plt.subplots(5, 1, figsize = (15, 17))
data.plot(ax = axes[0], title = 'PJME: Energy Consumption Hourly', ms=.5, c = rc(clr))
data.resample('D').mean().plot(ax = axes[1], title = 'PJME: Daily Avg', c = rc(clr))
data.resample('W').mean().plot(ax = axes[2], title = 'PJME: Weekly Avg', c = rc(clr))
data.resample('M').mean().plot(ax = axes[3], title = 'PJME: Monthly Avg', c = rc(clr))
data.resample('A').mean().plot(ax = axes[4], title = 'PJME: Yearly Avg', c = rc(clr))
axes[0].legend().remove(); axes[1].legend().remove();
axes[2].legend().remove(); axes[3].legend().remove();
axes[4].legend().remove();
plt.tight_layout()
plt.show()
Featurized Data |
---|
featurized = featurize_dt_index(data)
df_overview(featurized, 'featurized_data')
energy_consumption | hour | weekday | weekday_name | month | month_name | quarter | year | week_of_year | day_of_year | date_offset | season | time_of_day | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
datatype | float64 | int64 | int64 | category | int64 | category | int64 | int64 | float64 | int64 | int64 | category | category |
missing values | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 408 | 0 |
count | 145,366.00 | 145,366.00 | 145,366.00 | nan | 145,366.00 | nan | 145,366.00 | 145,366.00 | 145,366.00 | 145,366.00 | 145,366.00 | nan | nan |
mean | 32,080.22 | 11.50 | 3.00 | nan | 6.44 | nan | 2.48 | 2,009.80 | 26.22 | 180.46 | 624.66 | nan | nan |
std | 6,464.01 | 6.92 | 2.00 | nan | 3.44 | nan | 1.11 | 4.79 | 15.02 | 105.14 | 380.84 | nan | nan |
min | 14,544.00 | 0.00 | 0.00 | nan | 1.00 | nan | 1.00 | 2,002.00 | 1.00 | 1.00 | 0.00 | nan | nan |
25% | 27,573.00 | 6.00 | 1.00 | nan | 3.00 | nan | 1.00 | 2,006.00 | 13.00 | 90.00 | 297.00 | nan | nan |
50% | 31,421.00 | 12.00 | 3.00 | nan | 6.00 | nan | 2.00 | 2,010.00 | 26.00 | 179.00 | 596.00 | nan | nan |
75% | 35,650.00 | 18.00 | 5.00 | nan | 9.00 | nan | 3.00 | 2,014.00 | 39.00 | 271.00 | 900.00 | nan | nan |
max | 62,009.00 | 23.00 | 6.00 | nan | 12.00 | nan | 4.00 | 2,018.00 | 53.00 | 366.00 | 1,299.00 | nan | nan |
total rows | 145,366 |
---|---|
total columns | 13 |
column names | energy_consumption, hour, weekday, weekday_name, month, month_name, quarter, year, week_of_year, day_of_year, date_offset, season, time_of_day |
index start | 2002-12-31 01:00:00 |
index end | 2018-01-02 00:00:00 |
total missing values | 408 |
featurized_data Head and Tail |
energy_consumption | hour | weekday | weekday_name | month | month_name | quarter | year | week_of_year | day_of_year | date_offset | season | time_of_day | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Datetime | |||||||||||||
2002-12-31 | 26,498.00 | 1 | 1 | Tuesday | 12 | December | 4 | 2,002 | 1.00 | 365 | 911 | Winter | midnight |
2002-12-31 | 25,147.00 | 2 | 1 | Tuesday | 12 | December | 4 | 2,002 | 1.00 | 365 | 911 | Winter | midnight |
2002-12-31 | 24,574.00 | 3 | 1 | Tuesday | 12 | December | 4 | 2,002 | 1.00 | 365 | 911 | Winter | midnight |
energy_consumption | hour | weekday | weekday_name | month | month_name | quarter | year | week_of_year | day_of_year | date_offset | season | time_of_day | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Datetime | |||||||||||||
2018-01-01 | 42,402.00 | 22 | 0 | Monday | 1 | January | 1 | 2,018 | 1.00 | 1 | 1,081 | Winter | night |
2018-01-01 | 40,164.00 | 23 | 0 | Monday | 1 | January | 1 | 2,018 | 1.00 | 1 | 1,081 | Winter | night |
2018-01-02 | 38,608.00 | 0 | 1 | Tuesday | 1 | January | 1 | 2,018 | 1.00 | 2 | 1,082 | Winter | midnight |
Feature Visualization: Importance of Trends | ||
---|---|---|
def boxplot_correlation2(df, feature_x, feature_y, order=None, palette=None):
import seaborn as sns
fig, ax = plt.subplots(figsize=(17, 5), facecolor= '#287A70')
ax.set_facecolor('#1E232E')
sns.boxplot(data=df,
x=feature_x,
y=feature_y,
order=order,
palette=palette)
x_name = str(df[feature_x].name)
y_name = str(df[feature_y].name)
ax.grid()
plt.xlabel(x_name, color='white', fontsize=15)
plt.ylabel(y_name, color='white', fontsize=15)
plt.xticks(color='white');
plt.yticks(color='white');
plt.title(f'Feature Correlation: {x_name.capitalize()} - {y_name.capitalize()}',
fontsize=20, pad=20, color='white');
plt.show()
boxplot_correlation2(featurized, 'year', 'energy_consumption', palette='rainbow')
boxplot_correlation2(featurized, 'quarter', 'energy_consumption', palette='rainbow')
boxplot_correlation2(featurized, 'month', 'energy_consumption', palette='rainbow')
boxplot_correlation2(featurized, 'week_of_year', 'energy_consumption', palette='rainbow')
order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
boxplot_correlation2(featurized, 'weekday_name', 'energy_consumption',
order = order, palette='rainbow')
order = ['midnight', 'early_morning', 'late_morning', 'afternoon', 'evening', 'night']
boxplot_correlation2(featurized, 'time_of_day', 'energy_consumption',
order = order, palette='rainbow')
boxplot_correlation2(featurized, 'hour', 'energy_consumption', palette='rainbow')
Prophet Model: Energy Consumption | ||
---|---|---|
- train-test split |
split_point = '2015-01-01'
train_data = data.loc[data.index <= split_point].copy()
test_data = data.loc[data.index > split_point].copy()
plt.style.use('elegant.mplstyle')
fig, ax = plt.subplots(figsize = (17, 6))
train_data.plot(ax = ax, c = rc(clr), ms = 4, style='.');
test_data.plot(ax = ax, c = rc(clr), ms = 4, style='.');
plt.title('Train-Test Split');
plt.legend(labels = ['training', 'testing']);
plt.show()
Prophet data prep |
---|
energy_training = train_data.reset_index()\
.rename(columns = {'Datetime': 'ds',
'energy_consumption': 'y'})
energy_testing = test_data.reset_index()\
.rename(columns = {'Datetime': 'ds',
'energy_consumption': 'y'})
head_tail_horz(energy_training, 5, 'Training Data')
Training Data |
ds | y | |
---|---|---|
0 | 2002-12-31 | 26,498.00 |
1 | 2002-12-31 | 25,147.00 |
2 | 2002-12-31 | 24,574.00 |
3 | 2002-12-31 | 24,393.00 |
4 | 2002-12-31 | 24,860.00 |
ds | y | |
---|---|---|
113922 | 2014-01-01 | 36,193.00 |
113923 | 2014-01-01 | 35,601.00 |
113924 | 2014-01-01 | 34,242.00 |
113925 | 2014-01-01 | 32,215.00 |
113926 | 2014-01-02 | 30,159.00 |
Incorporating holidays |
---|
energy_prophet = Prophet()
energy_prophet.add_country_holidays(country_name='US')
energy_prophet = energy_prophet.fit(energy_training)
21:39:08 - cmdstanpy - INFO - Chain [1] start processing 21:39:55 - cmdstanpy - INFO - Chain [1] done processing
pretty('US Holidays from Prophet')
pd.DataFrame(energy_prophet.train_holiday_names).style\
.hide(axis = 'index')\
.hide(axis = 'columns')
US Holidays from Prophet |
New Year's Day |
Martin Luther King Jr. Day |
Washington's Birthday |
Memorial Day |
Independence Day |
Labor Day |
Columbus Day |
Veterans Day |
Thanksgiving |
Christmas Day |
New Year's Day (Observed) |
Independence Day (Observed) |
Christmas Day (Observed) |
Veterans Day (Observed) |
Energy Consumption: Prophet Model Predictions | ||
---|---|---|
energy_forecast = energy_prophet.predict(df = energy_testing)
head_tail_vert(energy_forecast, 5, 'PMJ Energy: Forecast')
PMJ Energy: Forecast: head(5) |
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | Christmas Day | Christmas Day_lower | Christmas Day_upper | Christmas Day (Observed) | Christmas Day (Observed)_lower | Christmas Day (Observed)_upper | Columbus Day | Columbus Day_lower | Columbus Day_upper | Independence Day | Independence Day_lower | Independence Day_upper | Independence Day (Observed) | Independence Day (Observed)_lower | Independence Day (Observed)_upper | Labor Day | Labor Day_lower | Labor Day_upper | Martin Luther King Jr. Day | Martin Luther King Jr. Day_lower | Martin Luther King Jr. Day_upper | Memorial Day | Memorial Day_lower | Memorial Day_upper | New Year's Day | New Year's Day_lower | New Year's Day_upper | New Year's Day (Observed) | New Year's Day (Observed)_lower | New Year's Day (Observed)_upper | Thanksgiving | Thanksgiving_lower | Thanksgiving_upper | Veterans Day | Veterans Day_lower | Veterans Day_upper | Veterans Day (Observed) | Veterans Day (Observed)_lower | Veterans Day (Observed)_upper | Washington's Birthday | Washington's Birthday_lower | Washington's Birthday_upper | additive_terms | additive_terms_lower | additive_terms_upper | daily | daily_lower | daily_upper | holidays | holidays_lower | holidays_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2015-01-01 | 31,266.52 | 21,100.54 | 29,951.60 | 31,266.52 | 31,266.52 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -3,057.33 | -3,057.33 | -3,057.33 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -5,514.97 | -5,514.97 | -5,514.97 | -4,430.34 | -4,430.34 | -4,430.34 | -3,057.33 | -3,057.33 | -3,057.33 | 1,317.81 | 1,317.81 | 1,317.81 | 654.90 | 654.90 | 654.90 | 0.00 | 0.00 | 0.00 | 25,751.55 |
1 | 2015-01-01 | 31,266.48 | 19,960.18 | 28,759.61 | 31,266.48 | 31,266.48 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -3,057.33 | -3,057.33 | -3,057.33 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -7,016.75 | -7,016.75 | -7,016.75 | -5,927.22 | -5,927.22 | -5,927.22 | -3,057.33 | -3,057.33 | -3,057.33 | 1,312.01 | 1,312.01 | 1,312.01 | 655.80 | 655.80 | 655.80 | 0.00 | 0.00 | 0.00 | 24,249.74 |
2 | 2015-01-01 | 31,266.45 | 18,902.56 | 27,809.64 | 31,266.45 | 31,266.45 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -3,057.33 | -3,057.33 | -3,057.33 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -7,886.21 | -7,886.21 | -7,886.21 | -6,790.28 | -6,790.28 | -6,790.28 | -3,057.33 | -3,057.33 | -3,057.33 | 1,304.67 | 1,304.67 | 1,304.67 | 656.73 | 656.73 | 656.73 | 0.00 | 0.00 | 0.00 | 23,380.24 |
3 | 2015-01-01 | 31,266.41 | 18,965.58 | 27,544.12 | 31,266.41 | 31,266.41 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -3,057.33 | -3,057.33 | -3,057.33 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -8,025.93 | -8,025.93 | -8,025.93 | -6,922.19 | -6,922.19 | -6,922.19 | -3,057.33 | -3,057.33 | -3,057.33 | 1,295.91 | 1,295.91 | 1,295.91 | 657.69 | 657.69 | 657.69 | 0.00 | 0.00 | 0.00 | 23,240.49 |
4 | 2015-01-01 | 31,266.38 | 19,467.08 | 28,517.51 | 31,266.38 | 31,266.38 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -3,057.33 | -3,057.33 | -3,057.33 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | -7,350.14 | -7,350.14 | -7,350.14 | -6,237.33 | -6,237.33 | -6,237.33 | -3,057.33 | -3,057.33 | -3,057.33 | 1,285.83 | 1,285.83 | 1,285.83 | 658.68 | 658.68 | 658.68 | 0.00 | 0.00 | 0.00 | 23,916.23 |
PMJ Energy: Forecast: tail(5) |
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | Christmas Day | Christmas Day_lower | Christmas Day_upper | Christmas Day (Observed) | Christmas Day (Observed)_lower | Christmas Day (Observed)_upper | Columbus Day | Columbus Day_lower | Columbus Day_upper | Independence Day | Independence Day_lower | Independence Day_upper | Independence Day (Observed) | Independence Day (Observed)_lower | Independence Day (Observed)_upper | Labor Day | Labor Day_lower | Labor Day_upper | Martin Luther King Jr. Day | Martin Luther King Jr. Day_lower | Martin Luther King Jr. Day_upper | Memorial Day | Memorial Day_lower | Memorial Day_upper | New Year's Day | New Year's Day_lower | New Year's Day_upper | New Year's Day (Observed) | New Year's Day (Observed)_lower | New Year's Day (Observed)_upper | Thanksgiving | Thanksgiving_lower | Thanksgiving_upper | Veterans Day | Veterans Day_lower | Veterans Day_upper | Veterans Day (Observed) | Veterans Day (Observed)_lower | Veterans Day (Observed)_upper | Washington's Birthday | Washington's Birthday_lower | Washington's Birthday_upper | additive_terms | additive_terms_lower | additive_terms_upper | daily | daily_lower | daily_upper | holidays | holidays_lower | holidays_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
31434 | 2018-08-02 | 30,132.09 | -3,559.54 | 81,500.62 | -14,521.40 | 70,750.07 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 11,265.73 | 11,265.73 | 11,265.73 | 4,451.12 | 4,451.12 | 4,451.12 | 0.00 | 0.00 | 0.00 | 1,067.63 | 1,067.63 | 1,067.63 | 5,746.99 | 5,746.99 | 5,746.99 | 0.00 | 0.00 | 0.00 | 41,397.82 |
31435 | 2018-08-02 | 30,132.05 | -3,653.66 | 80,128.07 | -14,524.12 | 70,750.16 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 10,490.17 | 10,490.17 | 10,490.17 | 3,695.91 | 3,695.91 | 3,695.91 | 0.00 | 0.00 | 0.00 | 1,052.16 | 1,052.16 | 1,052.16 | 5,742.09 | 5,742.09 | 5,742.09 | 0.00 | 0.00 | 0.00 | 40,622.22 |
31436 | 2018-08-02 | 30,132.01 | -5,845.96 | 80,022.23 | -14,526.84 | 70,750.25 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 8,858.04 | 8,858.04 | 8,858.04 | 2,084.42 | 2,084.42 | 2,084.42 | 0.00 | 0.00 | 0.00 | 1,036.43 | 1,036.43 | 1,036.43 | 5,737.19 | 5,737.19 | 5,737.19 | 0.00 | 0.00 | 0.00 | 38,990.06 |
31437 | 2018-08-02 | 30,131.98 | -8,894.63 | 77,498.15 | -14,529.56 | 70,750.34 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 6,647.30 | 6,647.30 | 6,647.30 | -105.26 | -105.26 | -105.26 | 0.00 | 0.00 | 0.00 | 1,020.27 | 1,020.27 | 1,020.27 | 5,732.29 | 5,732.29 | 5,732.29 | 0.00 | 0.00 | 0.00 | 36,779.28 |
31438 | 2018-08-03 | 30,131.94 | -12,106.01 | 75,678.56 | -14,532.28 | 70,750.44 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 4,318.70 | 4,318.70 | 4,318.70 | -2,412.16 | -2,412.16 | -2,412.16 | 0.00 | 0.00 | 0.00 | 1,003.47 | 1,003.47 | 1,003.47 | 5,727.39 | 5,727.39 | 5,727.39 | 0.00 | 0.00 | 0.00 | 34,450.64 |
header_text('Forecast: PMJ Energy Data')
customized_plot(model = energy_prophet,
forecast = energy_forecast)
Forecast: PMJ Energy Data |
Visualizing Components |
---|
matplotlib.rcParams.update(matplotlib.rcParamsDefault)
fig = energy_prophet.plot_components(energy_forecast)
plt.show()
Energy Consumption: Results Visualized | ||
---|---|---|
Monthly Comparison: Earliest Predictions |
---|
compare_predictions(energy_forecast, energy_testing,
'2015-01-01', '2015-02-01', 'January 2015:', legend_loc = 2)
compare_predictions(energy_forecast, energy_testing,
'2015-02-01', '2015-03-01', 'February 2015:', legend_loc = 2)
compare_predictions(energy_forecast, energy_testing,
'2015-03-01', '2015-04-01', 'March 2015:')
compare_predictions(energy_forecast, energy_testing,
'2015-04-01', '2015-05-01', 'April 2015:', legend_loc = 4)
Weekly Comparison: Earliest Predictions |
---|
compare_predictions(energy_forecast, energy_testing,
'2015-01-01', '2015-01-08', 'January 2015 Week 1:', legend_loc = 2)
compare_predictions(energy_forecast, energy_testing,
'2015-01-08', '2015-01-15', 'January 2015 Week 2:', legend_loc = 4)
compare_predictions(energy_forecast, energy_testing,
'2015-01-15', '2015-01-22', 'January 2015 Week 3:', legend_loc = 4)
compare_predictions(energy_forecast, energy_testing,
'2015-01-22', '2015-01-29', 'January 2015 Week 4:', legend_loc = 4)
Monthly Comparison (incorporating holidays): Latest Predictions |
---|
compare_predictions(energy_forecast, energy_testing,
'2018-04-01', '2018-05-01', 'April 2018:')
compare_predictions(energy_forecast, energy_testing,
'2018-05-01', '2018-06-01', 'May 2018:', legend_loc = 2)
compare_predictions(energy_forecast, energy_testing,
'2018-06-01', '2018-07-01', 'June 2018:')
compare_predictions(energy_forecast, energy_testing,
'2018-07-01', '2018-08-01', 'July 2018:', legend_loc = 4)
Weekly Comparison (incorporating holidays): Latest Predictions |
---|
compare_predictions(energy_forecast, energy_testing,
'2018-07-01', '2018-07-08', 'July 2018 Week 1:', legend_loc = 2)
compare_predictions(energy_forecast, energy_testing,
'2018-07-08', '2018-07-15', 'July 2018 Week 2:', legend_loc = 4)
compare_predictions(energy_forecast, energy_testing,
'2018-07-15', '2018-07-22', 'July 2018 Week 3:', legend_loc = 4)
compare_predictions(energy_forecast, energy_testing,
'2018-07-22', '2018-07-29', 'July 2018 Week 4:', legend_loc = 4)
Error Metrics: Prophet - Eastern US Energy Consumption |
---|
actual_values = energy_testing.y
predictions = energy_forecast.yhat
pretty(np.sqrt(mse(actual_values, predictions)), 'Mean Squared Error Score:')
pretty(mae(actual_values, predictions), 'Mean Absolute Error Score:')
pretty(f'{mape(actual_values, predictions)*100:.3f}%',
'Mean Absolute Percenage Error:')
Mean Squared Error Score: |
6634.361847 |
Mean Absolute Error Score: |
5197.493614 |
Mean Absolute Percenage Error: |
16.566% |
Future Predictions |
---|
# retraining model on entirety of the data |
---|
data.reset_index(inplace = True)
data.columns = ['ds', 'y']
future_model = Prophet()
future_model = future_model.fit(data)
19:14:30 - cmdstanpy - INFO - Chain [1] start processing 19:15:10 - cmdstanpy - INFO - Chain [1] done processing
future = future_model.make_future_dataframe(periods = 365*24,
freq = 'h',
include_history = False)
future_forecast = future_model.predict(future)
head_tail_vert(future_forecast, 5, 'Future Prediction Forecast')
Future Prediction Forecast: head(5) |
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | daily | daily_lower | daily_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2018-08-03 | 30,824.25 | 28,391.79 | 37,699.65 | 30,824.25 | 30,824.25 | 2,392.84 | 2,392.84 | 2,392.84 | -4,336.67 | -4,336.67 | -4,336.67 | 944.13 | 944.13 | 944.13 | 5,785.38 | 5,785.38 | 5,785.38 | 0.00 | 0.00 | 0.00 | 33,217.09 |
1 | 2018-08-03 | 30,824.22 | 26,960.72 | 36,568.21 | 30,824.22 | 30,824.22 | 912.37 | 912.37 | 912.37 | -5,801.94 | -5,801.94 | -5,801.94 | 933.11 | 933.11 | 933.11 | 5,781.20 | 5,781.20 | 5,781.20 | 0.00 | 0.00 | 0.00 | 31,736.59 |
2 | 2018-08-03 | 30,824.20 | 26,282.28 | 35,536.65 | 30,824.20 | 30,824.20 | 47.62 | 47.62 | 47.62 | -6,649.93 | -6,649.93 | -6,649.93 | 920.52 | 920.52 | 920.52 | 5,777.02 | 5,777.02 | 5,777.02 | 0.00 | 0.00 | 0.00 | 30,871.81 |
3 | 2018-08-03 | 30,824.17 | 26,594.54 | 35,221.46 | 30,824.17 | 30,824.17 | -96.59 | -96.59 | -96.59 | -6,775.47 | -6,775.47 | -6,775.47 | 906.04 | 906.04 | 906.04 | 5,772.84 | 5,772.84 | 5,772.84 | 0.00 | 0.00 | 0.00 | 30,727.58 |
4 | 2018-08-03 | 30,824.15 | 26,525.49 | 36,453.54 | 30,824.15 | 30,824.15 | 567.80 | 567.80 | 567.80 | -6,090.15 | -6,090.15 | -6,090.15 | 889.29 | 889.29 | 889.29 | 5,768.66 | 5,768.66 | 5,768.66 | 0.00 | 0.00 | 0.00 | 31,391.95 |
Future Prediction Forecast: tail(5) |
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | daily | daily_lower | daily_upper | weekly | weekly_lower | weekly_upper | yearly | yearly_lower | yearly_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
8755 | 2019-08-02 | 30,604.14 | 35,124.95 | 46,663.70 | 27,527.92 | 33,577.84 | 10,391.84 | 10,391.84 | 10,391.84 | 4,425.20 | 4,425.20 | 4,425.20 | 135.17 | 135.17 | 135.17 | 5,831.48 | 5,831.48 | 5,831.48 | 0.00 | 0.00 | 0.00 | 40,995.98 |
8756 | 2019-08-02 | 30,604.12 | 34,371.74 | 46,265.86 | 27,527.75 | 33,578.28 | 9,517.51 | 9,517.51 | 9,517.51 | 3,649.72 | 3,649.72 | 3,649.72 | 40.51 | 40.51 | 40.51 | 5,827.28 | 5,827.28 | 5,827.28 | 0.00 | 0.00 | 0.00 | 40,121.62 |
8757 | 2019-08-02 | 30,604.09 | 32,580.06 | 43,727.23 | 27,526.56 | 33,578.72 | 7,805.73 | 7,805.73 | 7,805.73 | 2,042.67 | 2,042.67 | 2,042.67 | -60.02 | -60.02 | -60.02 | 5,823.09 | 5,823.09 | 5,823.09 | 0.00 | 0.00 | 0.00 | 38,409.83 |
8758 | 2019-08-02 | 30,604.07 | 30,965.24 | 42,377.90 | 27,525.22 | 33,579.16 | 5,540.35 | 5,540.35 | 5,540.35 | -112.31 | -112.31 | -112.31 | -166.23 | -166.23 | -166.23 | 5,818.89 | 5,818.89 | 5,818.89 | 0.00 | 0.00 | 0.00 | 36,144.42 |
8759 | 2019-08-03 | 30,604.04 | 28,106.42 | 39,401.62 | 27,523.87 | 33,579.59 | 3,169.70 | 3,169.70 | 3,169.70 | -2,367.12 | -2,367.12 | -2,367.12 | -277.88 | -277.88 | -277.88 | 5,814.70 | 5,814.70 | 5,814.70 | 0.00 | 0.00 | 0.00 | 33,773.74 |
Future Predictions Compared with Previous Year |
---|
plt.style.use('elegant.mplstyle')
fig, ax = plt.subplots(figsize = (20, 6))
future_forecast.plot(x = 'ds',
y = 'yhat',
c='cyan',
ax = ax,
title = 'Future Energy Consumption Predictions');
plt.xlabel('Prediction Timeline')
plt.ylabel('Predicted Energy Consumption')
plt.show()
fig, ax = plt.subplots(figsize = (20, 6))
comparison = data.set_index('ds').loc['2017-08-03 01:00:00':'2018-08-03 00:00:00' ]
comparison.plot(c='deeppink',
ax = ax,
title = 'Actual Energy Consumption One Year Prior');
plt.xlabel('One Year Before Predictions')
plt.ylabel("Previous Year's Energy Consumption")
plt.show()