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)
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)