Commit 34062b83 by Pamela Osuna

final kfold added

parent 5945ed43
......@@ -124,4 +124,62 @@ def run_nn(input_, output_, n_experiences, params):
print("Average accuracy: ", total_acc)
print("Average area under the curve: ", total_auc)
return total_acc, total_auc, X_train_kfold, X_test_kfold, validation_Y_one_hot
def run_kfold(X_train, X_test, y_train, y_test, params):
c, b, e = params
for i in range(N_SPLITS):
# change the labels from categorical to one-hot encoding
y_train[i] = to_categorical(y_train[i], num_classes = 4)
y_test[i] = to_categorical(y_test[i], num_classes = 4)
#convert input to np.array
X_train[i] = np.array(X_train[i])
X_test[i] = np.array(X_test[i])
#convert each element of the train and test set into a matrix of size 30x1(?)
X_train[i] = X_train[i].reshape(-1, 30, 1)
X_test[i] = X_test[i].reshape(-1, 30, 1)
#convert the data from an int8 format to a float32 type
X_train[i] = X_train[i].astype('float32')
X_test[i] = X_test[i].astype('float32')
# defining keras model
model = m.model_architecture(c)
#compile the keras model
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
total_acc = 0
total_auc = 0
bs, ep = m.choose_batch_epochs(b,e)
for i in range(N_SPLITS):
#train the model
model.fit(X_train[i], y_train[i], batch_size = bs, epochs = ep, verbose = 1, validation_data = (X_test[i], y_test[i]))
#calculate accuracy
_,accuracy = model.evaluate(X_test[i], y_test[i], verbose = 0)
total_acc += accuracy
print("t_set = " + str(i))
print('Test accuracy:', accuracy)
# calculate area under the curve
y_pred = model.predict(X_validation, batch_size = bs)
fpr, tpr, auc = ra.roc_auc(N_CLASSES, validation_Y_one_hot, y_pred)
total_auc += auc
print("Area under the curve:", auc)
total_acc = total_acc/(N_SPLITS)
total_auc = total_acc/(N_SPLITS)
print("Average accuracy: ", total_acc)
print("Average area under the curve: ", total_auc)
return total_acc, total_auc
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment