In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
In [ ]:
from sklearn.datasets import load_digits
from sklearn.cross_validation import train_test_split
from sklearn.svm import LinearSVC
digits = load_digits()
X, y = digits.data, digits.target
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
classifier = LinearSVC(random_state=0).fit(X_train, y_train)
y_test_pred = classifier.predict(X_test)
print("Accuracy: %f" % classifier.score(X_test, y_test))
In [ ]:
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_test_pred)
In [ ]:
plt.imshow(confusion_matrix(y_test, y_test_pred), cmap="coolwarm", interpolation="None")
plt.colorbar()
plt.xlabel("Predicted label")
plt.xticks(range(10))
plt.yticks(range(10))
plt.ylabel("True label")
In [ ]:
y_even = y % 2
X_train, X_test, y_train, y_test = train_test_split(X, y_even, random_state=42)
classifier = LinearSVC().fit(X_train, y_train)
y_test_pred = classifier.predict(X_test)
In [ ]:
confusion_matrix(y_test, y_test_pred)
Binary confusion matrix:
True Positive (TP) | False Negative (FN) |
False Positive (FP) | True Negative (TN) |
In [ ]:
from sklearn.metrics import classification_report
print(classification_report(y_test, y_test_pred))
In [ ]: