Exemplo n.º 1
0
def create_lstm_model(look_back: int = 1, eval_type: str = 'mape'):
    """
    create a lstm model without feature
    :param look_back:
    :param eval_type: model evaluation method
    :return:
    """

    # get data from file library
    origin_data = load_data()

    # Z-score standardization of origin data
    scale_data, scale_model = min_max_scale_data(data=origin_data)

    # Divide the data set into a training set and a test set
    # (not considering the validation set)
    train, test = split_train_test_data(feature_x=scale_data, test_size=0.8)

    # convert an array of values into a data set matrix
    x_train, y_train = deal_data(data=train, look_back=look_back)
    x_test, y_test = deal_data(data=test, look_back=look_back)

    # lstm X: [samples, time steps, features],so transform data
    train_x = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
    test_x = np.reshape(x_test, (x_test.shape[0], 1, x_test.shape[1]))

    # create lstm network model
    lstm_model = Sequential()
    lstm_model.add(LSTM(4, input_shape=(1, look_back)))
    lstm_model.add(Dense(1))
    lstm_model.compile(loss='mean_squared_error', optimizer='adam')

    # model train
    lstm_model.fit(train_x, y_train, epochs=1000, batch_size=64, verbose=2)

    # model predict
    predict_y = lstm_model.predict(test_x)

    # model evaluation
    true_y = scale_model.inverse_transform([y_test])[0]
    predict_y = scale_model.inverse_transform(predict_y)
    predict_y = predict_y.reshape(predict_y.shape[0])

    eval_value = evaluation_criteria_model(true_y=true_y,
                                           predict_y=predict_y,
                                           eval_type=eval_type)
    print(eval_value)

    return eval_value
Exemplo n.º 2
0
def create_model(model_type: str='svm', reduce_type: str='pca',
                 eval_type: str='mape') -> float:
    """
    create a ml model
    :param model_type: select model
    :param reduce_type: data dimension reduction method
    :param eval_type: model evaluation method
    :return:
    """

    # get data from file library
    x, y = load_data()

    # Divide the data set into a training set and a test set
    # (not considering the validation set)
    x_train, x_test, y_train, y_test = split_train_test_data(
        feature_x=x, label_y=y)

    # Z-score standardization of origin data
    x_train_scale, scale_x_model, y_train_scale, scale_y_model = standard_scale(
        x=x_train, y=y_train)
    x_test_scale = scale_x_model.transform(x_test)
    y_test_scale = scale_y_model.transform(y_test.reshape(-1, 1))

    # Dimension reduction of standardized data sets: PCA or TruncatedSVD
    reduced_train_x, reduced_model = data_reducer(
        x=x_train_scale, reduce_type=reduce_type)
    reduced_test_x = reduced_model.transform(x_test_scale)

    # create model
    ele_model = select_model(features_data=reduced_train_x,
                             label_data=y_train_scale.ravel(),
                             model_type=model_type)

    # model train
    ele_model.fit(reduced_train_x, y_train_scale.ravel())
    rbf_svr_predict = ele_model.predict(reduced_test_x)

    # model evaluation
    y_true = scale_y_model.inverse_transform(y_test_scale)
    y_true = y_true.reshape(y_true.shape[0])
    y_predict = scale_y_model.inverse_transform(rbf_svr_predict)

    eval_value = evaluation_criteria_model(true_y=y_true, predict_y=y_predict,
                                           eval_type=eval_type)

    return eval_value
Exemplo n.º 3
0
        nn_predictions = np.array([])
        for i in range(len(test_x)):
            batch_x = np.reshape(test_x[i, :], (1, ncv.n_input))
            nn_predictions = np.append(
                nn_predictions,
                sess.run(predict_value, feed_dict={x: batch_x})[0])

        print("Optimization Finished!")

    nn_predictions.flatten()

    return [test_y, nn_predictions]


if __name__ == "__main__":

    ncv1 = NetworkConstantVariable()

    inp = lagmat(load_data(), ncv1.n_input, trim='both')
    inp = inp[1:]
    print(inp)
    outp = inp[1:, 0]
    print(outp.shape)
    inp = inp[:-1]
    print(inp.shape)

    real_value, predicted_value = run_mlp(ncv1, inp, outp)
    print(real_value, predicted_value)
    mape = evaluation_criteria_model(real_value, predicted_value)
    print("mape = ", mape)
Exemplo n.º 4
0
def create_feature_lstm_model_lag_n(neurons_num: int,
                                    epochs: int,
                                    batch_size: int,
                                    n_step: int = 1):

    # step1: prepare data
    # get data from file library
    origin_data = load_data_feature_lstm()

    # Z-score standardization of origin data
    scale_data, scale_model = min_max_scale_data(data=origin_data)

    # Constructed into a supervised learning problem
    reframe_data = series_to_supervised(data=scale_data, n_in=n_step, n_out=1)

    # Divide data into training and test sets
    values = reframe_data.values
    n_train_month = 59
    train = values[:n_train_month, :]
    test = values[n_train_month:, :]

    # split data to input and output
    n_features = int(reframe_data.values.shape[1] / (n_step + 1))
    n_obs = n_features * n_step
    train_x, train_y = train[:, :n_obs], train[:, -n_features]
    test_x, test_y = test[:, :n_obs], test[:, -n_features]

    # LSTM X: [samples, time steps, features],so transform data
    train_x = np.reshape(train_x, (train_x.shape[0], n_step, n_features))
    test_x = np.reshape(test_x, (test_x.shape[0], n_step, n_features))

    # Design network structure
    # 20, (64, 128)
    model = Sequential()
    model.add(
        LSTM(neurons_num, input_shape=(train_x.shape[1], train_x.shape[2])))
    model.add(Dense(1))
    model.compile(loss='mae', optimizer='adam')
    # Fitting network
    history = model.fit(train_x,
                        train_y,
                        epochs=epochs,
                        batch_size=batch_size,
                        verbose=2,
                        validation_data=(test_x, test_y),
                        shuffle=False)

    # Plot history loss
    plt.plot(history.history['loss'], label='train')
    plt.plot(history.history['val_loss'], label='test')
    plt.legend()
    plt.show()

    # Give prediction
    y_predict = model.predict(test_x)
    test_x = test_x.reshape((test_x.shape[0], n_step * n_features))

    # Reverse scaling predict value
    inv_y_predict = np.concatenate((y_predict, test_x[:, -(n_features - 1):]),
                                   axis=1)
    inv_y_predict = scale_model.inverse_transform(inv_y_predict)
    inv_y_predict = inv_y_predict[:, 0]
    print("inv_y_predict = ", inv_y_predict)

    # Reverse scaling actual value
    test_y_true = test_y.reshape((len(test_y), 1))
    inv_y_true = np.concatenate((test_y_true, test_x[:, -(n_features - 1):]),
                                axis=1)
    inv_y_true = scale_model.inverse_transform(inv_y_true)
    inv_y_true = inv_y_true[:, 0]
    print("inv_y_true = ", inv_y_true)

    # Calculate rmse and mape
    rmse = sqrt(mean_squared_error(inv_y_true, inv_y_predict))
    print('Test RMSE: %.3f' % rmse)
    mape = evaluation_criteria_model(inv_y_true, inv_y_predict)
    print('Test MAPE: %.3f' % mape)
Exemplo n.º 5
0
def train_parameters_model(param_str,
                           train_x,
                           train_y,
                           test_x,
                           test_y,
                           n_step,
                           n_features,
                           scale_model,
                           is_plot: bool = False):
    """

    :param param_str: params consists str "_"
    :param train_x:
    :param train_y:
    :param test_x:
    :param test_y:
    :param n_step: time step
    :param n_features: features number
    :param scale_model:
    :param is_plot:
    :return:
    """

    print("start train params = {}".format(param_str))
    time.sleep(1)
    param_lst = param_str.split("_")

    # Design network structure
    # 20, (64, 128)
    model = Sequential()
    model.add(
        LSTM(int(param_lst[0]),
             input_shape=(train_x.shape[1], train_x.shape[2])))
    model.add(Dense(1))
    model.compile(loss='mae', optimizer='adam')
    # Fitting network
    history = model.fit(train_x,
                        train_y,
                        epochs=int(param_lst[1]),
                        batch_size=int(param_lst[2]),
                        verbose=2,
                        validation_data=(test_x, test_y),
                        shuffle=False)

    if is_plot:
        # Plot history loss
        plt.plot(history.history['loss'], label='train')
        plt.plot(history.history['val_loss'], label='test')
        plt.legend()
        plt.show()

    # Give prediction
    y_predict = model.predict(test_x)
    test_x = test_x.reshape((test_x.shape[0], n_step * n_features))

    # Reverse scaling predict value
    inv_y_predict = np.concatenate((y_predict, test_x[:, -(n_features - 1):]),
                                   axis=1)
    inv_y_predict = scale_model.inverse_transform(inv_y_predict)
    inv_y_predict = inv_y_predict[:, 0]

    # Reverse scaling actual value
    test_y_true = test_y.reshape((len(test_y), 1))
    inv_y_true = np.concatenate((test_y_true, test_x[:, -(n_features - 1):]),
                                axis=1)
    inv_y_true = scale_model.inverse_transform(inv_y_true)
    inv_y_true = inv_y_true[:, 0]

    return evaluation_criteria_model(inv_y_true, inv_y_predict)
Exemplo n.º 6
0
def create_feature_lstm_model():
    """train a lstm model with features"""

    # get data from file library
    origin_data = load_data_feature_lstm()

    # Z-score standardization of origin data
    scale_data, scale_model = min_max_scale_data(data=origin_data)

    # Constructed into a supervised learning problem
    reframe_data = series_to_supervised(data=scale_data, n_in=1, n_out=1)

    # Discard columns we don't want to predict
    delete_cols = [i for i in range(19, 36)]
    reframe_data.drop(reframe_data.columns[delete_cols], axis=1, inplace=True)

    # Divide data into training and test sets
    values = reframe_data.values
    n_train_month = 59
    train = values[:n_train_month, :]
    test = values[n_train_month:, :]
    train_x, train_y = train[:, :-1], train[:, -1]
    test_x, test_y = test[:, :-1], test[:, -1]

    # LSTM X: [samples, time steps, features],so transform data
    train_x = np.reshape(train_x, (train_x.shape[0], 1, train_x.shape[1]))
    test_x = np.reshape(test_x, (test_x.shape[0], 1, test_x.shape[1]))

    # Design network structure
    # 20, (64, 128)
    model = Sequential()
    model.add(LSTM(20, input_shape=(train_x.shape[1], train_x.shape[2])))
    model.add(Dense(1))
    model.compile(loss='mae', optimizer='adam')
    # Fitting network
    history = model.fit(train_x,
                        train_y,
                        epochs=850,
                        batch_size=128,
                        validation_data=(test_x, test_y),
                        verbose=2,
                        shuffle=False)
    # Plot history loss
    plt.plot(history.history['loss'], label='train')
    plt.plot(history.history['val_loss'], label='test')
    plt.legend()
    plt.show()

    # Give prediction
    y_predict = model.predict(test_x)
    test_x = test_x.reshape((test_x.shape[0], test_x.shape[2]))

    # Reverse scaling predict value
    inv_y_predict = np.concatenate((y_predict, test_x[:, 1:]), axis=1)
    inv_y_predict = scale_model.inverse_transform(inv_y_predict)
    inv_y_predict = inv_y_predict[:, 0]
    print("inv_y_predict = ", inv_y_predict)

    # Reverse scaling actual value
    test_y_true = test_y.reshape((len(test_y), 1))
    inv_y_true = np.concatenate((test_y_true, test_x[:, 1:]), axis=1)
    inv_y_true = scale_model.inverse_transform(inv_y_true)
    inv_y_true = inv_y_true[:, 0]
    print("inv_y_true = ", inv_y_true)

    # Calculate rmse and mape
    rmse = sqrt(mean_squared_error(inv_y_true, inv_y_predict))
    print('Test RMSE: %.3f' % rmse)
    mape = evaluation_criteria_model(inv_y_true, inv_y_predict)
    print('Test MAPE: %.3f' % mape)