def test_normalize(self): for add in [0, -1, 0.00000001, -0.99999999]: length = 1000 days = pd.date_range(start="2017-01-01", periods=length) y = np.zeros(length) y[1] = 1 y = y + add df = pd.DataFrame({"ds": days, "y": y}) m = NeuralProphet(normalize="soft", ) data_params = df_utils.init_data_params( df, normalize=m.normalize, covariates_config=m.config_covar, regressor_config=m.regressors_config, events_config=m.events_config, ) df_norm = df_utils.normalize(df, data_params)
def test_newer_sample_weight(): dates = pd.date_range(start="2020-01-01", periods=100, freq="D") a = [0, 1] * 50 y = -1 * np.array(a[:50]) y = np.concatenate([y, np.array(a[50:])]) # first half: y = -a # second half: y = a df = pd.DataFrame({"ds": dates, "y": y, "a": a}) newer_bias = 5 m = NeuralProphet( epochs=10, batch_size=10, learning_rate=1.0, newer_samples_weight=newer_bias, newer_samples_start=0.0, # growth='off', n_changepoints=0, daily_seasonality=False, weekly_seasonality=False, yearly_seasonality=False, ) m.add_future_regressor("a") metrics_df = m.fit(df) # test that second half dominates # -> positive relationship of a and y dates = pd.date_range(start="2020-01-01", periods=100, freq="D") a = [1] * 100 y = [None] * 100 df = pd.DataFrame({"ds": dates, "y": y, "a": a}) forecast1 = m.predict(df[:10]) forecast2 = m.predict(df[-10:]) avg_a1 = np.mean(forecast1["future_regressor_a"]) avg_a2 = np.mean(forecast2["future_regressor_a"]) log.info("avg regressor a contribution first samples: {}".format(avg_a1)) log.info("avg regressor a contribution last samples: {}".format(avg_a2)) # must hold assert avg_a1 > 0.1 assert avg_a2 > 0.1 # this is less strict, as it also depends on trend, but should still hold avg_y1 = np.mean(forecast1["yhat1"]) avg_y2 = np.mean(forecast2["yhat1"]) log.info("avg yhat first samples: {}".format(avg_y1)) log.info("avg yhat last samples: {}".format(avg_y2)) assert avg_y1 > -0.9 assert avg_y2 > 0.1
def test_trend(self): log.info("testing: Trend") df = pd.read_csv(PEYTON_FILE, nrows=512) m = NeuralProphet( growth="linear", n_changepoints=10, changepoints_range=0.9, trend_reg=1, trend_reg_threshold=False, yearly_seasonality=False, weekly_seasonality=False, daily_seasonality=False, epochs=EPOCHS, ) # print(m.config_trend) metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df, periods=60, n_historic_predictions=len(df)) forecast = m.predict(df=future) if self.plot: m.plot(forecast) # m.plot_components(forecast) m.plot_parameters() plt.show()
def create_model(ui_params, data): # data training if ui_params.model == "fbprophet": m = Prophet(changepoint_range=ui_params.training_percentage, yearly_seasonality=ui_params.yearly_seasonality, weekly_seasonality=ui_params.weekly_seasonality, daily_seasonality=ui_params.daily_seasonality, seasonality_mode=ui_params.seasonality_mode) if ui_params.future_volume > 0: m.add_regressor('Volume') if ui_params.holidays is not None: m.add_country_holidays(country_name=ui_params.holidays) m.fit(data) train_metrics = None val_metrics = None elif ui_params.model == "neuralprophet": # changepoint_range=ui_params.training_percentage, # replaced with n_changepoint m = NeuralProphet(yearly_seasonality=ui_params.yearly_seasonality, weekly_seasonality=ui_params.weekly_seasonality, daily_seasonality=ui_params.daily_seasonality, seasonality_mode=ui_params.seasonality_mode, n_forecasts=ui_params.future_days, num_hidden_layers=5) if ui_params.training_percentage < 1.0: validation_percentage = 1.0 - ui_params.training_percentage df_train, df_val = m.split_df(data, valid_p=validation_percentage) else: df_train = data df_val = None train_metrics = m.fit(df_train, freq="D", validate_each_epoch=True) if df_val is None: val_metrics = None else: val_metrics = m.test(df_val) return m, train_metrics, val_metrics
def test_train_eval_test(self): log.info("testing: Train Eval Test") m = NeuralProphet( n_lags=10, n_forecasts=3, ar_sparsity=0.1, epochs=3, batch_size=32, ) df = pd.read_csv(PEYTON_FILE, nrows=95) df = df_utils.check_dataframe(df, check_y=False) df = m._handle_missing_data(df, freq="D", predicting=False) df_train, df_test = m.split_df(df, freq="D", valid_p=0.1) metrics = m.fit(df_train, freq="D", validate_each_epoch=True, valid_p=0.1) metrics = m.fit(df_train, freq="D") val_metrics = m.test(df_test) log.debug("Metrics: train/eval: \n {}".format(metrics.to_string(float_format=lambda x: "{:6.3f}".format(x)))) log.debug("Metrics: test: \n {}".format(val_metrics.to_string(float_format=lambda x: "{:6.3f}".format(x))))
def predictions(df: DataFrame) -> DataFrame: """ Make predictions about :param df: Dataframe to make the predictions :return: """ m = NeuralProphet() m.fit(df, freq='D') future = m.make_future_dataframe(df, periods=DAYS_OF_PREDICTION) forecast = m.predict(future) forecast['ds'] = pd.to_datetime(forecast['ds']).dt.strftime('%Y-%m-%d') forecast = forecast.set_index('ds') forecast.rename(columns={'yhat1': 'currency'}, inplace=True) forecast = forecast.round(2) return forecast
def test_newer_sample_weight(): dates = pd.date_range(start="2020-01-01", periods=1000, freq="D") a = [0, 1] * 500 y = -2 * np.array(a[:500]) y = np.concatenate([y, 2 * np.array(a[500:])]) # first half: y = -2a # second half: y = 2a df = pd.DataFrame({"ds": dates, "y": y, "a": a}) m = NeuralProphet( epochs=10, batch_size=128, newer_samples_weight=10, newer_samples_start=0.0, learning_rate=0.1, daily_seasonality=False, weekly_seasonality=False, yearly_seasonality=False, ) m.add_future_regressor("a") metrics_df = m.fit(df) # test that second half dominates # -> positive relationship of a and y dates = pd.date_range(start="2020-01-01", periods=1000, freq="D") a = [1] * 1000 y = [None] * 1000 df = pd.DataFrame({"ds": dates, "y": y, "a": a}) forecast1 = m.predict(df[:10]) forecast2 = m.predict(df[-10:]) avg_a1 = np.mean(forecast1["future_regressor_a"]) avg_a2 = np.mean(forecast2["future_regressor_a"]) # must hold assert avg_a1 > 0.5 assert avg_a2 > 0.5 # this is less strict, as it also depends on trend, but should still hold avg_y1 = np.mean(forecast1["yhat1"]) avg_y2 = np.mean(forecast2["yhat1"]) assert avg_y1 > -1.5 assert avg_y2 > 0.5
def test_add_lagged_regressors(self): NROWS = 512 EPOCHS = 3 BATCH_SIZE = 32 df = pd.read_csv(PEYTON_FILE, nrows=NROWS) df["A"] = df["y"].rolling(7, min_periods=1).mean() df["B"] = df["y"].rolling(15, min_periods=1).mean() df["C"] = df["y"].rolling(30, min_periods=1).mean() col_dict = { "1": "A", "2": ["B"], "3": ["A", "B", "C"], } for key, value in col_dict.items(): log.debug(value) if isinstance(value, list): feats = np.array(["ds", "y"] + value) else: feats = np.array(["ds", "y", value]) df1 = pd.DataFrame(df, columns=feats) cols = [col for col in df1.columns if col not in ["ds", "y"]] m = NeuralProphet( n_forecasts=1, n_lags=3, weekly_seasonality=False, daily_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) m = m.add_lagged_regressor(names=cols) metrics_df = m.fit(df1, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df1, n_historic_predictions=365) ## Check if the future dataframe contains all the lagged regressors check = any(item in future.columns for item in cols) forecast = m.predict(future) log.debug(check)
def objective(trial: Trial) -> float: params = { "epochs": trial.suggest_categorical("epochs", [50, 100, 200, 300, 400, 500]), "batch_size": 64, "num_hidden_layers": trial.suggest_int("num_hidden_layers", 0, 5), "learning_rate": trial.suggest_float("learning_rate", 1e-3, 0.1), "changepoints_range": trial.suggest_discrete_uniform("changepoints_range", 0.8, 0.95, 0.001), "n_changepoints": trial.suggest_int("n_changepoints", 20, 35), "seasonality_mode": "additive", "yearly_seasonality": False, "weekly_seasonality": True, "daily_seasonality": True, "loss_func": "MSE", } # fit_model m = NeuralProphet(**params) m.fit(train, freq="1D") future = m.make_future_dataframe(train, periods=len(valid), n_historic_predictions=True) forecast = m.predict(future) valid_forecast = forecast[forecast.y.isna()] val_rmse = mean_squared_error(valid_forecast.yhat1, valid, squared=False) return val_rmse
def test_ar_deep(self): log.info("testing: AR-Net (deep)") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) m = NeuralProphet( n_forecasts=7, n_lags=14, num_hidden_layers=2, d_hidden=32, yearly_seasonality=False, weekly_seasonality=False, daily_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) m.highlight_nth_step_ahead_of_each_forecast(m.n_forecasts) metrics_df = m.fit(df, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df, n_historic_predictions=90) forecast = m.predict(df=future) if self.plot: m.plot_last_forecast(forecast, include_previous_forecasts=3) m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_lag_reg(self): log.info("testing: Lagged Regressors") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) m = NeuralProphet( n_forecasts=2, n_lags=3, weekly_seasonality=False, daily_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) df["A"] = df["y"].rolling(7, min_periods=1).mean() df["B"] = df["y"].rolling(30, min_periods=1).mean() m = m.add_lagged_regressor(names="A") m = m.add_lagged_regressor(names="B", only_last_value=True) metrics_df = m.fit(df, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df, n_historic_predictions=10) forecast = m.predict(future) if self.plot: print(forecast.to_string()) m.plot_last_forecast(forecast, include_previous_forecasts=5) m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_no_trend(self): log.info("testing: No-Trend") df = pd.read_csv(PEYTON_FILE, nrows=512) m = NeuralProphet( growth="off", yearly_seasonality=False, weekly_seasonality=False, daily_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) # m.highlight_nth_step_ahead_of_each_forecast(m.n_forecasts) metrics_df = m.fit(df, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df, periods=60, n_historic_predictions=60) forecast = m.predict(df=future) if self.plot: m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_seasons(self): log.info("testing: Seasonality: additive") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) # m = NeuralProphet(n_lags=60, n_changepoints=10, n_forecasts=30, verbose=True) m = NeuralProphet( yearly_seasonality=8, weekly_seasonality=4, seasonality_mode="additive", seasonality_reg=1, epochs=EPOCHS, batch_size=BATCH_SIZE, ) metrics_df = m.fit(df, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df, n_historic_predictions=365, periods=365) forecast = m.predict(df=future) log.debug("SUM of yearly season params: {}".format( sum(abs(m.model.season_params["yearly"].data.numpy())))) log.debug("SUM of weekly season params: {}".format( sum(abs(m.model.season_params["weekly"].data.numpy())))) log.debug("season params: {}".format(m.model.season_params.items())) if self.plot: m.plot(forecast) # m.plot_components(forecast) m.plot_parameters() plt.show() log.info("testing: Seasonality: multiplicative") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) # m = NeuralProphet(n_lags=60, n_changepoints=10, n_forecasts=30, verbose=True) m = NeuralProphet( yearly_seasonality=8, weekly_seasonality=4, seasonality_mode="multiplicative", epochs=EPOCHS, batch_size=BATCH_SIZE, ) metrics_df = m.fit(df, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df, n_historic_predictions=365, periods=365) forecast = m.predict(df=future)
train_data = pd.read_csv('c:/data/dacon/solar/energy.csv') # 시간 변환 train_data['time'] = train_data['time'].apply(lambda x: convert_time(x)) energy_list = ['dangjin_floating', 'dangjin_warehouse', 'dangjin', 'ulsan'] submission = pd.read_csv('c:/data/dacon/solar/sample_submission.csv') for name in energy_list: print(name) column = name df = pd.DataFrame() df['ds'] = train_data['time'] df['y'] = train_data[column] # 모델 설정 model = NeuralProphet() # 훈련 loss = model.fit(df, freq="H") # 예측용 데이터 프레임 만들기 df_pred = model.make_future_dataframe(df, periods=18000) # 예측 predict = model.predict(df_pred) # 2021-02-01 ~ 2021-03-01 predict_1 = predict.copy() predict_1 = predict_1.query('ds >= "2021-02-01 00:00:00"') predict_1 = predict_1.query('ds < "2021-03-01 00:00:00"') # 2021-06-09 ~ 2021-07-09 predict_2 = predict.copy() predict_2 = predict_2.query('ds >= "2021-06-09 00:00:00"')
import pandas as pd from neuralprophet import NeuralProphet, LSTM, DeepAR, TFT, NBeats from neuralprophet.utils.utils import set_log_level # set_log_level("ERROR") df = pd.read_csv("example_data/yosemite_temps.csv") df.head(3) df = df.iloc[:1000] # runs NeuralProphet on sample data m = NeuralProphet(n_lags=12, n_forecasts=3, epochs=10, learning_rate=1) metrics_NP = m.fit(df, freq="5min", validate_each_epoch=True) # print(metrics) m = LSTM(n_lags=12, n_forecasts=3, num_hidden_layers=1, d_hidden=64, learning_rate=1, epochs=10) metrics_LSTM = m.fit(df, freq="5min", validate_each_epoch=True) m = NBeats(n_lags=12, n_forecasts=3, epochs=10, learning_rate=1) metrics_NBeats = m.fit(df, freq="5min") m = DeepAR(n_lags=12, n_forecasts=3, epochs=10, learning_rate=1) metrics_DeepAR = m.fit(df, freq="5min")
def test_predict(self): log.info("testing: Predict") df = pd.read_csv(PEYTON_FILE, nrows=512) m = NeuralProphet( n_forecasts=3, n_lags=5, epochs=1, ) metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df, periods=None, n_historic_predictions=len(df) - m.n_lags) forecast = m.predict(future) if self.plot: m.plot_last_forecast(forecast, include_previous_forecasts=10) m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
t1 = process_time() df_pred_prophet = model_prophet.predict(df_test) predict_time.append(process_time() - t1) # %% [markdown] # We do the same for `neuralprophet`. # The API is essentially the same as `prophet` so it's easy to switch in. # We set the number of changepoints to the same as the `fbprophet` default of 25. # The time spent training is dependent on the number of epochs we train for. # The default number is a function of the data length, but can be manually overridden. # %% Train neural prophet fit_time_neural = [] predict_time_neural = [] for ii in range(trials): t1 = process_time() model_nprophet = NeuralProphet(trend_reg=1.0, n_changepoints=25, log_level='WARNING') model_nprophet.fit(df_train, freq="D") fit_time_neural.append(process_time() - t1) t1 = process_time() future_nprophet = model_nprophet.make_future_dataframe( df=df_train.iloc[[-1]], periods=df_test.shape[0], ) df_pred_nprophet = model_nprophet.predict(future_nprophet) predict_time_neural.append(process_time() - t1) # %% [markdown] # ### Plotting results # Neural prophet is a fair bit slower to train, but significantly faster for predicting. # %% Compare running times
def test_air_data(self): log.info("TEST air_passengers.csv") df = pd.read_csv(AIR_FILE) m = NeuralProphet( n_changepoints=0, yearly_seasonality=2, seasonality_mode="multiplicative", epochs=EPOCHS, batch_size=BATCH_SIZE, ) metrics = m.fit(df, freq="MS") future = m.make_future_dataframe(df, periods=48, n_historic_predictions=len(df) - m.n_lags) forecast = m.predict(future) if self.plot: m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
freq = 10 df["x1"] = np.sin( np.linspace(start=0, stop=freq * 2 * np.math.pi, num=df.shape[0])) freq = 3 df["x2"] = np.sin( np.linspace(start=0, stop=freq * 2 * np.math.pi, num=df.shape[0])) df["y"] = df["x1"] + df["x2"] df.set_index("ds")["y"].plot() df_train = df.iloc[:int(df.shape[0] / 2)] df_test = df.iloc[int(df.shape[0] / 2):] # %% t1 = process_time() model_nprophet = NeuralProphet() model_nprophet = NeuralProphet(n_lags=100, n_forecasts=10) model_nprophet.add_future_regressor("x1") model_nprophet.add_future_regressor("x2") model_nprophet.fit(df_train, freq="D") t2 = process_time() - t1 t3 = process_time() future_nprophet = model_nprophet.make_future_dataframe( df=df_train, #.iloc[[-1]], regressors_df=df_test[["x1", "x2"]], periods=df_test.shape[0], ) df_pred_nprophet = model_nprophet.predict(future_nprophet) t4 = process_time() - t3 print(t2, t4)
def test_future_reg(self): log.info("testing: Future Regressors") df = pd.read_csv(PEYTON_FILE, nrows=NROWS + 50) m = NeuralProphet( epochs=EPOCHS, batch_size=BATCH_SIZE, ) df["A"] = df["y"].rolling(7, min_periods=1).mean() df["B"] = df["y"].rolling(30, min_periods=1).mean() regressors_df_future = pd.DataFrame(data={ "A": df["A"][-50:], "B": df["B"][-50:] }) df = df[:-50] m = m.add_future_regressor(name="A") m = m.add_future_regressor(name="B", mode="multiplicative") metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df=df, regressors_df=regressors_df_future, n_historic_predictions=10, periods=50) forecast = m.predict(df=future) if self.plot: m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_names(self): log.info("testing: names") m = NeuralProphet() m._validate_column_name("hello_friend")
def test_global_modeling_no_exogenous_variable(): ### GLOBAL MODELLING - NO EXOGENOUS VARIABLE log.info("Global Modeling - No exogenous variables") df = pd.read_csv(PEYTON_FILE, nrows=512) df1_0 = df.iloc[:128, :].copy(deep=True) df2_0 = df.iloc[128:256, :].copy(deep=True) df3_0 = df.iloc[256:384, :].copy(deep=True) df4_0 = df.iloc[384:, :].copy(deep=True) train_input = { 0: df1_0, 1: { "df1": df1_0, "df2": df2_0 }, 2: { "df1": df1_0, "df2": df2_0 } } test_input = {0: df3_0, 1: {"df1": df3_0}, 2: {"df1": df3_0, "df2": df4_0}} info_input = { 0: "Testing df train / df test - no events, no regressors", 1: "Testing dict df train / df test - no events, no regressors", 2: "Testing dict df train / dict df test - no events, no regressors", } for i in range(0, 3): log.info(info_input[i]) m = NeuralProphet(n_forecasts=2, n_lags=10, epochs=EPOCHS, batch_size=BATCH_SIZE) metrics = m.fit(train_input[i], freq="D") forecast = m.predict(df=test_input[i]) forecast_trend = m.predict_trend(df=test_input[i]) forecast_seasonal_componets = m.predict_seasonal_components( df=test_input[i]) if PLOT: forecast = forecast if isinstance(forecast, list) else [forecast] for key in forecast: fig1 = m.plot(forecast[key]) fig2 = m.plot(forecast[key]) with pytest.raises(ValueError): forecast = m.predict({"df4": df4_0}) log.info( "Error - dict with names not provided in the train dict (not in the data params dict)" ) with pytest.raises(ValueError): metrics = m.test({"df4": df4_0}) log.info( "Error - dict with names not provided in the train dict (not in the data params dict)" ) m = NeuralProphet( n_forecasts=2, n_lags=10, epochs=EPOCHS, batch_size=BATCH_SIZE, ) m.fit({"df1": df1_0, "df2": df2_0}, freq="D") with pytest.raises(ValueError): forecast = m.predict({"df4": df4_0}) # log.info("unknown_data_normalization was not set to True") with pytest.raises(ValueError): metrics = m.test({"df4": df4_0}) # log.info("unknown_data_normalization was not set to True") with pytest.raises(ValueError): forecast_trend = m.predict_trend({"df4": df4_0}) # log.info("unknown_data_normalization was not set to True") with pytest.raises(ValueError): forecast_seasonal_componets = m.predict_seasonal_components( {"df4": df4_0}) # log.info("unknown_data_normalization was not set to True") # Set unknown_data_normalization to True - now there should be no errors m.config_normalization.unknown_data_normalization = True forecast = m.predict({"df4": df4_0}) metrics = m.test({"df4": df4_0}) forecast_trend = m.predict_trend({"df4": df4_0}) forecast_seasonal_componets = m.predict_seasonal_components({"df4": df4_0}) m.plot_parameters(df_name="df1") m.plot_parameters()
def test_ar_sparse(): log.info("testing: AR (sparse") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) m = NeuralProphet( n_forecasts=3, n_lags=14, ar_sparsity=0.5, yearly_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) m.highlight_nth_step_ahead_of_each_forecast(m.n_forecasts) metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df, n_historic_predictions=90) forecast = m.predict(df=future) if PLOT: m.plot_last_forecast(forecast, include_previous_forecasts=3) m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
) # Product_1766.plot(x='Date', y='Order_Demand') # plt.show() Product_1766 = (Product_1766[['Date','Order_Demand']] .rename(columns={'Date':'ds', 'Order_Demand':'y'}) ) # print(Product_1766.describe()) # print(Product_1766.tail(200)) m = NeuralProphet( normalize='standardize', num_hidden_layers = 1, n_forecasts=60, n_lags=2, #seasonality_mode="multiplicative", epochs=10 ) m.fit(Product_1766, freq='D') future = m.make_future_dataframe(Product_1766, periods=60, n_historic_predictions=len(Product_1766)) forecast = m.predict(future) m.plot(forecast) plt.show()
def test_events(self): log.info("testing: Events") df = pd.read_csv(PEYTON_FILE)[-NROWS:] playoffs = pd.DataFrame({ "event": "playoff", "ds": pd.to_datetime([ "2008-01-13", "2009-01-03", "2010-01-16", "2010-01-24", "2010-02-07", "2011-01-08", "2013-01-12", "2014-01-12", "2014-01-19", "2014-02-02", "2015-01-11", "2016-01-17", "2016-01-24", "2016-02-07", ]), }) superbowls = pd.DataFrame({ "event": "superbowl", "ds": pd.to_datetime(["2010-02-07", "2014-02-02", "2016-02-07"]), }) events_df = pd.concat((playoffs, superbowls)) m = NeuralProphet( n_lags=2, n_forecasts=30, daily_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) # set event windows m = m.add_events(["superbowl", "playoff"], lower_window=-1, upper_window=1, mode="multiplicative", regularization=0.5) # add the country specific holidays m = m.add_country_holidays("US", mode="additive", regularization=0.5) history_df = m.create_df_with_events(df, events_df) metrics_df = m.fit(history_df, freq="D") future = m.make_future_dataframe(df=history_df, events_df=events_df, periods=30, n_historic_predictions=90) forecast = m.predict(df=future) log.debug("Event Parameters:: {}".format(m.model.event_params)) if self.plot: m.plot_components(forecast) m.plot(forecast) m.plot_parameters() plt.show()
def test_ar_net(self): log.info("testing: AR-Net") df = pd.read_csv(PEYTON_FILE) m = NeuralProphet( n_forecasts=7, n_lags=14, # ar_sparsity=0.01, # num_hidden_layers=0, num_hidden_layers=2, d_hidden=64, # yearly_seasonality=False, # weekly_seasonality=False, # daily_seasonality=False, epochs=EPOCHS, ) m.highlight_nth_step_ahead_of_each_forecast(m.n_forecasts) metrics_df = m.fit(df, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df, n_historic_predictions=len(df) - m.n_lags) forecast = m.predict(df=future) if self.plot: m.plot_last_forecast(forecast, include_previous_forecasts=3) m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_plot(self): log.info("testing: Plotting") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) m = NeuralProphet( n_forecasts=7, n_lags=14, epochs=EPOCHS, batch_size=BATCH_SIZE, ) metrics_df = m.fit(df, freq="D") m.highlight_nth_step_ahead_of_each_forecast(7) future = m.make_future_dataframe(df, n_historic_predictions=10) forecast = m.predict(future) m.plot(forecast) m.plot_last_forecast(forecast, include_previous_forecasts=10) m.plot_components(forecast) m.plot_parameters() m.highlight_nth_step_ahead_of_each_forecast(None) future = m.make_future_dataframe(df, n_historic_predictions=10) forecast = m.predict(future) m.plot(forecast) m.plot_last_forecast(forecast) m.plot_components(forecast) m.plot_parameters() if self.plot: plt.show()
def test_lag_reg(self): log.info("testing: Lagged Regressors") df = pd.read_csv(PEYTON_FILE) m = NeuralProphet( n_forecasts=3, n_lags=7, ar_sparsity=0.1, # num_hidden_layers=2, # d_hidden=64, # yearly_seasonality=False, # weekly_seasonality=False, # daily_seasonality=False, epochs=EPOCHS, ) if m.n_lags > 0: df["A"] = df["y"].rolling(7, min_periods=1).mean() df["B"] = df["y"].rolling(30, min_periods=1).mean() m = m.add_lagged_regressor(name="A") m = m.add_lagged_regressor(name="B", only_last_value=True) # m.highlight_nth_step_ahead_of_each_forecast(m.n_forecasts) metrics_df = m.fit(df, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df, n_historic_predictions=365) forecast = m.predict(future) if self.plot: # print(forecast.to_string()) m.plot_last_forecast(forecast, include_previous_forecasts=10) m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_random_seed(self): log.info("TEST random seed") df = pd.read_csv(PEYTON_FILE, nrows=512) set_random_seed(0) m = NeuralProphet( epochs=EPOCHS, batch_size=BATCH_SIZE, ) metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df, periods=10, n_historic_predictions=10) forecast = m.predict(future) checksum1 = sum(forecast["yhat1"].values) set_random_seed(0) m = NeuralProphet( epochs=EPOCHS, batch_size=BATCH_SIZE, ) metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df, periods=10, n_historic_predictions=10) forecast = m.predict(future) checksum2 = sum(forecast["yhat1"].values) set_random_seed(1) m = NeuralProphet( epochs=EPOCHS, batch_size=BATCH_SIZE, ) metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df, periods=10, n_historic_predictions=10) forecast = m.predict(future) checksum3 = sum(forecast["yhat1"].values) log.debug("should be same: {} and {}".format(checksum1, checksum2)) log.debug("should not be same: {} and {}".format(checksum1, checksum3)) assert math.isclose(checksum1, checksum2) assert not math.isclose(checksum1, checksum3)
def test_future_reg(self): log.info("testing: Future Regressors") df = pd.read_csv(PEYTON_FILE) m = NeuralProphet( n_forecasts=1, n_lags=0, epochs=EPOCHS, ) df["A"] = df["y"].rolling(7, min_periods=1).mean() df["B"] = df["y"].rolling(30, min_periods=1).mean() m = m.add_future_regressor(name="A", regularization=0.5) m = m.add_future_regressor(name="B", mode="multiplicative", regularization=0.3) metrics_df = m.fit(df, freq="D") regressors_df = pd.DataFrame(data={ "A": df["A"][:50], "B": df["B"][:50] }) future = m.make_future_dataframe(df=df, regressors_df=regressors_df, n_historic_predictions=10, periods=50) forecast = m.predict(df=future) if self.plot: # print(forecast.to_string()) # m.plot_last_forecast(forecast, include_previous_forecasts=3) m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()