def get_prophet_forecast(df, country="ALL", save_model=True): df_ts = df.copy() if country == "ALL" else df[df['country'] == country].copy() if len(df_ts) <= 180: return None df_month = df_ts.groupby('inv_month').sum().reset_index() df_ts['inv_date'] = pd.to_datetime(df_ts['inv_date']) df_ts.rename(columns={'inv_date': 'ds', 'value': 'y'}, inplace=True) m = Prophet(yearly_seasonality=20) m.fit(df_ts) if save_model: with open(fr'models\{country}_model.json', 'w') as fout: json.dump(model_to_json(m), fout) # Save model future = m.make_future_dataframe(periods=180, freq='D') forecast = m.predict(future) forecast['inv_month'] = forecast['ds'].apply( lambda v: date(v.year, v.month, 1).isoformat()) monthly_forecast = forecast[['inv_month', 'yhat' ]].groupby('inv_month').sum().reset_index() df_forecast = pd.merge(monthly_forecast, df_month, on='inv_month', how='left') return df_forecast
def _estimate_weekly_pattern(self, time, energy_per_day, daily_masks): epd = energy_per_day.copy() for i in range(daily_masks.shape[0]): if daily_masks[i] < 1: epd[i] = np.nan daily_times = [i*self.vpd for i in range(daily_masks.shape[0])] try: time_index = pd.to_datetime( time.take(daily_times), format='%Y-%m-%d %H:%M:%S') except ValueError: time_index = pd.to_datetime( time.take(daily_times), format='%d-%b-%Y %H:%M:%S') time_index.reset_index(drop=True, inplace=True) daily_data = pd.concat([time_index, pd.Series( epd)], keys=['ds', 'y'], axis=1) prophet_model = Prophet(weekly_seasonality=True, yearly_seasonality=True) if daily_data.count()['y'] > 1: prophet_model.fit(daily_data) # `future` starts with the first day of the time series. # This allows us to select the corresponding pattern value # for day `i` by accessing `weekly[i % 7]`. future = prophet_model.make_future_dataframe(periods=0) forecast = prophet_model.predict(future) return forecast.get('weekly').head(7) return pd.Series(np.zeros(7))
def get_forecast(df, month, country="ALL"): df_ts = df.copy() if country=="ALL" else df[df['country']==country].copy() if len(df_ts) <= 180: return None actual = df_ts[df_ts['inv_month'] == month.isoformat()]['value'].sum() df_ts = df_ts[['inv_month', 'inv_date', 'value']].groupby(['inv_month', 'inv_date']).sum().reset_index() df_train = df_ts[df_ts['inv_month'] < month.isoformat()] df_train['inv_date'] = pd.to_datetime(df_train['inv_date']) df_train.rename(columns={'inv_date':'ds', 'value':'y'}, inplace=True) m = Prophet(yearly_seasonality=20) m.fit(df_train) future = m.make_future_dataframe(periods=60, freq='D') df_forecast = m.predict(future) df_forecast['inv_month'] = df_forecast['ds'].apply(lambda v: date(v.year, v.month, 1).isoformat()) forecast = df_forecast[df_forecast['inv_month'] == month.isoformat()]['yhat'].sum() exp_model = SimpleExpSmoothing(df_ts[['inv_month', 'inv_date', 'value']].set_index(['inv_month', 'inv_date'])).fit(smoothing_level=0.2, optimized=False) exp_forecast = forecast_df = sum(exp_model.forecast(30)) return actual, forecast, exp_forecast
def predict_in_france(data, country, vaccination_metric, future_days, plot=True): df = data[(data['country'] == country)] df = df[['date', vaccination_metric]] df.columns = ['ds', 'y'] model = Prophet(interval_width=0.95) model.fit(df) layout = dict( title= 'Prediction : Nombre de personnes vaccinées par cent dans les 60 prochains jours (jusqu' "'" 'au 28/06/21)', xaxis=dict(title='Dates'), yaxis=dict(title='Pourcentage')) future = model.make_future_dataframe(periods=future_days) forecast = model.predict(future) if plot: fig = plot_plotly(model, forecast) fig.layout = layout fig.write_html("analyse/Prediction_France.html") else: return forecast
def make_forecast(df, len_forecast: int, time_series_label: str): """ Function for making time series forecasting with Prophet library :param df: dataframe to process :param len_forecast: forecast length :param time_series_label: name of time series to process :return predicted_values: forecast :return model_name: name of the model (always 'AutoTS') """ df['ds'] = df['datetime'] df['y'] = df[time_series_label] prophet_model = Prophet() prophet_model.fit(df) future = prophet_model.make_future_dataframe(periods=len_forecast, include_history=False) forecast = prophet_model.predict(future) predicted_values = np.array(forecast['yhat']) model_name = 'Prophet' return predicted_values, model_name
def _prophet_fit_and_predict( # pylint: disable=too-many-arguments df: DataFrame, confidence_interval: float, yearly_seasonality: Union[bool, str, int], weekly_seasonality: Union[bool, str, int], daily_seasonality: Union[bool, str, int], periods: int, freq: str, ) -> DataFrame: """ Fit a prophet model and return a DataFrame with predicted results. """ try: prophet_logger = logging.getLogger("prophet.plot") prophet_logger.setLevel(logging.CRITICAL) from prophet import Prophet # pylint: disable=import-error prophet_logger.setLevel(logging.NOTSET) except ModuleNotFoundError: raise QueryObjectValidationError(_("`prophet` package not installed")) model = Prophet( interval_width=confidence_interval, yearly_seasonality=yearly_seasonality, weekly_seasonality=weekly_seasonality, daily_seasonality=daily_seasonality, ) if df["ds"].dt.tz: df["ds"] = df["ds"].dt.tz_convert(None) model.fit(df) future = model.make_future_dataframe(periods=periods, freq=freq) forecast = model.predict(future)[[ "ds", "yhat", "yhat_lower", "yhat_upper" ]] return forecast.join(df.set_index("ds"), on="ds").set_index(["ds"])
def forecast(self, forecast_horizon: int = 96): super().forecast(forecast_horizon) print("Running Prophet forecast for Currency-pair: {} using forecast horizon: {}", self.currency_pair.upper(), forecast_horizon) print("Dataset: ", self.currency_pair.upper()) print(self.training_data.head(5)) print(".....\t.........\t...") print(self.training_data.tail(5)) # model = Prophet(interval_width=0.99, mcmc_samples=60) model = Prophet(interval_width=0.99) model.fit(self.training_data) future = model.make_future_dataframe(periods=forecast_horizon) future.tail() _forecast = model.predict(future) last_n = _forecast.tail(forecast_horizon) last_n.to_csv(f"output/{self.currency_pair}__{self.model_name.lower()}__{forecast_horizon}__forecasts.csv") # last_n = _forecast[["ds", "yhat", "yhat_lower", "yhat_upper"]].tail(n) print(last_n) self.forecasts = last_n["yhat"] self.forecasts_upper = last_n["yhat_upper"] self.forecasts_lower = last_n["yhat_lower"] self.errors = [(abs(upper - lower) / 2) for upper, lower in zip(self.forecasts_upper, self.forecasts_lower)] self.forecasts_raw = last_n
def test_prophet_trend_onnx(self): df = self._get_data() m = Prophet() m.fit(df) future = m.make_future_dataframe(periods=365) future_np = (future.values - np.datetime64("1970-01-01T00:00:00.000000000")).astype( np.int64) / 1000000000 # Convert with Hummingbird. hb_model = hummingbird.ml.convert(m, "onnx", future_np) # Predictions. prophet_trend = m.predict(future)["trend"] hb_trend = hb_model.predict(future_np) import onnx onnx.save(hb_model.model, "prophet.onnx") np.testing.assert_allclose(prophet_trend, hb_trend, rtol=1e-06, atol=1e-06)
def fit_prophet(dtf_train, dtf_test, lst_exog=None, model=None, freq="D", conf=0.95, figsize=(15,10)): ## setup prophet if model is None: model = Prophet(growth="linear", changepoints=None, n_changepoints=25, seasonality_mode="multiplicative", yearly_seasonality="auto", weekly_seasonality="auto", daily_seasonality="auto", holidays=None, interval_width=conf) if lst_exog != None: for regressor in lst_exog: model.add_regressor(regressor) ## train model.fit(dtf_train) ## test dtf_prophet = model.make_future_dataframe(periods=len(dtf_test)+10, freq=freq, include_history=True) if model.growth == "logistic": dtf_prophet["cap"] = dtf_train["cap"].unique()[0] if lst_exog != None: dtf_prophet = dtf_prophet.merge(dtf_train[["ds"]+lst_exog], how="left") dtf_prophet.iloc[-len(dtf_test):][lst_exog] = dtf_test[lst_exog].values dtf_prophet = model.predict(dtf_prophet) dtf_train = dtf_train.merge(dtf_prophet[["ds","yhat"]], how="left").rename( columns={'yhat':'model', 'y':'ts'}).set_index("ds") dtf_test = dtf_test.merge(dtf_prophet[["ds","yhat","yhat_lower","yhat_upper"]], how="left").rename( columns={'yhat':'forecast', 'y':'ts', 'yhat_lower':'lower', 'yhat_upper':'upper'}).set_index("ds") ## evaluate dtf = dtf_train.append(dtf_test) dtf = utils_evaluate_ts_model(dtf, conf=conf, figsize=figsize, title="Prophet") return dtf, model
def generate_forecast(df: pd.DataFrame, length: int) -> pd.DataFrame: """Takes in a training set (data - value) and a length to return a forecast DataFrame""" model = Prophet(interval_width=0.50) model.fit(df) return model.predict(model.make_future_dataframe(periods=length))
def prophet_forecast(self, train_start, train_end, test_start, test_end): from sys import platform if platform == "linux" or platform == "linux2": from prophet import Prophet # linux elif platform == "darwin": from fbprophet import Prophet # OS X elif platform == "win32": from fbprophet import Prophet # Windows... data = self.data.reset_index() data.rename(columns={'Date': 'ds', self.column: 'y'}, inplace=True) # FIXME and take user input size = len(data) df_train = data.iloc[int(-size * .10):, :] df_test = data.iloc[int(-size * .20):, :] model_prophet = Prophet(seasonality_mode='additive') model_prophet.add_seasonality(name='monthly', period=30.5, fourier_order=5) model_prophet.fit(df_train) df_future = model_prophet.make_future_dataframe(periods=365) df_pred = model_prophet.predict(df_future) model_prophet.plot(df_pred) plt.tight_layout() plt.title('Prophet Forecast') plt.savefig(os.path.join(img_dirp, f'img/prophet_forecast.png')) model_prophet.plot_components(df_pred) plt.tight_layout() plt.savefig(os.path.join(img_dirp, 'img/components.png')) # merge test set with predicted data and plot accuracy of model's predictions selected_columns = ['ds', 'yhat_lower', 'yhat_upper', 'yhat'] df_pred = df_pred.loc[:, selected_columns].reset_index(drop=True) df_test = df_test.merge(df_pred, on=['ds'], how='left') df_test.ds = pd.to_datetime(df_test.ds) df_test.set_index('ds', inplace=True) fig, ax = plt.subplots(1, 1) ax = sns.lineplot( data=df_test[['y', 'yhat_lower', 'yhat_upper', 'yhat']]) ax.fill_between(df_test.index, df_test.yhat_lower, df_test.yhat_upper, alpha=0.3) ax.set(title=f'{self.column} - actual vs. predicted', xlabel='Date', ylabel='{self.column}') plt.tight_layout() plt.savefig(os.path.join(img_dirp, 'img/actual_v_predicted.png'))
def test_make_future_dataframe(self): N = 468 train = DATA.head(N // 2) forecaster = Prophet() forecaster.fit(train) future = forecaster.make_future_dataframe(periods=3, freq='D', include_history=False) correct = pd.DatetimeIndex(['2013-04-26', '2013-04-27', '2013-04-28']) self.assertEqual(len(future), 3) for i in range(3): self.assertEqual(future.iloc[i]['ds'], correct[i]) future = forecaster.make_future_dataframe(periods=3, freq='M', include_history=False) correct = pd.DatetimeIndex(['2013-04-30', '2013-05-31', '2013-06-30']) self.assertEqual(len(future), 3) for i in range(3): self.assertEqual(future.iloc[i]['ds'], correct[i])
def getTickerForcast(ticker, period, dataPeriod): stockTicker = yf.Ticker(ticker) data = stockTicker.history(period=dataPeriod, interval="1d") df = pd.DataFrame({'ds': data.index.values, 'y': data['Close'].values}) m = Prophet(daily_seasonality=True) m.fit(df) future = m.make_future_dataframe(periods=period) prediction = m.predict(future) return pd.DataFrame({ 'date': prediction['ds'].values, 'close': prediction['yhat'].values }).to_dict('records')
def run_prophet(data, predict_period=40): data = data.reset_index() print("data columns", data.columns) data = data[["Date", "Adj Close"]] data.columns = ["ds", "y"] m = Prophet() m.fit(data) future = m.make_future_dataframe(periods=predict_period) future.tail() forecast = m.predict(future) # forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail() forecast = forecast[['ds', 'trend', 'yhat', 'yhat_lower', 'yhat_upper']] return forecast, m
class MetricPredictor: """docstring for Predictor.""" model_name = "prophet" model_description = "Forecasted value from Prophet model" model = None predicted_df = None metric = None def __init__(self, metric, rolling_data_window_size="10d"): """Initialize the Metric object.""" self.metric = Metric(metric, rolling_data_window_size) def train(self, metric_data=None, prediction_duration=15): """Train the Prophet model and store the predictions in predicted_df.""" prediction_freq = "1MIN" # convert incoming metric to Metric Object if metric_data: # because the rolling_data_window_size is set, this df should not bloat self.metric += Metric(metric_data) # Don't really need to store the model, as prophet models are not retrainable # But storing it as an example for other models that can be retrained self.model = Prophet(daily_seasonality=True, weekly_seasonality=True, yearly_seasonality=True) _LOGGER.info("training data range: %s - %s", self.metric.start_time, self.metric.end_time) # _LOGGER.info("training data end time: %s", self.metric.end_time) _LOGGER.debug("begin training") self.model.fit(self.metric.metric_values) future = self.model.make_future_dataframe( periods=int(prediction_duration), freq=prediction_freq, include_history=False, ) forecast = self.model.predict(future) forecast["timestamp"] = forecast["ds"] forecast = forecast[["timestamp", "yhat", "yhat_lower", "yhat_upper"]] forecast = forecast.set_index("timestamp") self.predicted_df = forecast _LOGGER.debug(forecast) def predict_value(self, prediction_datetime): """Return the predicted value of the metric for the prediction_datetime.""" nearest_index = self.predicted_df.index.get_loc(prediction_datetime, method="nearest") return self.predicted_df.iloc[[nearest_index]]
def forecast(df, periods, showflag=True): m = Prophet() m.fit(df) future = m.make_future_dataframe(periods=periods) predict = m.predict(future) if showflag: fig = m.plot(predict) fig.set_figheight(18) fig.set_figwidth(9) plt.title("forcasted") plt.show() return m, future, predict
def get_model_forecast_pred(self): train, test = self.data[:-self.PRED_DAYS], self.data[-self.PRED_DAYS:] model = Prophet(growth='logistic', holidays=self.holidays, holidays_prior_scale=self.holiday_weight, seasonality_prior_scale=self.seasonality_weight, changepoint_prior_scale=self.changepoint_weight, changepoint_range=self.changepoint_range, changepoints=self.changepoints if self.changepoints else None, ) if self.ADD_COUNTRY_HOLIDAY: model.add_country_holidays(country_name='KR') if self.ADD_MONTHLY_SEASONALITY: model.add_seasonality(name='montly_seasonality', period=30.5, fourier_order=5) if self.PRC: model.add_regressor('avg_prc', prior_scale=self.price_weight, standardize=False) model.fit(train) future = model.make_future_dataframe(periods=self.PRED_DAYS) future = pd.merge(future, train, left_on='ds', right_on='ds', how='left') future = future[['ds', 'floor', 'cap', 'avg_prc']] future['avg_prc'] = self.data.avg_prc.values future_fill_missing = {'cap' : 100, 'floor' : 0.0} future.fillna(future_fill_missing, inplace=True) else: model.fit(train) future = model.make_future_dataframe(periods=self.PRED_DAYS) future['cap'] = 100 future['floor'] = 0.0 forecast = model.predict(future) pred = forecast[['ds', 'yhat']][-self.PRED_DAYS:] pred['yhat'] = np.where(pred['yhat'] < 0, 0, pred['yhat']) return model, forecast, pred
def test_fit_predict(self): days = 30 N = DATA.shape[0] train = DATA.head(N - days) test = DATA.tail(days) test.reset_index(inplace=True) forecaster = Prophet() forecaster.fit(train, seed=1237861298) np.random.seed(876543987) future = forecaster.make_future_dataframe(days, include_history=False) future = forecaster.predict(future) # this gives ~ 10.64 res = self.rmse(future['yhat'], test['y']) self.assertTrue(15 > res > 5, msg="backend: {}".format(forecaster.stan_backend))
def distributed_model_prediction(history_pd): history_pd['ds'] = pd.to_datetime(history_pd['ds']) # make the model prophet_model = Prophet() prophet_model.fit(history_pd) future_pd = prophet_model.make_future_dataframe(periods=90, freq='d', include_history=True) # make predictions results_pd = prophet_model.predict(future_pd) results_pd["country"] = history_pd["country"].iloc[0] return pd.DataFrame(results_pd, columns=result_schema.fieldNames())
def test_flat_growth(self): m = Prophet(growth='flat') x = np.linspace(0, 2 * np.pi, 8 * 7) history = pd.DataFrame({ 'ds': pd.date_range(start='2020-01-01', periods=8 * 7, freq='d'), 'y': 30 + np.sin(x * 8.), }) m.fit(history) future = m.make_future_dataframe(10, include_history=True) fcst = m.predict(future) m_ = m.params['m'][0, 0] k = m.params['k'][0, 0] self.assertAlmostEqual(k, 0) self.assertAlmostEqual(fcst['trend'].unique()[0], m_ * m.y_scale) self.assertEqual(np.round(m_ * m.y_scale), 30.0)
def prophet_fit_and_predict_with_exog_and_advance_vars( y: [[float]], k: int, t: [float], a: [[float]], model_params: dict = None) -> Tuple[List, List, Any, Any]: """ Simpler wrapper for testing - univariate w/ advance vars w/ supplied times and future times """ assert len(t) == len(y) + k assert len(a) == len(y) + k assert isinstance(y[0], List) a_cols = ['a' + str(i) for i in range(len(a[0]))] df = pd.DataFrame(columns=a_cols, data=a[:-k]) Y = transpose(y) df['y'] = Y[0] n_exog = len(y[0]) - 1 y_cols = ['y'] + ['y' + str(i) for i in range(1, len(y[0]))] for i in range(1, n_exog + 1): df['y' + str(i)] = Y[i][:len(y)] dt = epoch_to_naive_datetime(t) df['ds'] = dt[:len(y)] kwargs_used = dict([(k, v) for k, v in PROPHET_MODEL.items()]) if model_params: kwargs_used.update(model_params) m = Prophet(**kwargs_used) for a_col in a_cols: m.add_regressor(name=a_col) for y_col in y_cols[1:]: m.add_regressor(name=y_col) with no_stdout_stderr(): m.fit(df) freq = infer_freq_from_epoch(t) future = m.make_future_dataframe(periods=k, freq=freq) future['ds'] = dt full_a_data = transpose(a) for a_col, a_vals in zip(a_cols, full_a_data): future[a_col] = a_vals for i in range(1, n_exog + 1): # Just bring forward future['y' + str(i)] = Y[i] + [Y[i][-1]] * k forecast = m.predict(future) x = forecast['yhat'].values[-k:] # Use m.plot(forecast) to take a peak x_std = [ u - l for u, l in zip(forecast['yhat_upper'].values, forecast['yhat_lower'].values) ] return x, x_std, forecast, m
def estimate(file_path): try: df = pd.read_csv(file_path) except: raise FileNotFoundError('파일을 찾을 수 없습니다') date_range = pd.date_range(start=START_DATE, end=END_DATE, freq='1H')[:-1] df['ds'] = df['dt'].map(str) + " " + df['dhour'].map(str) + ":00:00" df['ds'] = pd.to_datetime(df['ds']) df = df.set_index('ds') df = pd.merge(date_range.to_frame(), df, left_index=True, right_index=True, how='left') df['ds'] = df.index df = df.rename(columns={'sales' : 'y'}) missing_fill_val = {'avg_prc' : df.avg_prc.median(), 'y' : 0} df.fillna(missing_fill_val, inplace=True) q3 = df['y'].quantile(q=0.75) cap = q3 * 1.5 df['y'] = df['y'].apply(lambda x : cap if x >= cap else x) df = df[['ds', 'y', 'avg_prc']] scaler = MinMaxScaler() scaled_value = scaler.fit_transform(df[['avg_prc', 'y']].values) df[['avg_prc', 'y']] = scaled_value df['floor'] = 0 df['cap'] = 1.2 train = df[:-PRED_DAYS] test = df[-PRED_DAYS:] m = Prophet(growth='logistic', holidays=holidays, holidays_prior_scale=1) m.add_regressor('avg_prc') m.fit(train) future = m.make_future_dataframe(periods=PRED_DAYS, freq='H') future = pd.merge(future, train, left_on='ds', right_on='ds', how='left') future = future[['ds', 'floor', 'cap', 'avg_prc']] future_fill_missing = {'avg_prc' : df.iloc[len(df)-1]['avg_prc'], 'cap' : 1.2, 'floor' : 0} future.fillna(future_fill_missing, inplace=True) forecast = m.predict(future) pred = forecast[['ds', 'yhat']][-PRED_DAYS:] pred['yhat'] = np.where(pred['yhat'] < 0, 0, pred['yhat']) rmse = math.sqrt(mean_squared_error(test['y'], pred['yhat'])) r2score = r2_score(test['y'], pred['yhat']) return rmse, r2score
def test_prophet_trend(self): df = self._get_data() m = Prophet() m.fit(df) # Convert with Hummingbird. hb_model = hummingbird.ml.convert(m, "torch") # Predictions. future = m.make_future_dataframe(periods=365) prophet_trend = m.predict(future)["trend"].values hb_trend = hb_model.predict(future) np.testing.assert_allclose(prophet_trend, hb_trend, rtol=1e-06, atol=1e-06)
def _project_stat(stats_df: DataFrame, num_games: int) -> float: """Project a single stat for `num_games` more games :param stats_df: A dataframe containing just two fields: game_date and the statistic you want to project :param num_games: The number of games for which you want to project the statistic :return: The total of the statistic that the player is projected to have in the matchup. For example, a player could be projected to get 62 points over three games """ # this is what Prophet needs the columns to be called stats_df.columns = ["ds", "y"] with OutputSuppressor(): model = Prophet() model.fit(stats_df) future = model.make_future_dataframe(periods=num_games) forecast = model.predict(future) return sum( max(float(val), 0) for val in forecast[["yhat"]].tail(num_games).values)
def plot_optima_data(optima_data, months): m = Prophet(seasonality_mode='multiplicative') print(optima_data) prophet_data = pd.DataFrame.from_dict(optima_data, orient='index', columns=['ds', 'y']) with suppress_stdout_stderr(): m.fit(prophet_data) future = m.make_future_dataframe(periods=months, freq='m', include_history=True) forecast = m.predict(future) forecast["cost_rounded"] = forecast.yhat.round() forecast["cost_lower_rounded"] = forecast.yhat_lower.round() forecast["cost_upper_rounded"] = forecast.yhat_upper.round() print(forecast[[ 'ds', 'yhat', 'yhat_lower', 'yhat_upper', 'cost_rounded', 'cost_lower_rounded', 'cost_upper_rounded' ]].tail())
def prophet_fit_and_predict_with_time_and_advance_time(y: [float], k: int, t: [float], model_params: dict = None) -> \ Tuple[List, List, Any, Any]: """ Simpler wrapper for testing - univariate only w/ supplied times and future times """ assert len(t) == len(y) + k df = pd.DataFrame(columns=['y'], data=y) dt = epoch_to_naive_datetime(t) df['ds'] = dt[:len(y)] kwargs_used = dict([(k, v) for k, v in PROPHET_MODEL.items()]) if model_params: kwargs_used.update(model_params) m = Prophet(**kwargs_used) with no_stdout_stderr(): m.fit(df) freq = infer_freq_from_epoch(t) future = m.make_future_dataframe(periods=k, freq=freq) future['ds'] = dt forecast = m.predict(future) x = forecast['yhat'].values[-k:] # Use m.plot(forecast) to take a peak x_std = [ u - l for u, l in zip(forecast['yhat_upper'].values, forecast['yhat_lower'].values) ] return x, x_std, forecast, m
def get_model_forecast(info): ds = ast.literal_eval(info['train_ds']) y = ast.literal_eval(info['train_y']) avg_prc = ast.literal_eval(info['train_avg_prc']) test_y = ast.literal_eval(info['test_y']) test_avg_prc = ast.literal_eval(info['test_avg_prc']) dic = {'ds': ds, 'y': y, 'avg_prc': avg_prc} data = pd.DataFrame(dic) holidays = pd.read_json(info['holidays']) ## feature engineering if data['avg_prc'].max() > 0: data['avg_prc'] = data['avg_prc'] / data['avg_prc'].max() * 100 else: data['avg_prc'] = data['avg_prc'] / (data['avg_prc'].max() + 1) * 100 data['cap'] = 100.0 data['floor'] = 0.0 ## run prophet model = Prophet(growth='logistic', holidays=holidays) model.add_country_holidays(country_name='KR') model.add_seasonality(name='monthly', period=30.5, fourier_order=5) if data['avg_prc'].isna().sum() == 0: model.add_regressor('avg_prc') model.fit(data) ## get estimation future = model.make_future_dataframe(periods=PRED_DAYS) if data['avg_prc'].isna().sum() == 0: future['avg_prc'] = pd.concat( [pd.Series(avg_prc), pd.Series(test_avg_prc)], ignore_index=True) future['cap'] = 100 future['floor'] = 0.0 forecast = model.predict(future) return model, forecast
def predict(): # Build model m = Prophet(interval_width=0.95, yearly_seasonality=True, weekly_seasonality=True, daily_seasonality=True, changepoint_prior_scale=2) m.add_seasonality(name='monthly', period=30.5, fourier_order=5, prior_scale=0.02) df_new = data()[0] m.fit(df_new) future = m.make_future_dataframe(periods=7, freq='D') # Predict forecast = m.predict(future) forecast = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']] forecast['yhat'] = np.exp(forecast['yhat']) forecast['yhat_lower'] = np.exp(forecast['yhat_lower']) forecast['yhat_upper'] = np.exp(forecast['yhat_upper']) return forecast
def prophet_fit_and_predict_simple( y: [float], k: int, freq: str = None, model_params: dict = None) -> Tuple[List, List, Any, Any]: """ Simpler wrapper for testing - univariate only """ df = pd.DataFrame(columns=['y'], data=y) freq = freq or PROPHET_META['freq'] df['ds'] = pd.date_range(start=EPOCH, periods=len(y), freq=freq) # UTC kwargs_used = dict([(k, v) for k, v in PROPHET_MODEL.items()]) if model_params: kwargs_used.update(model_params) m = Prophet(**kwargs_used) with no_stdout_stderr(): m.fit(df) future = m.make_future_dataframe(periods=k, freq=freq) forecast = m.predict(future) x = forecast['yhat'].values[-k:] # Use m.plot(forecast) to take a peak x_std = [ u - l for u, l in zip(forecast['yhat_upper'].values, forecast['yhat_lower'].values) ] return x, x_std, forecast, m
def get_prophet_forecast(prophet_training_data): m = Prophet() m.fit(prophet_training_data) future = m.make_future_dataframe(periods=24) return m.predict(future)