Python 如何循环通过各种列车和测试分路

Python 如何循环通过各种列车和测试分路,python,pandas,keras,Python,Pandas,Keras,我使用TimeSeriesSplit()创建了各种训练和测试分割。我的数据框架有377个观测值,其中有6个输入变量和1个目标变量 我将数据帧拆分为train并使用以下代码进行测试: #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 (&quo

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

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

#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)
如何使用上述代码循环所有不同的拆分并将结果存储在列表或数据帧中

我还想绘制预测结果,如下所示

这是我根据@Ashraful答案得到的图表


使用此命令替换上一个代码块

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文件中找到我的实现。

这非常有效,谢谢。但是我如何绘制所有值的预测图,这样我就能看到模型是如何预测的?我已经编辑了这个问题,以显示一个示例图形更新的问题,以显示添加了更新后得到的绘图。我在每次折叠后都会得到这张图,但我想在最后绘制一张图,如问题中所示。有没有办法将每次折叠的列车数据保存到一个数据帧中?例如,fold 1,我获取预测的列车并保存,fold 2我附加到列车预测数据帧,fold 3我附加,依此类推