model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
We find the best hyperparameter optimizing for recall:
def print_gridsearch_scores_deep_learning(x_train_data,y_train_data):
c_param_range = [0.01,0.1,1,10,100]
clf = GridSearchCV(KerasClassifier(build_fn=network_builder, epochs=50, batch_size=128,
verbose=1, input_dim=29),
{"hidden_dimensions": ([10], [10, 10, 10], [100, 10])}, cv=5, scoring='recall')
clf.fit(x_train_data,y_train_data)
print "Best parameters set found on development set:"
print clf.bestparams
print "Grid scores on development set:"
means = clf.cv_results_['mean_test_score']
stds = clf.cv_results_['std_test_score']
for mean, std, params in zip(means, stds, clf.cv_results_['params']):
print "%0.3f (+/-%0.03f) for %r" % (mean, std * 2, params)
Finally, as shown, we print the scores from the deep learning model:
print_gridsearch_scores_deep_learning(X_train_undersample, y_train_undersample)
Epoch 1/50
550/550 [==============================] - 2s 3ms/step - loss: 0.7176 - acc: 0.2673
Epoch 2/50
550/550 [==============================] - 0s 25us/step - loss: 0.6955 - acc: 0.4582
Epoch 3/50
550/550 [==============================] - 0s 41us/step - loss: 0.6734 - acc: 0.6327
Epoch 4/50
550/550 [==============================] - 0s 36us/step - loss: 0.6497 - acc: 0.6491
Epoch 5/50
550/550 [==============================] - 0s 43us/step - loss: 0.6244 - acc: 0.6655
This produces the following output:
{'hidden_dimensions': [100, 10]} Grid scores on development set: 0.903 (+/-0.066) for {'hidden_dimensions': [10]} 0.897 (+/-0.070) for {'hidden_dimensions': [10, 10, 10]} 0.912 (+/-0.079) for {'hidden_dimensions': [100, 10]}
We use this hidden_dimensions parameter to build the final model with the whole training dataset and predict the classes in the test dataset:
k = KerasClassifier(build_fn=network_builder, epochs=50, batch_size=128,
hidden_dimensions=[100, 10], verbose=0, input_dim=29)
k.fit(X_train_undersample,y_train_undersample.values.ravel())
y_pred_undersample = k.predict(X_test_undersample.values)
# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test_undersample,y_pred_undersample)
np.set_printoptions(precision=2)
print("Recall metric in the testing dataset: ", cnf_matrix[1,1]/(cnf_matrix[1,0]+cnf_matrix[1,1]))
# Plot non-normalized confusion matrix
class_names = [0,1]
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names, title='Confusion matrix')
plt.show()
We get the following output from the preceding code:
y_pred = k.predict(X_test.values)
# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test,y_pred)
np.set_printoptions(precision=2)
print("Recall metric in the testing dataset: ", cnf_matrix[1,1]/(cnf_matrix[1,0]+cnf_matrix[1,1]))
# Plot non-normalized confusion matrix
class_names = [0,1]
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names, title='Confusion matrix')
plt.show()
From the preceding graph, we know that this is the best recall so far that we've seen on the entire dataset, thanks to deep learning.