Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Returns:
pandas.DataFrame: collection of data + forecast info
['date', 'avgPrice', 'yhat', 'yhat_low', 'yhat_high', 'prediction']
"""
data['date'] = pd.to_datetime(data['date'])
filter_date = data['date'].max()
## Build DataFrame ##
predict_df = pd.DataFrame()
predict_df['ds'] = data['date']
predict_df['y'] = data['avgPrice']
## Run prediction ##
# https://facebookincubator.github.io/prophet/docs/quick_start.html#python-api
model = Prophet()
model.fit(predict_df)
future = model.make_future_dataframe(periods=forecast_range)
tst = model.predict(future)
predict_df = pd.merge(
predict_df, model.predict(future),
on='ds',
how='right'
)
## Build report for endpoint ##
report = pd.DataFrame()
report['date'] = pd.to_datetime(predict_df['ds'], format='%Y-%m-%d')
report['avgPrice'] = predict_df['y']
report['yhat'] = predict_df['yhat']
report['yhat_low'] = predict_df['yhat_lower']
# If the input data frame contains less than 2 non-Null rows, prediction is not possible
if len(self.input_df) - self.input_df.y.isnull().sum() <= 2:
if self.debug:
self._print_log(3)
# A series of null values is returned to avoid an error in Qlik
return pd.Series([np.NaN for y in range(self.request_row_count)])
# Instantiate a Prophet object and fit the input data frame:
if len(self.prophet_kwargs) > 0:
self.model = Prophet(**self.prophet_kwargs)
else:
self.model = Prophet()
# Add custom seasonalities if defined in the arguments
if self.name is not None and len(self.add_seasonality_kwargs) > 0:
self.model.add_seasonality(**self.add_seasonality_kwargs)
# Add additional regressors if defined in the arguments
if self.has_regressors:
i=0
for regressor in self.regressors_df.columns:
self.model.add_regressor(regressor, **self.regressor_kwargs[i])
i+=1
self.model.fit(self.input_df, **self.fit_kwargs)
# Create a data frame for future values
self.future_df = self.model.make_future_dataframe(**self.make_kwargs)
'X': seasonal_features,
'sigmas': prior_scales,
'tau': self.changepoint_prior_scale,
'trend_indicator': int(self.growth == 'logistic'),
's_a': component_cols['additive_terms'],
's_m': component_cols['multiplicative_terms'],
}
if self.growth == 'linear':
dat['cap'] = np.zeros(self.history.shape[0])
kinit = self.linear_growth_init(history)
else:
dat['cap'] = history['cap_scaled']
kinit = self.logistic_growth_init(history)
model = prophet_stan_model
def stan_init():
return {
'k': kinit[0],
'm': kinit[1],
'delta': np.zeros(len(self.changepoints_t)),
'beta': np.zeros(seasonal_features.shape[1]),
'sigma_obs': 1,
}
if (
(history['y'].min() == history['y'].max())
and self.growth == 'linear'
):
# Nothing to fit.
self.params = stan_init()
def __init__(self, name, dataset, features, targets, job_option, validation_option=None):
MLJob.__init__(self, name, dataset)
self.job_option = job_option
self.features = features
self.targets = targets
self.validation_result = {}
self.validation_option = validation_option
if self.job_option is None:
self.job_option = {}
self._handle_job_option()
self.type = 'TimeSerialsForecastsJob'
self.model = Prophet()
def create_model(d):
"""Create Prophet model using each input grid parameter set."""
m = Prophet(seasonality_prior_scale=seasonality_prior_scale,
changepoint_prior_scale=changepoint_prior_scale,
changepoint_range=changepoint_range,
interval_width=0.95, weekly_seasonality=True, daily_seasonality=True)
d['model'] = m
return d
def create_model(self):
# Make the model
model = fbprophet.Prophet(daily_seasonality=self.daily_seasonality,
weekly_seasonality=self.weekly_seasonality,
yearly_seasonality=self.yearly_seasonality,
changepoint_prior_scale=self.changepoint_prior_scale,
changepoints=self.changepoints)
if self.monthly_seasonality:
# Add monthly seasonality
model.add_seasonality(name = 'monthly', period = 30.5, fourier_order = 5)
return model
def prophet_model(self):
model = fbprophet.Prophet(daily_seasonality=False, yearly_seasonality=False)
return model
changepoints = self.changepoints
numChangepoints = self.config.getIntConfig("train.num.changepoints")[0]
changepointRange = self.config.getFloatConfig("train.changepoint.range")[0]
yearlySeasonality = typedValue(self.config.getStringConfig("train.yearly.seasonality")[0])
weeklySeasonality = typedValue(self.config.getStringConfig("train.weekly.seasonality")[0])
dailySeasonality = typedValue(self.config.getStringConfig("train.daily.seasonality")[0])
holidays = self.holidays
seasonalityMode = self.config.getStringConfig("train.seasonality.mode")[0]
seasonalityPriorScale = self.config.getFloatConfig("train.seasonality.prior.scale")[0]
holidaysPriorScale = self.config.getFloatConfig("train.holidays.prior.scale")[0]
changepointPriorScale = self.config.getFloatConfig("train.changepoint.prior.scale")[0]
mcmcSamples = self.config.getIntConfig("train.mcmc.samples")[0]
intervalWidth = self.config.getFloatConfig("train.interval.width")[0]
uncertaintySamples = self.config.getIntConfig("train.uncertainty.samples")[0]
self.model = Prophet(growth=growth, changepoints=changepoints, n_changepoints=numChangepoints,\
changepoint_range=changepointRange, yearly_seasonality=yearlySeasonality, weekly_seasonality=weeklySeasonality,\
daily_seasonality=dailySeasonality, holidays=holidays, seasonality_mode=seasonalityMode,\
seasonality_prior_scale=seasonalityPriorScale, holidays_prior_scale=holidaysPriorScale,\
changepoint_prior_scale=changepointPriorScale,mcmc_samples=mcmcSamples,interval_width=intervalWidth,\
uncertainty_samples=uncertaintySamples)
# Calculate seasonal components
self.forecast = self.model.predict_seasonal_components(df_y)
else:
# Prepare the seasonality data frame
start = pd.to_datetime('2017-01-01 0000')
period = self.model.seasonalities[self.seasonality]['period']
end = start + pd.Timedelta(days=period)
# plot_points = 200
# plot_points is used instead of period below in fbprophet/forecaster.py.
# However, it seems to make more sense to use period given the expected usage in Qlik
intervals = pd.to_datetime(np.linspace(start.value, end.value, period))
df_x = plot.seasonality_plot_df(self.model, intervals)
# Calculate seasonal components
self.forecast = self.model.predict_seasonal_components(df_x)
# Set the correct sort order for the response
try:
self.forecast = self.forecast.reindex(self.sort_order.index)
except AttributeError:
pass
# For standard forecast the output rows equal the input rows
else:
# Prepare the forecast
self.forecast = self.model.predict(self.future_df)
# For return=y_then_yhat[_upper / _lower] we return y values followed by relevant results for the forecast periods
def _forecast(self):
"""
Execute the forecast algorithm according to the request type
"""
# If this is a seasonality request, we need to return the relevant seasonlity component
if self.is_seasonality_request:
if self.seasonality == 'weekly':
# Prepare the seasonality data frame
# Parameter start needs to be any arbitrary week starting on a Sunday
days = (pd.date_range(start='2017-01-01', periods=7) + pd.Timedelta(days=self.weekly_start))
df_w = plot.seasonality_plot_df(self.model, days)
# Calculate seasonal components
self.forecast = self.model.predict_seasonal_components(df_w)
elif self.seasonality == 'yearly':
# Prepare the seasonality data frame
# Parameter start needs to be 1st January for any arbitrary year
days = (pd.date_range(start='2017-01-01', periods=365) + pd.Timedelta(days=self.yearly_start))
df_y = plot.seasonality_plot_df(self.model, days)
# Calculate seasonal components
self.forecast = self.model.predict_seasonal_components(df_y)
else:
# Prepare the seasonality data frame
start = pd.to_datetime('2017-01-01 0000')