Skip to content

Commit

Permalink
Merge pull request #2 from rayz90/master
Browse files Browse the repository at this point in the history
Fix compatibility with Prophet 0.2
  • Loading branch information
CollinRooney12 authored Dec 14, 2017
2 parents 6df5715 + ee00c43 commit b4e7fb7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
11 changes: 7 additions & 4 deletions htsprophet/fitForecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

#%%
def fitForecast(y, h, sumMat, nodes, method, freq, include_history, cap, capF, changepoints, n_changepoints, \
yearly_seasonality, weekly_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
changepoint_prior_scale, mcmc_samples, interval_width, uncertainty_samples, boxcoxT, skipFitting):
yearly_seasonality, weekly_seasonality, daily_seasonality, holidays, seasonality_prior_scale, \
holidays_prior_scale, changepoint_prior_scale, mcmc_samples, interval_width, uncertainty_samples, \
boxcoxT, skipFitting):

forecastsDict = {}
mse = {}
Expand Down Expand Up @@ -70,11 +71,11 @@ def fitForecast(y, h, sumMat, nodes, method, freq, include_history, cap, capF, c
nodeToForecast = nodeToForecast.rename(columns = {nodeToForecast.columns[1] : 'y'})
if capF is None:
growth = 'linear'
m = Prophet(growth, changepoints1, n_changepoints1, yearly_seasonality, weekly_seasonality, holidays, seasonality_prior_scale, \
m = Prophet(growth, changepoints1, n_changepoints1, yearly_seasonality, weekly_seasonality, daily_seasonality, holidays, seasonality_prior_scale, \
holidays_prior_scale, changepoint_prior_scale, mcmc_samples, interval_width, uncertainty_samples)
else:
growth = 'logistic'
m = Prophet(growth, changepoints, n_changepoints, yearly_seasonality, weekly_seasonality, holidays, seasonality_prior_scale, \
m = Prophet(growth, changepoints, n_changepoints, yearly_seasonality, weekly_seasonality, daily_seasonality, holidays, seasonality_prior_scale, \
holidays_prior_scale, changepoint_prior_scale, mcmc_samples, interval_width, uncertainty_samples)
nodeToForecast['cap'] = cap1
m.fit(nodeToForecast)
Expand All @@ -97,6 +98,8 @@ def fitForecast(y, h, sumMat, nodes, method, freq, include_history, cap, capF, c
forecastsDict[node].trend = inv_boxcox(forecastsDict[node].trend, boxcoxT[node])
if "seasonal" in forecastsDict[node].columns.tolist():
forecastsDict[node].seasonal = inv_boxcox(forecastsDict[node].seasonal, boxcoxT[node])
if "daily" in forecastsDict[node].columns.tolist():
forecastsDict[node].daily = inv_boxcox(forecastsDict[node].daily, boxcoxT[node])
if "weekly" in forecastsDict[node].columns.tolist():
forecastsDict[node].weekly = inv_boxcox(forecastsDict[node].weekly, boxcoxT[node])
if "yearly" in forecastsDict[node].columns.tolist():
Expand Down
22 changes: 11 additions & 11 deletions htsprophet/hts.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#%%
def hts(y, h = 1, nodes = [[2]], method='OLS', freq = 'D', transform = None, include_history = True, cap = None, capF = None, changepoints = None, \
n_changepoints = 25, yearly_seasonality = 'auto', weekly_seasonality = 'auto', holidays = None, seasonality_prior_scale = 10.0, \
n_changepoints = 25, yearly_seasonality = 'auto', weekly_seasonality = 'auto', daily_seasonality = 'auto', holidays = None, seasonality_prior_scale = 10.0, \
holidays_prior_scale = 10.0, changepoint_prior_scale = 0.05, mcmc_samples = 0, interval_width = 0.80, uncertainty_samples = 0, skipFitting = False, numThreads = 0):
'''
Parameters
Expand Down Expand Up @@ -211,32 +211,32 @@ def SummingMat(nodes):
if numThreads != 0:
pool = ThreadPool(numThreads)
results = pool.starmap(fitForecast, zip([y.iloc[trainIndex, :]]*7, [len(testIndex)]*7, [sumMat]*7, [nodes]*7, methodList, [freq]*7, [include_history]*7, [cap]*7, [capF]*7, [changepoints]*7, [n_changepoints]*7, \
[yearly_seasonality]*7, [weekly_seasonality]*7, [holidays]*7, [seasonality_prior_scale]*7, [holidays_prior_scale]*7,\
[yearly_seasonality]*7, [weekly_seasonality]*7, [daily_seasonality]*7, [holidays]*7, [seasonality_prior_scale]*7, [holidays_prior_scale]*7,\
[changepoint_prior_scale]*7, [mcmc_samples]*7, [interval_width]*7, [uncertainty_samples]*7, [boxcoxT]*7, [skipFitting]*7))
pool.close()
pool.join()
ynew1, ynew2, ynew3, ynew4, ynew5, ynew6, ynew7 = results
else:
ynew1 = fitForecast(y.iloc[trainIndex, :], len(testIndex), sumMat, nodes, methodList[0], freq, include_history, cap, capF, changepoints, n_changepoints, \
yearly_seasonality, weekly_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
yearly_seasonality, weekly_seasonality, daily_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
changepoint_prior_scale, mcmc_samples, interval_width, uncertainty_samples, boxcoxT, skipFitting)
ynew2 = fitForecast(y.iloc[trainIndex, :], len(testIndex), sumMat, nodes, methodList[1], freq, include_history, cap, capF, changepoints, n_changepoints, \
yearly_seasonality, weekly_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
yearly_seasonality, weekly_seasonality, daily_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
changepoint_prior_scale, mcmc_samples, interval_width, uncertainty_samples, boxcoxT, skipFitting)
ynew3 = fitForecast(y.iloc[trainIndex, :], len(testIndex), sumMat, nodes, methodList[2], freq, include_history, cap, capF, changepoints, n_changepoints, \
yearly_seasonality, weekly_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
yearly_seasonality, weekly_seasonality, daily_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
changepoint_prior_scale, mcmc_samples, interval_width, uncertainty_samples, boxcoxT, skipFitting)
ynew4 = fitForecast(y.iloc[trainIndex, :], len(testIndex), sumMat, nodes, methodList[3], freq, include_history, cap, capF, changepoints, n_changepoints, \
yearly_seasonality, weekly_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
yearly_seasonality, weekly_seasonality, daily_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
changepoint_prior_scale, mcmc_samples, interval_width, uncertainty_samples, boxcoxT, skipFitting)
ynew5 = fitForecast(y.iloc[trainIndex, :], len(testIndex), sumMat, nodes, methodList[4], freq, include_history, cap, capF, changepoints, n_changepoints, \
yearly_seasonality, weekly_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
yearly_seasonality, weekly_seasonality, daily_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
changepoint_prior_scale, mcmc_samples, interval_width, uncertainty_samples, boxcoxT, skipFitting)
ynew6 = fitForecast(y.iloc[trainIndex, :], len(testIndex), sumMat, nodes, methodList[5], freq, include_history, cap, capF, changepoints, n_changepoints, \
yearly_seasonality, weekly_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
yearly_seasonality, weekly_seasonality, daily_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
changepoint_prior_scale, mcmc_samples, interval_width, uncertainty_samples, boxcoxT, skipFitting)
ynew7 = fitForecast(y.iloc[trainIndex, :], len(testIndex), sumMat, nodes, methodList[6], freq, include_history, cap, capF, changepoints, n_changepoints, \
yearly_seasonality, weekly_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
yearly_seasonality, weekly_seasonality, daily_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
changepoint_prior_scale, mcmc_samples, interval_width, uncertainty_samples, boxcoxT, skipFitting)
#
for key in ynew1.keys():
Expand All @@ -253,7 +253,7 @@ def SummingMat(nodes):
choices = [np.mean(MASE1), np.mean(MASE2), np.mean(MASE3), np.mean(MASE4), np.mean(MASE5), np.mean(MASE6), np.mean(MASE7)]
choice = methodList[choices.index(min(choices))]
ynew = fitForecast(y, h, sumMat, nodes, choice, freq, include_history, cap, capF, changepoints, n_changepoints, \
yearly_seasonality, weekly_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
yearly_seasonality, weekly_seasonality, daily_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
changepoint_prior_scale, mcmc_samples, interval_width, uncertainty_samples, boxcoxT, skipFitting)
print(choice)

Expand All @@ -268,7 +268,7 @@ def SummingMat(nodes):
i += 1
sumMat = SummingMat(nodes)
ynew = fitForecast(y, h, sumMat, nodes, method, freq, include_history, cap, capF, changepoints, n_changepoints, \
yearly_seasonality, weekly_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
yearly_seasonality, weekly_seasonality, daily_seasonality, holidays, seasonality_prior_scale, holidays_prior_scale,\
changepoint_prior_scale, mcmc_samples, interval_width, uncertainty_samples, boxcoxT, skipFitting)
##
# Inverse boxcox the data
Expand Down
6 changes: 3 additions & 3 deletions htsprophet/runHTS.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@
# This output is a dictionary of dataframes, so you can do any further analysis that you may want. It also allows you to plot the forecasts.
# Some functions I've made are: (1 means I'm plotting the total node)
##
plotNode(myDict, 1, h = 52, xlabel = "Week", ylabel = "Number of Sessions")
plotChild(myDict, 1, nodes, h = 52, xlabel = "Week", ylabel = "Number of Sessions", legend = ["Total","Air","Land","Sea"])
plotNodeComponents(myDict, column = 1, holidays = holidays)
plotNode(myDict, "Total", h = 52, xlabel = "Week", ylabel = "Number of Sessions")
plotChild(myDict, "Total", h = 52, xlabel = "Week", ylabel = "Number of Sessions")
plotNodeComponents(myDict, column = "Total", holidays = holidays)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup

setup(name = 'htsprophet',
version = '0.1.0',
version = '0.1.1',
description = "Creates Hierarchical Time Series Forecasts with Facebook's Prophet tool",
url = "https://github.com/CollinRooney12/htsprophet",
author = "Collin Rooney",
Expand All @@ -16,6 +16,6 @@
'matplotlib',
'pandas>=0.18.1',
'numpy',
'fbprophet',
'fbprophet>=0.2.0',
'scikit-learn>=0.18'],
)

0 comments on commit b4e7fb7

Please sign in to comment.