2010-05-26 20:40:29 +08:00
|
|
|
"""
|
|
|
|
|
================
|
|
|
|
|
Confusion matrix
|
|
|
|
|
================
|
|
|
|
|
|
|
|
|
|
Example of confusion matrix usage to evaluate the quality
|
2013-04-09 18:20:18 +08:00
|
|
|
of the output of a classifier on the iris data set. The
|
|
|
|
|
diagonal elements represent the number of points for which
|
|
|
|
|
the predicted label is equal to the true label, while
|
|
|
|
|
off-diagonal elements are those that are mislabeled by the
|
|
|
|
|
classifier. The higher the diagonal values of the confusion
|
|
|
|
|
matrix the better, indicating many correct predictions.
|
2014-07-20 19:27:04 +08:00
|
|
|
|
|
|
|
|
The figures show the confusion matrix with and without
|
|
|
|
|
normalization by class support size (number of elements
|
|
|
|
|
in each class). This kind of normalization can be
|
|
|
|
|
interesting in case of class imbalance to have a more
|
|
|
|
|
visual interpretation of which class is being misclassified.
|
|
|
|
|
|
|
|
|
|
Here the results are not as good as they could be as our
|
|
|
|
|
choice for the regularization parameter C was not the best.
|
|
|
|
|
In real life applications this parameter is usually chosen
|
|
|
|
|
using :ref:`grid_search`.
|
|
|
|
|
|
2010-05-26 20:40:29 +08:00
|
|
|
"""
|
2013-04-09 18:20:18 +08:00
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2010-05-26 20:40:29 +08:00
|
|
|
|
2016-07-08 07:25:14 +08:00
|
|
|
import itertools
|
2014-07-20 19:27:04 +08:00
|
|
|
import numpy as np
|
2014-09-01 16:17:33 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2014-07-20 19:27:04 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn import svm, datasets
|
2015-09-11 02:26:39 +08:00
|
|
|
from sklearn.model_selection import train_test_split
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.metrics import confusion_matrix
|
2010-05-26 20:40:29 +08:00
|
|
|
|
|
|
|
|
# import some data to play with
|
|
|
|
|
iris = datasets.load_iris()
|
|
|
|
|
X = iris.data
|
|
|
|
|
y = iris.target
|
2016-07-08 07:25:14 +08:00
|
|
|
class_names = iris.target_names
|
2013-04-09 18:20:18 +08:00
|
|
|
|
|
|
|
|
# Split the data into a training set and a test set
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
|
2010-05-26 20:40:29 +08:00
|
|
|
|
2014-07-20 19:27:04 +08:00
|
|
|
# Run classifier, using a model that is too regularized (C too low) to see
|
|
|
|
|
# the impact on the results
|
|
|
|
|
classifier = svm.SVC(kernel='linear', C=0.01)
|
2013-04-09 18:20:18 +08:00
|
|
|
y_pred = classifier.fit(X_train, y_train).predict(X_test)
|
2010-05-26 20:40:29 +08:00
|
|
|
|
2014-09-01 16:17:33 +08:00
|
|
|
|
2016-07-08 07:25:14 +08:00
|
|
|
def plot_confusion_matrix(cm, classes,
|
|
|
|
|
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`.
|
|
|
|
|
"""
|
|
|
|
|
if normalize:
|
|
|
|
|
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
|
|
|
|
|
print("Normalized confusion matrix")
|
|
|
|
|
else:
|
|
|
|
|
print('Confusion matrix, without normalization')
|
|
|
|
|
|
|
|
|
|
print(cm)
|
|
|
|
|
|
2016-12-04 04:57:15 +08:00
|
|
|
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'
|
2016-07-08 07:25:14 +08:00
|
|
|
thresh = cm.max() / 2.
|
|
|
|
|
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
|
2016-12-04 04:57:15 +08:00
|
|
|
plt.text(j, i, format(cm[i, j], fmt),
|
2016-07-08 07:25:14 +08:00
|
|
|
horizontalalignment="center",
|
|
|
|
|
color="white" if cm[i, j] > thresh else "black")
|
|
|
|
|
|
2014-09-01 16:17:33 +08:00
|
|
|
plt.ylabel('True label')
|
|
|
|
|
plt.xlabel('Predicted label')
|
2018-05-04 22:47:23 +08:00
|
|
|
plt.tight_layout()
|
|
|
|
|
|
2014-09-01 16:17:33 +08:00
|
|
|
|
2010-05-26 20:40:29 +08:00
|
|
|
# Compute confusion matrix
|
2016-07-08 07:25:14 +08:00
|
|
|
cnf_matrix = confusion_matrix(y_test, y_pred)
|
2014-09-01 16:17:33 +08:00
|
|
|
np.set_printoptions(precision=2)
|
2016-07-08 07:25:14 +08:00
|
|
|
|
|
|
|
|
# Plot non-normalized confusion matrix
|
2014-09-01 16:17:33 +08:00
|
|
|
plt.figure()
|
2016-07-08 07:25:14 +08:00
|
|
|
plot_confusion_matrix(cnf_matrix, classes=class_names,
|
|
|
|
|
title='Confusion matrix, without normalization')
|
2014-07-20 19:27:04 +08:00
|
|
|
|
2016-07-08 07:25:14 +08:00
|
|
|
# Plot normalized confusion matrix
|
2014-07-20 19:27:04 +08:00
|
|
|
plt.figure()
|
2016-07-08 07:25:14 +08:00
|
|
|
plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
|
|
|
|
|
title='Normalized confusion matrix')
|
2014-09-01 16:17:33 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|