我有这段代码,我想对所有折叠(测试和训练)应用 10 折交叉验证,下面的代码仅适用于一折。如何为所有折叠编写一个循环(9 折用于训练,1 折用于测试,循环用于所有折叠)?
dataset=pd.read_csv("C:/Users/User/Desktop/minority class equal.csv",encoding='cp1252')
dataset.shape
#output(53480, 37)
array = dataset.values
X = np.expand_dims(array[:,0:36], axis=-1)
Y = np.expand_dims(array[:,36], axis=-1)
kf = KFold(n_splits=10)
kf.get_n_splits(X)
ACC_array = np.array([])
sensitivity_array = np.array([])
specificity_array = np.array([])
for trainindex, testindex in kf.split(X):
Xtrain, Xtest = X[trainindex], X[testindex]
Ytrain, Ytest = Y[trainindex], Y[testindex]
Ytrain = np.expand_dims(Ytrain, axis=-1)
Ytest = np.expand_dims(Ytest, axis=-1)
print(Xtrain.shape, Ytrain.shape)
n_timesteps, n_features, n_outputs =Xtrain.shape[0], Xtrain.shape[1], Ytrain.shape[1]
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=1, activation='relu',input_shape=(n_features,1)))
model.add(Conv1D(filters=64, kernel_size=1, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(Xtrain, Ytrain, epochs=20, batch_size=100)
Xtest = Xtest.astype(np.float32)
Predictions = model.predict(Xtest,batch_size =1024)
rounded = [round(x[0]) for x in Predictions]
Y_predection = pd.DataFrame(rounded)
Y_predection = Y_predection.iloc[:, 0]
confusion = metrics.confusion_matrix(Ytest,Y_predection)
TP= confusion[1, 1]
TN= confusion[0, 0]
FP= confusion[0, 1]
FN= confusion[1, 0]
Accuracy=(TP + TN) / float(TP + TN + FP + FN)
sensitivity = TP / float(FN + TP)
specificity= TN / float(TN + FP)
ACC_array = np.append(ACC_array,Accuracy)
sensitivity_array = np.append(sensitivity_array,sensitivity)
specificity_array = np.append(specificity_array,specificity)
ACC_mean=np.mean(ACC_array, axis=0 )
print ('mean Accuracy',ACC_mean*100)
sensitivity_mean=np.mean(sensitivity_array, axis=0 )
print ('mean sensitivity',sensitivity_mean*100)
specificity_mean=np.mean(specificity_array, axis=0 )
print ('mean specificity',specificity_mean*100)
此代码没有任何错误,但仅针对(测试为 1 倍,训练为 9 倍)应用了 10 倍交叉验证。