如何遍历各种训练和测试拆分

时间:2020-09-02 17:42:58

标签: python pandas keras

我有各种使用TimeSeriesSplit()创建的训练和测试拆分。我的数据框有377个观测值,其中包含6个输入变量和1个目标变量。

我使用以下代码将数据帧拆分为训练并进行测试:

#train set 
i=0
for X_train, X_test in tscv.split(data):
    i=i+1
    print ("No of observations under train%s=%s"%(i,len(X_train)))
    print ("No of observations under test%s=%s" % (i, len(X_test)))

X_train1, X_test1 = data[:67, :-1],  data[67:129,:-1]
X_train2, X_test2 = data[:129,:-1], data[129:191,:-1]
X_train3, X_test3 = data[:191,:-1], data[191:253,:-1]
X_train4, X_test4 = data[:253,:-1], data[253:315,:-1]
X_train5, X_test5 = data[:315,:-1], data[315:377,:-1]

#test set
i=0
for y_train, y_test in tscv.split(data):
    i=i+1
    print ("No of observations under train%s=%s"%(i,len(y_train)))
    print ("No of observations under test%s=%s" % (i, len(y_test)))

y_train1, y_test1 = data[:67, -1], data[67:129 ,-1]
y_train2, y_test2 = data[:129,-1], data[129:191,-1]
y_train3, y_test3 = data[:191,-1], data[191:253,-1]
y_train4, y_test4 = data[:253,-1], data[253:315,-1]
y_train5, y_test5 = data[:315,-1], data[315:377,-1]

所以我总共有5个拆分。我想训练我的lstm模型遍历这些拆分,但是我不确定如何才能做到最好。这是我的lstm的代码:

# split into input and outputs
train_X, train_y = X_train, y_train
test_X, test_y = X_test, y_test

#reshape input to be 3D [samples, timesteps, features]
train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1]))
test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1]))

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,LSTM, Flatten
import matplotlib.pyplot as pyplot
# design network
model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
history = model.fit(train_X, train_y, epochs=700
                    , batch_size=72, validation_data=(test_X, test_y), verbose=2, shuffle=False)

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

#predictions
y_lstm = model.predict(test_X)

#metrics for test set
mse_lstm = mean_squared_error(y_test, y_lstm)
rmse_lstm = np.sqrt(mse_lstm)
r2_lstm = r2_score(y_test, y_lstm)
mae_lstm = mean_absolute_error(y_test, y_lstm)

#train metics
train     = model.predict(X_t_reshaped)
msetrain  = mean_squared_error(y_train, train)
rmsetrain = np.sqrt(msetrain)
r2train   = r2_score(y_train, train)

如何使用上面的代码遍历所有不同的拆分并将结果存储在列表或数据框中?

我还希望绘制出如下所示的预测结果

enter image description here

这是基于@Ashraful答案的能力

enter image description here

1 个答案:

答案 0 :(得分:1)

使用此替换您的最后一个代码块,

from sklearn.metrics import  mean_squared_error
from sklearn.metrics import *
import numpy as np
import csv  

Round = 3      # define the number of digits after decimal point you want 

fields = ['Fold_No', 'mse_lstm', 'rmse_lstm', 'r2_lstm','mae_lstm']  
csvfile = open('Summary.csv', 'w') 
csvwriter = csv.writer(csvfile)  
csvwriter.writerow(fields) 


for fold in range(1,6):
    print(f'Running fold {fold}')
    # split into input and outputs
    train_X, train_y = eval(f'X_train{fold}'),eval(f'y_train{fold}')
    test_X, test_y = eval(f'X_test{fold}'),eval(f'y_test{fold}')
    print(train_X.shape)



    #reshape input to be 3D [samples, timesteps, features]
    train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1]))
    test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1]))

    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense,LSTM, Flatten
    import matplotlib.pyplot as pyplot
    # design network
    model = Sequential()
    model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
    model.add(Dense(1))
    model.compile(loss='mae', optimizer='adam')
    history = model.fit(train_X, train_y, epochs=2
                        , batch_size=72, validation_data=(test_X, test_y), verbose=2, shuffle=False)

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

    #predictions
    train_output =  model.predict(train_X)
    y_lstm = model.predict(test_X)

    pyplot.plot(train_output, label='Training output')
    pyplot.plot(train_y, label='Obesrved Training Target')
    # pyplot.plot(train_y, label='Training value')
    pyplot.plot(test_y, label='Obesrved Predic. Target')
    pyplot.plot(y_lstm, label='Predicted Output')
    pyplot.legend(loc='upper right')
    # pyplot.legend()
    pyplot.show()
    
    #metrics for test set
    mse_lstm = mean_squared_error(y_test1, y_lstm)
    rmse_lstm = np.sqrt(mse_lstm)
    r2_lstm = r2_score(y_test1, y_lstm)
    mae_lstm = mean_absolute_error(y_test1, y_lstm)

    csvwriter.writerow([f'Fold_{fold}',round(mse_lstm,Round), round(rmse_lstm,Round), round(r2_lstm,Round),round(mae_lstm,Round)]) 


csvfile.close()

#read stored CSV file
summary= pd.read_csv('Summary.csv')

print(summary)

此外,我在colab文件中的工具也可以找到here