예제 #1
0
def test_add_lagged_regressors():
    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", validation_df=df1[-100:])
        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)
예제 #2
0
 def test_trend(self):
     log.info("testing: Trend")
     df = pd.read_csv(PEYTON_FILE, nrows=NROWS)
     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,
         batch_size=BATCH_SIZE,
     )
     # print(m.config_trend)
     metrics_df = m.fit(df, freq="D")
     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()
예제 #3
0
 def check_cv(df, freq, n_lags, n_forecasts, k, fold_pct,
              fold_overlap_pct):
     m = NeuralProphet(
         n_lags=n_lags,
         n_forecasts=n_forecasts,
     )
     folds = m.crossvalidation_split_df(
         df,
         freq=freq,
         k=k,
         fold_pct=fold_pct,
         fold_overlap_pct=fold_overlap_pct)
     total_samples = len(df) - m.n_lags + 2 - (2 * m.n_forecasts)
     per_fold = int(fold_pct * total_samples)
     not_overlap = per_fold - int(fold_overlap_pct * per_fold)
     assert all([
         per_fold == len(val) - m.n_lags + 1 - m.n_forecasts
         for (train, val) in folds
     ])
     assert all([
         total_samples - per_fold -
         (k - i - 1) * not_overlap == len(train) - m.n_lags + 1 -
         m.n_forecasts for i, (train, val) in enumerate(folds)
     ])
예제 #4
0
 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()
    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_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()
예제 #7
0
                    )

    # 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()
    
예제 #8
0
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)
예제 #9
0
def benchmark(model, method, df, params, one_dataset_metrics, n_epochs):
    '''
    Args:
        model: model name, LSTM, NP, DeepAR, TFT or NBeats
        method: str, 'onestep' or 'multistep'
        df: dataframe with time series
        params: corresponding parameters
        one_dataset_metrics: dictionary with metrics
        n_epochs: number of epochs for training of a model

    Returns: updates metrics dictionary

    '''
    valid_p = 0.2
    n_lags = params["n_lags"]
    freq = params["freq"]
    n_forecasts = params["n_forecasts"]
    try:
        if model == "LSTM":
            m = LSTM(n_lags=n_lags,
                     n_forecasts=n_forecasts,
                     learning_rate=0.01,
                     epochs=n_epochs)
        elif model == "NP":
            m = NeuralProphet(n_lags=n_lags,
                              n_forecasts=n_forecasts,
                              learning_rate=0.01,
                              epochs=n_epochs)
        elif model == "DeepAR":
            m = DeepAR(n_lags=int(n_lags),
                       n_forecasts=n_forecasts,
                       learning_rate=0.01,
                       epochs=n_epochs,
                       auto_lr_find=False)
        elif model == "NBeats":
            m = NBeats(n_lags=int(n_lags),
                       n_forecasts=n_forecasts,
                       learning_rate=0.01,
                       epochs=n_epochs)
        elif model == "TFT":
            m = TFT(n_lags=int(n_lags),
                    n_forecasts=n_forecasts,
                    learning_rate=0.01,
                    epochs=n_epochs)

        tr, vl = split_df(df,
                          n_lags=n_lags,
                          n_forecasts=n_forecasts,
                          valid_p=valid_p)
        m.fit(tr, freq=freq)
        future = m.make_future_dataframe(vl,
                                         periods=0,
                                         n_historic_predictions=True)
        forecast = m.predict(future)
        fold = forecast.iloc[n_lags:][[
            f"yhat{i}" for i in range(1, n_forecasts + 1)
        ]]

        y_predicted = [
            np.array(fold).diagonal(offset=-i)
            for i in range(len(fold) - n_forecasts + 1)
        ]
        y = np.array(vl[n_lags:]["y"])
        y_rolled = [
            y[i:i + n_forecasts] for i in range(len(y) - n_forecasts + 1)
        ]

        y_naive = np.array(vl[n_lags - 1:-1]["y"])
        y_naive_rolled = [
            y_naive[i:i + n_forecasts]
            for i in range(len(y_naive) - n_forecasts + 1)
        ]

        smapes = np.mean(
            [smape(y_rolled[i], y_predicted[i]) for i in range(len(y_rolled))])
        mases = np.mean([
            mase(y_rolled[i], y_predicted[i], y_naive_rolled[i])
            for i in range(len(y_rolled))
        ])
        mueses = np.mean(
            [mues(y_rolled[i], y_predicted[i]) for i in range(len(y_rolled))])
        moeses = np.mean(
            [moes(y_rolled[i], y_predicted[i]) for i in range(len(y_rolled))])
        muases = np.mean(
            [muas(y_rolled[i], y_predicted[i]) for i in range(len(y_rolled))])
        moases = np.mean(
            [moas(y_rolled[i], y_predicted[i]) for i in range(len(y_rolled))])

        one_dataset_metrics.update({
            f"smape_{model}_{method}": smapes,
            f"mase_{model}_{method}": mases,
            f"mues_{model}_{method}": mueses,
            f"moes_{model}_{method}": moeses,
            f"muas_{model}_{method}": muases,
            f"moas_{model}_{method}": moases,
        })

    except:
        print("error")
예제 #10
0
 def test_names(self):
     log.info("testing: names")
     m = NeuralProphet()
     m._validate_column_name("hello_friend")
import pandas as pd
from neuralprophet import NeuralProphet, set_log_level
import matplotlib.pyplot as plt

set_log_level("ERROR")
df = pd.read_csv("~/DEVELOPMENT/DSDE/NP/RESOURCES/DATA/yosemite_temps.csv",
                 parse_dates=['ds'])
# print(df.info())
# print(df.head())

m = NeuralProphet(
    n_lags=12,
    changepoints_range=0.95,
    n_changepoints=30,
    weekly_seasonality=False,
    batch_size=64,
    epochs=10,
    learning_rate=1.0,
)
metrics = m.fit(df, freq='5min')

future = m.make_future_dataframe(df, n_historic_predictions=True)
forecast = m.predict(future)
fig = m.plot(forecast)

plt.show()
'''
Multi-step forecast
To predict multiple steps into the future, we could 'unroll' our single-step model, by predicting a step ahead, adding the forecasted value to our data, and then forecasting the next step until we reach the horizon we are interested in. However, there is a better way to do this: We can directly forecast multiple steps ahead with NeuralProphet.

We can set n_forecasts to the desired number of steps we would like to forecast (also known as 'forecast horizon'). NeuralProphet will forecast n_forecasts steps into the future, at every single step. Thus, we have n_forecasts overlapping predictions of vaying age at every historic point.
              on='date').reset_index(drop=True)
df.isnull().sum()
df.tail(10)
df.head(10)
df.adj_close.interpolate(
    method='nearest', inplace=True
)  # Ideally no interpolate should be used. Check the occurence of NA
df.dropna(
    axis=0, inplace=True
)  # Incase the first row is Null, it will not get fixed by interpolate and hence removing such scenarios

df['y'] = df.adj_close * df.exchange_rate
df.rename(columns={"date": "ds"}, inplace=True)

df = df[['ds', 'y']]
model = NeuralProphet()

model = NeuralProphet(
    growth="linear",  # Determine trend types: 'linear', 'discontinuous', 'off'
    changepoints=
    None,  # list of dates that may include change points (None -> automatic )
    n_changepoints=5,
    changepoints_range=0.8,
    trend_reg=0,
    trend_reg_threshold=False,
    yearly_seasonality="auto",
    weekly_seasonality="auto",
    daily_seasonality="auto",
    seasonality_mode="additive",
    seasonality_reg=0,
    n_forecasts=1,
예제 #13
0
lag_range = range(1,25)
logging.getLogger("nprophet").setLevel(logging.WARNING)
for lag in lag_range:
    # fit statsmodels
    t1 = process_time()
    model_arima = statsmodels.tsa.arima.model.ARIMA(endog=df_train.set_index('ds'), order=(lag,0,0), freq='1D').fit()
    fit_time_ar.append(process_time() - t1)

    # fit neuralprophet
    t1 = process_time()
    model_nprophet_ar = NeuralProphet(
        growth="off",
        n_changepoints=0,
        n_forecasts=1,
        n_lags=lag,
        daily_seasonality=False,
        weekly_seasonality=False,
        yearly_seasonality=False,
        loss_func='MSE',
        normalize='off'
    )
    mae_np.append(model_nprophet_ar.fit(df_train, freq="D"))
    fit_time_np.append(process_time() - t1)
# %% [markdown]
# Plotting these results we can see that the fitting time for neuralprophet is fairly flat.
# As expected with fitting ARIMA models, the time of fitting increases rapidly with the number of lags.
# This means neuralprophet allows us to fit longer AR based models which would not be otherwise possible.
# %% plot results
fig, ax = plt.subplots(figsize=(10,6))
ax.plot(lag_range[:-1], fit_time_ar[:-1], '-x',label='ARIMA')
ax.plot(lag_range[:-1], fit_time_np[:-1], '-x',label='NeuralProphet')
예제 #14
0
def time_pattern():
    global target, daypara, df, df2, df_4pycaret, df_temp
    EPOCH = st.sidebar.slider("Epochs", 100, 1000)
    model = NeuralProphet(
        growth="linear",
        changepoints=None,
        n_changepoints=30,
        changepoints_range=0.95,
        trend_reg=0,
        trend_reg_threshold=False,
        yearly_seasonality="auto",
        weekly_seasonality=True,
        daily_seasonality="auto",
        seasonality_mode="additive",
        seasonality_reg=0,
        n_forecasts=30,
        n_lags=60,  ##determines autoregression 
        num_hidden_layers=0,
        d_hidden=None,
        ar_sparsity=None,
        learning_rate=None,
        epochs=EPOCH,
        loss_func="Huber",
        normalize="auto",
        impute_missing=True,
    )
    metrics = model.fit(df2, validate_each_epoch=True, freq="D")
    future = model.make_future_dataframe(df2,
                                         periods=252,
                                         n_historic_predictions=len(df2))
    with st.spinner("Training..."):
        forecast = model.predict(future)
        fig, ax = plt.subplots(1, 2, figsize=(17, 7))
        ax[0].plot(metrics["MAE"], 'ob', linewidth=6, label="Training Loss")
        ax[0].plot(metrics["MAE_val"],
                   '-r',
                   linewidth=2,
                   label="Validation Loss")
        ax[0].legend(loc='center right')
        ax[0].tick_params(axis='both', which='major')
        ax[0].set_xlabel("Epoch")
        ax[0].set_ylabel("Loss")
        ax[0].set_title("Model Loss (MAE)")
        ax[1].plot(metrics["SmoothL1Loss"],
                   'ob',
                   linewidth=6,
                   label="Training Loss")
        ax[1].plot(metrics["SmoothL1Loss_val"],
                   '-r',
                   linewidth=2,
                   label="Validation Loss")
        ax[1].legend(loc='center right')
        ax[1].tick_params(axis='both', which='major')
        ax[1].set_xlabel("Epoch")
        ax[1].set_ylabel("Loss")
        ax[1].set_title("Model Loss (SmoothL1Loss)")
        st.subheader("Loss Check")
        st.pyplot()
        with st.spinner("Recognizing Time Pattern"):
            st.subheader("Time Pattern")
            model.plot_parameters()
            st.set_option('deprecation.showPyplotGlobalUse', False)
            st.pyplot()
예제 #15
0
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"')
예제 #16
0
 def seek_the_oracle(current_series, args, series, forecast_length,
                     future_regressor):
     """Prophet for for loop or parallel."""
     current_series = current_series.rename(columns={series: 'y'})
     current_series['ds'] = current_series.index
     try:
         quant_range = (1 - args['prediction_interval']) / 2
         quantiles = [quant_range, 0.5, (1 - quant_range)]
         m = NeuralProphet(
             quantiles=quantiles,
             growth=self.growth,
             n_changepoints=self.n_changepoints,
             changepoints_range=self.changepoints_range,
             trend_reg=self.trend_reg,
             trend_reg_threshold=self.trend_reg_threshold,
             ar_sparsity=self.ar_sparsity,
             yearly_seasonality=self.yearly_seasonality,
             weekly_seasonality=self.weekly_seasonality,
             daily_seasonality=self.daily_seasonality,
             seasonality_mode=self.seasonality_mode,
             seasonality_reg=self.seasonality_reg,
             n_lags=self.n_lags,
             n_forecasts=forecast_length,
             num_hidden_layers=self.num_hidden_layers,
             d_hidden=self.d_hidden,
             learning_rate=self.learning_rate,
             loss_func=self.loss_func,
             train_speed=self.train_speed,
             normalize=self.normalize,
             collect_metrics=False,
         )
     except Exception:
         m = NeuralProphet(
             growth=self.growth,
             n_changepoints=self.n_changepoints,
             changepoints_range=self.changepoints_range,
             trend_reg=self.trend_reg,
             trend_reg_threshold=self.trend_reg_threshold,
             ar_sparsity=self.ar_sparsity,
             yearly_seasonality=self.yearly_seasonality,
             weekly_seasonality=self.weekly_seasonality,
             daily_seasonality=self.daily_seasonality,
             seasonality_mode=self.seasonality_mode,
             seasonality_reg=self.seasonality_reg,
             n_lags=self.n_lags,
             n_forecasts=forecast_length,
             num_hidden_layers=self.num_hidden_layers,
             d_hidden=self.d_hidden,
             learning_rate=self.learning_rate,
             loss_func=self.loss_func,
             train_speed=self.train_speed,
             normalize=self.normalize,
             collect_metrics=False,
         )
     if args['holiday']:
         m.add_country_holidays(country_name=args['holiday_country'])
     if args['regression_type'] == 'User':
         current_series = pd.concat(
             [current_series, args['regressor_train']], axis=1)
         for nme in args['regressor_name']:
             m.add_future_regressor(nme)
     m.fit(current_series,
           freq=args['freq'],
           progress_print=False,
           minimal=True)
     if args['regression_type'] == 'User':
         if future_regressor.ndim > 1:
             if future_regressor.shape[1] > 1:
                 ft_regr = (future_regressor.mean(
                     axis=1).to_frame().merge(
                         future_regressor.std(axis=1).to_frame(),
                         left_index=True,
                         right_index=True,
                     ))
             else:
                 ft_regr = future_regressor.copy()
             ft_regr.columns = args['regressor_train'].columns
             regr = pd.concat([args['regressor_train'], ft_regr])
             regr.columns = args['regressor_train'].columns
             # regr.index.name = 'ds'
             # regr.reset_index(drop=False, inplace=True)
             # future = future.merge(regr, on="ds", how='left')
         else:
             # a = np.append(args['regressor_train'], future_regressor.values)
             regr = future_regressor
         future = m.make_future_dataframe(current_series,
                                          periods=forecast_length,
                                          regressors_df=regr)
     else:
         future = m.make_future_dataframe(current_series,
                                          periods=forecast_length)
     fcst = m.predict(future, decompose=False)
     fcst = fcst.tail(forecast_length)  # remove the backcast
     # predicting that someday they will change back to fbprophet format
     if "yhat2" in fcst.columns:
         fcst['yhat1'] = fcst.fillna(0).sum(axis=1, numeric_only=True)
     try:
         forecast = fcst['yhat1']
     except Exception:
         forecast = fcst['yhat']
     forecast.name = series
     # not yet supported, so fill with the NaN column for now if missing
     try:
         lower_forecast = fcst['yhat_lower']
         upper_forecast = fcst['yhat_upper']
     except Exception:
         lower_forecast = fcst['y']
         upper_forecast = fcst['y']
     lower_forecast.name = series
     upper_forecast.name = series
     return (forecast, lower_forecast, upper_forecast)
예제 #17
0
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")


예제 #18
0
                                     axis='columns')
#print(aq_df_final.head())
shunyi = aq_df_final.query('station == "Shunyi"')[["ds", 'y']]
print(shunyi.head())

# stations = aq_df_final.groupby('station')
# print(stations.head())
target = pd.DataFrame()

# for station in stations.groups:
#     group = stations.get_group(station)
#     print(station)
m = NeuralProphet(
    n_forecasts=366,
    n_lags=2,
    changepoints_range=0.85,
    n_changepoints=30,
    epochs=10,
)
m.fit(shunyi, freq='D')

future = m.make_future_dataframe(shunyi, periods=366)
forecast = m.predict(future)
m.plot(forecast)
forecast = forecast.rename(columns={'yhat': 'yhat_' + 'shunyi'})
target = pd.merge(target,
                  forecast.set_index('ds'),
                  how='outer',
                  left_index=True,
                  right_index=True)
plt.show()
예제 #19
0
    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()
예제 #20
0
 def check_simple(df):
     m = NeuralProphet()
     folds = m.crossvalidation_split_df(df, freq="D", k=5, fold_pct=0.1, fold_overlap_pct=0.5)
     assert all([70 + i * 5 == len(train) for i, (train, val) in enumerate(folds)])
     assert all([10 == len(val) for (train, val) in folds])