-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Classification Report and confusion matrix
- Loading branch information
Qinqing Liu
committed
Apr 7, 2020
1 parent
99c4a9e
commit 824c640
Showing
5 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
from sklearn.metrics import classification_report, confusion_matrix | ||
import itertools | ||
|
||
def plot_confusion_matrix(y_true, y_pred, classes = ['benign', 'cancer'], | ||
normalize=False, | ||
title='Confusion matrix', | ||
cmap=plt.cm.Blues): | ||
""" | ||
This function prints and plots the confusion matrix. | ||
Normalization can be applied by setting `normalize=True`. | ||
""" | ||
print('Confusion Matrix') | ||
cm = confusion_matrix(y_true, y_pred) | ||
print('Classification Report') | ||
target_names = ['benigh', 'cancer'] | ||
print(classification_report(y_true, y_pred, target_names=target_names)) | ||
|
||
if normalize: | ||
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] | ||
print("Normalized confusion matrix") | ||
else: | ||
print('Confusion matrix, without normalization') | ||
|
||
print(cm) | ||
|
||
plt.imshow(cm, interpolation='nearest', cmap=cmap) | ||
plt.title(title) | ||
plt.colorbar() | ||
tick_marks = np.arange(len(classes)) | ||
plt.xticks(tick_marks, classes, rotation=45) | ||
plt.yticks(tick_marks, classes) | ||
|
||
fmt = '.2f' if normalize else 'd' | ||
thresh = cm.max() / 2. | ||
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): | ||
plt.text(j, i, format(cm[i, j], fmt), | ||
horizontalalignment="center", | ||
color="white" if cm[i, j] > thresh else "black") | ||
|
||
plt.ylabel('True label') | ||
plt.xlabel('Predicted label') | ||
plt.tight_layout() | ||
|
||
|
||
def save_cm_figs(y_true, y_pred, arc, target_names = ['begign', 'cancer']): | ||
# Compute confusion matrix | ||
|
||
np.set_printoptions(precision=2) | ||
|
||
# Plot non-normalized confusion matrix | ||
plt.figure() | ||
plot_confusion_matrix(y_true, y_pred, classes=target_names, | ||
title='Confusion matrix, without normalization') | ||
plt.savefig('./result/conf_no_norm_{}.png'.format(arc), bbox_inches='tight') | ||
# Plot normalized confusion matrix | ||
plt.figure() | ||
plot_confusion_matrix(y_true, y_pred, classes=target_names, normalize=True, | ||
title='Normalized confusion matrix') | ||
plt.savefig('./result/conf_norm_{}.png'.format(arc), bbox_inches='tight') | ||
plt.show() | ||
|