Пример #1
0
def average(input_df, kunag, matnr):
    """
    applies average model and calculates mse score on test data
    :param input_df:
    :param kunag:
    :param matnr:
    :return:
    """
    df = input_df.copy()
    df = remove_negative_rows(df)
    df_series = individual_series(df, kunag, matnr)
    df_series = data_transformation.get_weekly_aggregate(df_series)
    df_series["date"] = df_series["dt_week"].map(str)
    df_series["date"] = df_series["date"].apply(lambda x: x.replace("-", ""))
    df_series["prediction"] = df_series["quantity"]
    df_series_train, df_series_test = splitter(df_series)
    k = 0
    for index, row in df_series_test.iterrows():
        row["prediction"] = df_series_train["quantity"].mean()
        df_series_train = pd.concat([df_series_train,
                                     pd.DataFrame(row).T
                                     ]).reset_index(drop=True)
        if k == 0:
            test_index = df_series_train.shape[0] - 1
            k = 1
    output_df = df_series_train
    test_df = df_series_train.iloc[test_index:]
    # print("mean squared error is :",mean_squared_error(output_df["quantity"], output_df["prediction"]))
    return output_df, mean_squared_error(test_df["quantity"],
                                         test_df["prediction"])
Пример #2
0
def arima(input_df, kunag, matnr, p=2, d=1, q=4, trend="n"):
    df = input_df.copy()
    df = remove_negative_rows(df)
    df_series = individual_series(df, kunag, matnr)
    df_series = data_transformation.get_weekly_aggregate(df_series)
    df_series["date"] = df_series["dt_week"].map(str)
    df_series["date"] = df_series["date"].apply(lambda x: x.replace("-", ""))
    df_series["prediction"] = df_series["quantity"]
    df_series_train, df_series_test = splitter(df_series)
    k = 0
    for index, row in df_series_test.iterrows():
        df_series_train["quantity"] = df_series_train["quantity"].map(float)
        fit1 = sm.tsa.statespace.SARIMAX(df_series_train["quantity"],
                                         order=(p, d, q),
                                         trend=trend).fit()
        predicted = fit1.forecast(1)
        row["prediction"] = predicted.values[0]
        df_series_train = pd.concat([df_series_train,
                                     pd.DataFrame(row).T
                                     ]).reset_index(drop=True)
        if k == 0:
            test_index = df_series_train.shape[0] - 1
            k = 1
    output_df = df_series_train
    test_df = df_series_train.iloc[test_index:]
    # print("mean squared error is :",mean_squared_error(output_df["quantity"], output_df["prediction"]))
    return output_df, mean_squared_error(test_df["quantity"],
                                         test_df["prediction"])
Пример #3
0
def holts_linear_trend(input_df,
                       kunag,
                       matnr,
                       smoothing_level=0.3,
                       smoothing_slope=0.1):
    df = input_df.copy()
    df = remove_negative_rows(df)
    df_series = individual_series(df, kunag, matnr)
    df_series = data_transformation.get_weekly_aggregate(df_series)
    df_series["date"] = df_series["dt_week"].map(str)
    df_series["date"] = df_series["date"].apply(lambda x: x.replace("-", ""))
    df_series["prediction"] = df_series["quantity"]
    df_series_train, df_series_test = splitter(df_series)
    k = 0
    for index, row in df_series_test.iterrows():
        df_series_train["quantity"] = df_series_train["quantity"].map(float)
        fit1 = Holt(np.asarray(df_series_train["quantity"])).fit(
            smoothing_level=smoothing_level, smoothing_slope=smoothing_slope)
        predicted = fit1.forecast(1)
        row["prediction"] = predicted[0]
        df_series_train = pd.concat([df_series_train,
                                     pd.DataFrame(row).T
                                     ]).reset_index(drop=True)
        if k == 0:
            test_index = df_series_train.shape[0] - 1
            k = 1
    output_df = df_series_train
    test_df = df_series_train.iloc[test_index:]
    # print("mean squared error is :",mean_squared_error(output_df["quantity"], output_df["prediction"]))
    return output_df, mean_squared_error(test_df["quantity"],
                                         test_df["prediction"])
Пример #4
0
def simple_exponential_smoothing(input_df, kunag, matnr, alpha=0.6):

    df = input_df.copy()
    df = remove_negative_rows(df)
    df_series = individual_series(df, kunag, matnr)
    df_series = data_transformation.get_weekly_aggregate(df_series)
    df_series["date"] = df_series["dt_week"].map(str)
    df_series["date"] = df_series["date"].apply(lambda x: x.replace("-", ""))
    df_series["prediction"] = df_series["quantity"]
    df_series_train, df_series_test = splitter(df_series)
    k = 0
    for index, row in df_series_test.iterrows():
        fit2 = SimpleExpSmoothing(np.asarray(df_series_train["quantity"])).fit(
            smoothing_level=alpha, optimized=False)
        row["prediction"] = fit2.forecast(1)
        df_series_train = pd.concat([df_series_train,
                                     pd.DataFrame(row).T
                                     ]).reset_index(drop=True)
        if k == 0:
            test_index = df_series_train.shape[0] - 1
            k = 1
    output_df = df_series_train
    test_df = df_series_train.iloc[test_index:]
    # print("mean squared error is :",mean_squared_error(output_df["quantity"], output_df["prediction"]))
    return output_df, mean_squared_error(test_df["quantity"],
                                         test_df["prediction"])
Пример #5
0
def prophet(input_df,
            kunag,
            matnr,
            growth="linear",
            changepoint_prior_scale=0.5,
            yearly_seasonality=False,
            daily_seasonality=False,
            weekly_seasonality=False,
            seasonality_prior_scale=100):
    df = input_df.copy()
    df = remove_negative_rows(df)
    df_series = individual_series(
        df,
        kunag,
        matnr,
    )
    df_series = data_transformation.get_weekly_aggregate(df_series)
    df_series["date"] = df_series["dt_week"].map(str)
    df_series["date"] = df_series["date"].apply(lambda x: x.replace("-", ""))
    df_series["prediction"] = df_series["quantity"]
    df_series_train, df_series_test = splitter(df_series)
    k = 0
    for index, row in df_series_test.iterrows():
        df_series_train["quantity"] = df_series_train["quantity"].map(float)
        df_copy = df_series_train.copy()
        ts = prophet_preprocess(df_copy)
        m = Prophet(growth=growth,
                    changepoint_prior_scale=changepoint_prior_scale,
                    yearly_seasonality=yearly_seasonality,
                    daily_seasonality=daily_seasonality,
                    weekly_seasonality=weekly_seasonality,
                    seasonality_prior_scale=seasonality_prior_scale)
        m.fit(ts)
        future = m.make_future_dataframe(
            periods=1, freq="W", include_history=False).apply(
                lambda x:
                (x + pd.Timedelta(4, unit="D")))  # + pd.Timedelta(4, unit="D")

        forecast = m.predict(future)
        row["prediction"] = forecast.iloc[0]["yhat"]
        df_series_train = pd.concat([df_series_train,
                                     pd.DataFrame(row).T
                                     ]).reset_index(drop=True)
        if k == 0:
            test_index = df_series_train.shape[0] - 1
            k = 1
    output_df = df_series_train
    test_df = df_series_train.iloc[test_index:]
    return output_df, mean_squared_error(test_df["quantity"],
                                         test_df["prediction"])
Пример #6
0
def holts_winter_method(input_df,
                        kunag,
                        matnr,
                        seasonal_period,
                        alpha=None,
                        beta=None,
                        gamma=None):
    """

    :param input_df:
    :param kunag:
    :param matnr:
    :param seasonal_period:
    :param alpha:
    :param beta:
    :param gamma:
    :return:
    """
    df = input_df.copy()
    df = remove_negative_rows(df)
    df_series = individual_series(df, kunag, matnr)
    df_series = data_transformation.get_weekly_aggregate(df_series)
    df_series["date"] = df_series["dt_week"].map(str)
    df_series["date"] = df_series["date"].apply(lambda x: x.replace("-", ""))
    df_series["prediction"] = df_series["quantity"]
    df_series_train, df_series_test = splitter(df_series)
    k = 0
    for index, row in df_series_test.iterrows():
        df_series_train["quantity"] = df_series_train["quantity"].map(float)
        fit1 = ExponentialSmoothing(
            np.asarray(df_series_train["quantity"]),
            seasonal_periods=seasonal_period,
            trend='add',
            seasonal='add',
        ).fit(smoothing_level=alpha,
              smoothing_slope=beta,
              smoothing_seasonal=gamma)
        predicted = fit1.forecast(1)
        row["prediction"] = predicted[0]
        df_series_train = pd.concat([df_series_train,
                                     pd.DataFrame(row).T
                                     ]).reset_index(drop=True)
        if k == 0:
            test_index = df_series_train.shape[0] - 1
            k = 1
    output_df = df_series_train
    test_df = df_series_train.iloc[test_index:]
    # print("mean squared error is :",mean_squared_error(output_df["quantity"], output_df["prediction"]))
    return output_df, mean_squared_error(test_df["quantity"],
                                         test_df["prediction"])