예제 #1
0
def predict_point(y_train, y_test, X_train, X_test, model):
    """
    Method to fit a regression on one point, given as (t, x, y)
    :return: y vector of len (t) as a regression prediction
    """

    if np.count_nonzero(~np.isnan(y_train)) == 0:  # if point is empty
        pred = np.empty_like(y_test)
        pred[:] = np.nan
    else:
        if model == 'lasso':
            model = Lasso(alpha=0.1, max_iter=1000)

            y_clean, X_train_clean = clean_data(X=X_train, y=y_train)

            model.fit(X=X_train_clean, y=y_clean)
            mask = ~np.isnan(X_test).any(axis=1)
            X_test_clean = X_test[mask]

            pred = model.predict(X_test_clean)

            pred_out = np.empty_like(y_test)
            pred_out[mask] = pred
            pred_out[~mask] = np.nan
            pred_out[pred_out < 0] = 0

            return pred_out
        elif model == 'lstm':
            model = Sequential()
            model.add(LSTM(50, input_shape=(X_train.shape[1], X_train.shape[2]), return_sequences=False))
            model.add(Dense(1))
            model.compile(loss='mae', optimizer='adam')
예제 #2
0
    x_train = data_train[feature].as_matrix()
    y_train = data_train[v].as_matrix()

    from keras.models import Sequential
    from keras.layers.core import Dense, Activation
    import time

    start = time.clock()

    # 输入层为3个节点,隐藏层6个节点
    model = Sequential()  # 建立模型
    model.add(Dense(output_dim=6, input_dim=8))  # 添加输入层、隐藏层节点
    # model.add(Dense(input_dim=8,12))
    model.add(Activation('relu'))  # 使用relu作为激活函数,可以大幅度提高准确率
    model.add(Dense(units=1, input_dim=8))  # 添加输出层节点
    model.compile(loss='mean_squared_error', optimizer='adam')  # 编译模型
    model.fit(x_train, y_train, nb_epoch=3000, batch_size=16)  #训练模型,学习一千次
    end = time.clock()
    usetime = end - start
    print('训练该模型耗时' + str(usetime) + 's!')
    aa = v + '.model'
    model.save_weights(aa)  # 将该模型存储

    x = ((fatigue[feature] - data_mean[feature]) /
         data_std[feature]).as_matrix()
    pre = v + 'pre'
    fatigue[pre] = model.predict(x) * data_std[v] + data_mean[v]
    #保存的表名命名格式为“2_2_3_1k此表功能名称”,是此小节生成的第1张表格,功能为revenue:增值税预测结果
    fatigue.to_excel('2_2_3_1zengzhi.xlsx')

    import matplotlib.pyplot as plt