如何对所有10折应用交叉验证(培训和测试)?

时间:2019-12-29 16:18:18

标签: python conv-neural-network cross-validation k-fold

我有此代码,我想对所有折叠(测试和训练)应用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)

此代码没有任何错误,只是对它应用了10倍交叉验证(用于测试的1倍和用于培训的9倍)。

0 个答案:

没有答案