<jupyter_start><jupyter_text>Chapter 2: Classification  MNIST<jupyter_code>import numpy as np
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version=1)
mnist.keys()
X, y = mnist["data"], mnist["target"]
X.shape, y.shape # 784 => 28x28
import matplotlib.pyplot as plt
some_digit_image = X[0].reshape(28, 28)
plt.imshow(some_digit_image, cmap="binary")
plt.show()
some_digit_image_2 = X[1].reshape(28, 28)
plt.imshow(some_digit_image_2, cmap="binary")
plt.show()
y[0]
y = y.astype(np.uint8)
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]<jupyter_output><empty_output><jupyter_text>Training Binary classifier<jupyter_code># Number 5 detector
y_train_5 = (y_train == 5)
y_test_5 = (y_test == 5)
from sklearn.linear_model import SGDClassifier

sgd_clf = SGDClassifier(random_state=42)
sgd_clf.fit(X_train, y_train_5)
sgd_clf.score(X_train, y_train_5)
sgd_clf.predict([X[0]])
sgd_clf.predict([X[1]])<jupyter_output><empty_output><jupyter_text>Measuring accuracy<jupyter_code>from sklearn.model_selection import cross_val_score

cross_val_score(sgd_clf, X_train, y_train_5, cv=3, scoring="accuracy")
np.zeros(2)
np.zeros((len([1,2]), 1), dtype=bool)
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix

y_train_5_pred = cross_val_predict(sgd_clf, X_train, y_train_5, cv=3)

confusion_matrix(y_train_5, y_train_5_pred)

# Row: Actual Class [0, 1]
# Column: Predicted Class [0,
#                          1]
confusion_matrix(y_train_5, y_train_5)<jupyter_output><empty_output><jupyter_text>Precision and Recall**Precision:** accuracy of positive predictions**precision = TP / (TP + FP)****Recall:** sensitivity or true positive rate. Ration of positive instances that are correctly detected by the classifier**recall = TP / (TP + FN)****F1:** harmonic mean of precision and recall. Harmonic mean gives more weight to low values, as the result, classifier will only get high f1 score if both recall and precision are high<jupyter_code>from sklearn.metrics import precision_score, recall_score

precision_score(y_train_5, y_train_5_pred), recall_score(y_train_5, y_train_5_pred)

# Our classifier is correct only 83.7% of the time.
# Moreover, it only detects 65.1% of 5s
from  sklearn.metrics import f1_score

f1_score(y_train_5, y_train_5_pred)
y_scores = cross_val_predict(sgd_clf, X_train, y_train_5, cv=3, method="decision_function")
y_scores
len(y_train_5), len(y_scores)
my_arr = [1, 2, 3, 4, 5]
my_arr[:-1]
from sklearn.metrics import precision_recall_curve

precisions, recalls, thresholds = precision_recall_curve(y_train_5, y_scores)

plt.plot(thresholds, precisions[:-1], "b--", label="Precision")
plt.plot(thresholds, recalls[:-1], "g-", label="Recall")
plt.show()
precisions.shape, recalls.shape, thresholds.shape
np.argmax(precisions >= 0.90)
np.array([1,2,3,4,5]) >= 2
np.argmax(np.array([1,2,3,4,5]) >= 2)
threshold_90_precision = thresholds[np.argmax(precisions >= 0.90)]
# instead of calling predict method, manually use our requirement threshold
y_train_5_pred_90 = (y_scores >= threshold_90_precision)

precision_score(y_train_5, y_train_5_pred_90), recall_score(y_train_5, y_train_5_pred_90)<jupyter_output><empty_output><jupyter_text>ROC Curve<jupyter_code>from sklearn.metrics import roc_curve

fpr, tpr, thresholds = roc_curve(y_train_5, y_scores)

plt.plot(fpr, tpr, linewidth=2, label='SGD')
plt.plot([0,1], [0,1], "k--") # dashed diagonal
plt.show()
from sklearn.metrics import roc_auc_score

roc_auc_score(y_train_5, y_scores)
from sklearn.ensemble import RandomForestClassifier

forest_clf = RandomForestClassifier(random_state=42)
y_probas_forest = cross_val_predict(forest_clf, X_train, y_train_5, cv=3, method="predict_proba")
y_probas_forest.shape
y_scores_forest = y_probas_forest[:, 1] # proba of positive class
fpr_forest, tpr_forest, thresholds_forest = roc_curve(y_train_5, y_scores_forest)

plt.plot(fpr_forest, tpr_forest, linewidth=2, label="Random Forest")
plt.plot(fpr, tpr, "b:", label="SGD")
plt.plot([0,1], [0,1], "k--") # dashed diagonal
plt.legend(loc="lower right")
plt.show()
roc_auc_score(y_train_5, y_scores_forest)<jupyter_output><empty_output><jupyter_text>Multiclass Classification  SVM<jupyter_code>from sklearn.svm import SVC

svm_clf = SVC()
svm_clf.fit(X_train, y_train) # default uses OvO strategy for SVM
svm_clf.predict([X[0]]), svm_clf.predict([X[1]])
# OvO strategy, trained 45 binary classifiers, got their decision scores for the image
# selected the class that won the most duels

x_5_digit_scores = svm_clf.decision_function([X[0]])
x_5_digit_scores
np.argmax(x_5_digit_scores) # index of class
svm_clf.classes_
svm_clf.classes_[5] # the class
# using OvR strategy

from sklearn.multiclass import OneVsRestClassifier

ovr_clf = OneVsRestClassifier(SVC())
ovr_clf.fit(X_train, y_train)
ovr_clf.predict([X[0]])
len(ovr_clf.estimators_)<jupyter_output><empty_output><jupyter_text>SGD<jupyter_code>sgd_clf.fit(X_train, y_train) # OvR strategy under the hood
sgd_clf.predict([X[0]])
sgd_clf.decision_function([X[0]])
cross_val_score(sgd_clf, X_train, y_train, cv=3, scoring="accuracy")
# increase accuracy by Standardization (zero mean, unit variance)

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train.astype(np.float64))
cross_val_score(sgd_clf, X_train_scaled, y_train, cv=3, scoring="accuracy")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12197802197802197, 'fraction_numerical': 0.03131868131868132, 'mean_word_length': 7.751602564102564, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '130154', 'license': 'MIT', 'n_tokens_mistral': 2260, 'n_tokens_neox': 2106, 'n_words': 430, 'path': 'ch03-homl-classification.ipynb', 'repo_name': 'DeekshithShetty/my-hands-on-ml', 'size': '5460'}