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))

"""
Explanation: More evaluation methods for classification
End of explanation
"""


from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_test_pred)

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")

"""
Explanation: Confusion matrices
End of explanation
"""


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)

confusion_matrix(y_test, y_test_pred)

"""
Explanation: Binary tasks
End of explanation
"""


from sklearn.metrics import classification_report
print(classification_report(y_test, y_test_pred))

"""
Explanation: Binary confusion matrix:
<table>
<tr><td>True Positive (TP)</td><td>False Negative (FN) </td></tr>
<tr><td>False Positive (FP) </td><td>True Negative (TN) </td></tr>
</table>

$$ \text{precision} = \frac{TP}{FP + TP} $$
$$ \text{recall} = \frac{TP}{FN + TP} $$
$$ \text{accuracy} = \frac{TP + TN}{FP + FN + TP + TN} $$
$$ f_1 = 2 \frac{\text{precision} \cdot \text{recall}}{\text{precision} + \text{recall}} $$
End of explanation
"""


