Esempio n. 1
0
def create_sts_model(train_x, train_y):
    """
    Create model using Gaussian Naive Bayes and save this
    :param train_x: lagged list, regressor
    :param train_y: lagged_list, regressand
    :return: trained model
    """
    model = GaussianNB()
    model.fit(train_x, train_y)
    save_model(model, "simple_time_series")
    return model
Esempio n. 2
0
def feed_forward_create_model(ml_df):
    ml_df = ml_df.copy()
    train_dataset, test_dataset, train_labels, test_labels = preprocess(ml_df)
    normed_train_data, normed_test_data = normalize_dataset(
        train_dataset, test_dataset
    )
    history, model = canteen_model(normed_train_data, train_labels)

    test_labels["prediction"] = model.predict(normed_test_data).flatten()

    mae = mean_absolute_error(
        np.asarray(test_labels["Canteen"]),
        np.asarray(test_labels["prediction"]),
    )
    global FEED_MAE
    if mae < FEED_MAE:
        save_model(history.history, "feed_forward_history")
        save_model(history.epoch, "feed_forward_epoch")
        save_model(train_dataset, "feed_forward_train_dataset")
        save_model(test_labels, "feed_forward_test_set_prediction")
        model.save(
            "{}/models/saved_models/feed_forward_model.h5".format(ROOT_DIR)
        )
        FEED_MAE = mae
        print("FEED", str(mae))
    return model
Esempio n. 3
0
def linear_create_model(df):
    """
    Creates model and saves the model to a sav file.
    :param df: dataframe with canteen and date
    :return: model, x, y and test data set
    """
    dataframe = pd.DataFrame(df.index)
    dataframe["Canteen"] = df["Canteen"].values
    dataframe["date"] = pd.to_datetime(dataframe["date"])
    dataframe["date"] = dataframe["date"].map(dt.datetime.toordinal)

    train, test = train_test_split(dataframe, test_size=DATA_SET_TEST_SIZE)

    y = np.asarray(train["Canteen"])
    x = np.asarray(train["date"]).reshape((-1, 1))
    model = LinearRegression(normalize=True).fit(
        x, y)  # create linear regression object #train model on train data
    save_model(model, "linear_regression")
    return model, x, y, test
Esempio n. 4
0
def create_prophet_model(train):
    # Create model based on training dataframe
    train["floor"] = 0
    train["cap"] = 2100

    m = Prophet(
        growth="logistic",
        interval_width=0.95,
        seasonality_mode="multiplicative",
        changepoint_prior_scale=20,
        seasonality_prior_scale=20,
        yearly_seasonality=12,
        daily_seasonality=False,
    )
    m.add_seasonality(name="monthly", period=30.5, fourier_order=4)
    m.add_country_holidays(country_name="Norway")
    m.fit(train)
    # m.train_holiday_names
    save_model(m, "prophet")
    return m
Esempio n. 5
0
def catboost_create_model(dt_df):
    raw_data = dt_df.copy()
    df = preprocess_to_catboost(raw_data)
    train_dataset, test_dataset, train_labels, test_labels = preprocess(df)

    train_dataset_combined = Pool(train_dataset, train_labels)
    eval_dataset = Pool(test_dataset, test_labels)

    model = CatBoostRegressor(iterations=2000,
                              learning_rate=0.05,
                              depth=5,
                              eval_metric="MAE")
    model.fit(train_dataset_combined, eval_set=eval_dataset, verbose=0)
    test_labels["prediction"] = model.predict(test_dataset).flatten()

    mae = mean_absolute_error(
        np.asarray(test_labels["Canteen"]),
        np.asarray(test_labels["prediction"]),
    )
    global CAT_MAE
    if mae < CAT_MAE:
        save_model(model, "catboost")
        save_model(model.get_evals_result(), "catboost_evaluation_result")
        save_model(test_labels, "catboost_test_set_prediction")
        CAT_MAE = mae
        print("CAT", str(mae))

    return model
Esempio n. 6
0
def predict_lstm_with_testset(ml_df, test_period, local_testing=True):
    """
    LSTM prediction with existing data in ml_df.csv for training the model
    :param ml_df: a dataframe that is non categorical
    :param test_period: int for setting the test size
    :param local_testing: bool value, will print training data if set to True.
    :return: model history and the predicted values
    """
    df = ml_df.copy()
    model, scaler, test_dataset, test_labels, history = train_lstm(
        df, test_period, local_testing)

    # From tutorial https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/
    yhat = model.predict(test_dataset)

    test_dataset = test_dataset.reshape(
        (test_dataset.shape[0], test_dataset.shape[2]))
    inv_yhat = concatenate((yhat, test_dataset), axis=1)

    inv_yhat = scaler.inverse_transform(inv_yhat)
    inv_yhat = inv_yhat[:, 0]
    test_y = test_labels.reshape((len(test_labels), 1))

    inv_y = concatenate((test_y, test_dataset), axis=1)
    inv_y = scaler.inverse_transform(inv_y)
    inv_y = inv_y[:, 0]

    pred_df = pd.DataFrame({
        "prediction": inv_yhat.flatten(),
        "Canteen": inv_y.flatten()
    })
    mae = mean_absolute_error(np.asarray(pred_df["Canteen"]),
                              np.asarray(pred_df["prediction"]))
    global LSTM_MAE
    if mae < LSTM_MAE:
        model.save("{}/models/saved_models/lstm_model.h5".format(ROOT_DIR))
        save_model(history.history, "lstm_history")
        save_model(history.epoch, "lstm_epoch")
        save_model(pred_df, "lstm_test_set_prediction")
        LSTM_MAE = mae
        print("LSTM", str(mae))
    return history, inv_yhat