#!/usr/bin/env python # coding: utf-8 # **Chapter 3 – Classification** # # _This notebook contains all the sample code and solutions to the exercises in chapter 3._ # # # #
# Open In Colab # # #
# # Setup # First, let's import a few common modules, ensure MatplotLib plots figures inline and prepare a function to save the figures. We also check that Python 3.5 or later is installed (although Python 2.x may work, it is deprecated so we strongly recommend you use Python 3 instead), as well as Scikit-Learn ≥0.20. # In[1]: # Python ≥3.5 is required import sys assert sys.version_info >= (3, 5) # Is this notebook running on Colab or Kaggle? IS_COLAB = "google.colab" in sys.modules IS_KAGGLE = "kaggle_secrets" in sys.modules # Scikit-Learn ≥0.20 is required import sklearn assert sklearn.__version__ >= "0.20" # Common imports import numpy as np import os # to make this notebook's output stable across runs np.random.seed(42) # To plot pretty figures get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib as mpl import matplotlib.pyplot as plt mpl.rc('axes', labelsize=14) mpl.rc('xtick', labelsize=12) mpl.rc('ytick', labelsize=12) # Where to save the figures PROJECT_ROOT_DIR = "." CHAPTER_ID = "classification" IMAGES_PATH = os.path.join(PROJECT_ROOT_DIR, "images", CHAPTER_ID) os.makedirs(IMAGES_PATH, exist_ok=True) def save_fig(fig_id, tight_layout=True, fig_extension="png", resolution=300): path = os.path.join(IMAGES_PATH, fig_id + "." + fig_extension) print("Saving figure", fig_id) if tight_layout: plt.tight_layout() plt.savefig(path, format=fig_extension, dpi=resolution) # # MNIST # **Warning:** since Scikit-Learn 0.24, `fetch_openml()` returns a Pandas `DataFrame` by default. To avoid this and keep the same code as in the book, we use `as_frame=False`. # In[2]: from sklearn.datasets import fetch_openml mnist = fetch_openml('mnist_784', version=1, as_frame=False) mnist.keys() # In[3]: X, y = mnist["data"], mnist["target"] X.shape # In[4]: y.shape # In[5]: 28 * 28 # In[6]: get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib as mpl import matplotlib.pyplot as plt some_digit = X[0] some_digit_image = some_digit.reshape(28, 28) plt.imshow(some_digit_image, cmap=mpl.cm.binary) plt.axis("off") save_fig("some_digit_plot") plt.show() # In[7]: y[0] # In[8]: y = y.astype(np.uint8) # In[9]: def plot_digit(data): image = data.reshape(28, 28) plt.imshow(image, cmap = mpl.cm.binary, interpolation="nearest") plt.axis("off") # In[10]: # EXTRA def plot_digits(instances, images_per_row=10, **options): size = 28 images_per_row = min(len(instances), images_per_row) # This is equivalent to n_rows = ceil(len(instances) / images_per_row): n_rows = (len(instances) - 1) // images_per_row + 1 # Append empty images to fill the end of the grid, if needed: n_empty = n_rows * images_per_row - len(instances) padded_instances = np.concatenate([instances, np.zeros((n_empty, size * size))], axis=0) # Reshape the array so it's organized as a grid containing 28×28 images: image_grid = padded_instances.reshape((n_rows, images_per_row, size, size)) # Combine axes 0 and 2 (vertical image grid axis, and vertical image axis), # and axes 1 and 3 (horizontal axes). We first need to move the axes that we # want to combine next to each other, using transpose(), and only then we # can reshape: big_image = image_grid.transpose(0, 2, 1, 3).reshape(n_rows * size, images_per_row * size) # Now that we have a big image, we just need to show it: plt.imshow(big_image, cmap = mpl.cm.binary, **options) plt.axis("off") # In[11]: plt.figure(figsize=(9,9)) example_images = X[:100] plot_digits(example_images, images_per_row=10) save_fig("more_digits_plot") plt.show() # In[12]: y[0] # In[13]: X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:] # # Training a Binary Classifier # In[14]: y_train_5 = (y_train == 5) y_test_5 = (y_test == 5) # **Note**: some hyperparameters will have a different defaut value in future versions of Scikit-Learn, such as `max_iter` and `tol`. To be future-proof, we explicitly set these hyperparameters to their future default values. For simplicity, this is not shown in the book. # In[15]: from sklearn.linear_model import SGDClassifier sgd_clf = SGDClassifier(max_iter=1000, tol=1e-3, random_state=42) sgd_clf.fit(X_train, y_train_5) # In[16]: sgd_clf.predict([some_digit]) # In[17]: from sklearn.model_selection import cross_val_score cross_val_score(sgd_clf, X_train, y_train_5, cv=3, scoring="accuracy") # # Performance Measures # ## Measuring Accuracy Using Cross-Validation # In[18]: from sklearn.model_selection import StratifiedKFold from sklearn.base import clone skfolds = StratifiedKFold(n_splits=3, shuffle=True, random_state=42) for train_index, test_index in skfolds.split(X_train, y_train_5): clone_clf = clone(sgd_clf) X_train_folds = X_train[train_index] y_train_folds = y_train_5[train_index] X_test_fold = X_train[test_index] y_test_fold = y_train_5[test_index] clone_clf.fit(X_train_folds, y_train_folds) y_pred = clone_clf.predict(X_test_fold) n_correct = sum(y_pred == y_test_fold) print(n_correct / len(y_pred)) # **Note**: `shuffle=True` was omitted by mistake in previous releases of the book. # In[19]: from sklearn.base import BaseEstimator class Never5Classifier(BaseEstimator): def fit(self, X, y=None): pass def predict(self, X): return np.zeros((len(X), 1), dtype=bool) # In[20]: never_5_clf = Never5Classifier() cross_val_score(never_5_clf, X_train, y_train_5, cv=3, scoring="accuracy") # **Warning**: this output (and many others in this notebook and other notebooks) may differ slightly from those in the book. Don't worry, that's okay! There are several reasons for this: # * first, Scikit-Learn and other libraries evolve, and algorithms get tweaked a bit, which may change the exact result you get. If you use the latest Scikit-Learn version (and in general, you really should), you probably won't be using the exact same version I used when I wrote the book or this notebook, hence the difference. I try to keep this notebook reasonably up to date, but I can't change the numbers on the pages in your copy of the book. # * second, many training algorithms are stochastic, meaning they rely on randomness. In principle, it's possible to get consistent outputs from a random number generator by setting the seed from which it generates the pseudo-random numbers (which is why you will see `random_state=42` or `np.random.seed(42)` pretty often). However, sometimes this does not suffice due to the other factors listed here. # * third, if the training algorithm runs across multiple threads (as do some algorithms implemented in C) or across multiple processes (e.g., when using the `n_jobs` argument), then the precise order in which operations will run is not always guaranteed, and thus the exact result may vary slightly. # * lastly, other things may prevent perfect reproducibility, such as Python dicts and sets whose order is not guaranteed to be stable across sessions, or the order of files in a directory which is also not guaranteed. # ## Confusion Matrix # In[21]: from sklearn.model_selection import cross_val_predict y_train_pred = cross_val_predict(sgd_clf, X_train, y_train_5, cv=3) # In[22]: from sklearn.metrics import confusion_matrix confusion_matrix(y_train_5, y_train_pred) # In[23]: y_train_perfect_predictions = y_train_5 # pretend we reached perfection confusion_matrix(y_train_5, y_train_perfect_predictions) # ## Precision and Recall # In[24]: from sklearn.metrics import precision_score, recall_score precision_score(y_train_5, y_train_pred) # In[25]: cm = confusion_matrix(y_train_5, y_train_pred) cm[1, 1] / (cm[0, 1] + cm[1, 1]) # In[26]: recall_score(y_train_5, y_train_pred) # In[27]: cm[1, 1] / (cm[1, 0] + cm[1, 1]) # In[28]: from sklearn.metrics import f1_score f1_score(y_train_5, y_train_pred) # In[29]: cm[1, 1] / (cm[1, 1] + (cm[1, 0] + cm[0, 1]) / 2) # ## Precision/Recall Trade-off # In[30]: y_scores = sgd_clf.decision_function([some_digit]) y_scores # In[31]: threshold = 0 y_some_digit_pred = (y_scores > threshold) # In[32]: y_some_digit_pred # In[33]: threshold = 8000 y_some_digit_pred = (y_scores > threshold) y_some_digit_pred # In[34]: y_scores = cross_val_predict(sgd_clf, X_train, y_train_5, cv=3, method="decision_function") # In[35]: from sklearn.metrics import precision_recall_curve precisions, recalls, thresholds = precision_recall_curve(y_train_5, y_scores) # In[36]: def plot_precision_recall_vs_threshold(precisions, recalls, thresholds): plt.plot(thresholds, precisions[:-1], "b--", label="Precision", linewidth=2) plt.plot(thresholds, recalls[:-1], "g-", label="Recall", linewidth=2) plt.legend(loc="center right", fontsize=16) # Not shown in the book plt.xlabel("Threshold", fontsize=16) # Not shown plt.grid(True) # Not shown plt.axis([-50000, 50000, 0, 1]) # Not shown recall_90_precision = recalls[np.argmax(precisions >= 0.90)] threshold_90_precision = thresholds[np.argmax(precisions >= 0.90)] plt.figure(figsize=(8, 4)) # Not shown plot_precision_recall_vs_threshold(precisions, recalls, thresholds) plt.plot([threshold_90_precision, threshold_90_precision], [0., 0.9], "r:") # Not shown plt.plot([-50000, threshold_90_precision], [0.9, 0.9], "r:") # Not shown plt.plot([-50000, threshold_90_precision], [recall_90_precision, recall_90_precision], "r:")# Not shown plt.plot([threshold_90_precision], [0.9], "ro") # Not shown plt.plot([threshold_90_precision], [recall_90_precision], "ro") # Not shown save_fig("precision_recall_vs_threshold_plot") # Not shown plt.show() # In[37]: (y_train_pred == (y_scores > 0)).all() # In[38]: def plot_precision_vs_recall(precisions, recalls): plt.plot(recalls, precisions, "b-", linewidth=2) plt.xlabel("Recall", fontsize=16) plt.ylabel("Precision", fontsize=16) plt.axis([0, 1, 0, 1]) plt.grid(True) plt.figure(figsize=(8, 6)) plot_precision_vs_recall(precisions, recalls) plt.plot([recall_90_precision, recall_90_precision], [0., 0.9], "r:") plt.plot([0.0, recall_90_precision], [0.9, 0.9], "r:") plt.plot([recall_90_precision], [0.9], "ro") save_fig("precision_vs_recall_plot") plt.show() # In[39]: threshold_90_precision = thresholds[np.argmax(precisions >= 0.90)] # In[40]: threshold_90_precision # In[41]: y_train_pred_90 = (y_scores >= threshold_90_precision) # In[42]: precision_score(y_train_5, y_train_pred_90) # In[43]: recall_score(y_train_5, y_train_pred_90) # ## The ROC Curve # In[44]: from sklearn.metrics import roc_curve fpr, tpr, thresholds = roc_curve(y_train_5, y_scores) # In[45]: def plot_roc_curve(fpr, tpr, label=None): plt.plot(fpr, tpr, linewidth=2, label=label) plt.plot([0, 1], [0, 1], 'k--') # dashed diagonal plt.axis([0, 1, 0, 1]) # Not shown in the book plt.xlabel('False Positive Rate (Fall-Out)', fontsize=16) # Not shown plt.ylabel('True Positive Rate (Recall)', fontsize=16) # Not shown plt.grid(True) # Not shown plt.figure(figsize=(8, 6)) # Not shown plot_roc_curve(fpr, tpr) fpr_90 = fpr[np.argmax(tpr >= recall_90_precision)] # Not shown plt.plot([fpr_90, fpr_90], [0., recall_90_precision], "r:") # Not shown plt.plot([0.0, fpr_90], [recall_90_precision, recall_90_precision], "r:") # Not shown plt.plot([fpr_90], [recall_90_precision], "ro") # Not shown save_fig("roc_curve_plot") # Not shown plt.show() # In[46]: from sklearn.metrics import roc_auc_score roc_auc_score(y_train_5, y_scores) # **Note**: we set `n_estimators=100` to be future-proof since this will be the default value in Scikit-Learn 0.22. # In[47]: from sklearn.ensemble import RandomForestClassifier forest_clf = RandomForestClassifier(n_estimators=100, random_state=42) y_probas_forest = cross_val_predict(forest_clf, X_train, y_train_5, cv=3, method="predict_proba") # In[48]: y_scores_forest = y_probas_forest[:, 1] # score = proba of positive class fpr_forest, tpr_forest, thresholds_forest = roc_curve(y_train_5,y_scores_forest) # In[49]: recall_for_forest = tpr_forest[np.argmax(fpr_forest >= fpr_90)] plt.figure(figsize=(8, 6)) plt.plot(fpr, tpr, "b:", linewidth=2, label="SGD") plot_roc_curve(fpr_forest, tpr_forest, "Random Forest") plt.plot([fpr_90, fpr_90], [0., recall_90_precision], "r:") plt.plot([0.0, fpr_90], [recall_90_precision, recall_90_precision], "r:") plt.plot([fpr_90], [recall_90_precision], "ro") plt.plot([fpr_90, fpr_90], [0., recall_for_forest], "r:") plt.plot([fpr_90], [recall_for_forest], "ro") plt.grid(True) plt.legend(loc="lower right", fontsize=16) save_fig("roc_curve_comparison_plot") plt.show() # In[50]: roc_auc_score(y_train_5, y_scores_forest) # In[51]: y_train_pred_forest = cross_val_predict(forest_clf, X_train, y_train_5, cv=3) precision_score(y_train_5, y_train_pred_forest) # In[52]: recall_score(y_train_5, y_train_pred_forest) # # Multiclass Classification # In[53]: from sklearn.svm import SVC svm_clf = SVC(gamma="auto", random_state=42) svm_clf.fit(X_train[:1000], y_train[:1000]) # y_train, not y_train_5 svm_clf.predict([some_digit]) # In[54]: some_digit_scores = svm_clf.decision_function([some_digit]) some_digit_scores # In[55]: np.argmax(some_digit_scores) # In[56]: svm_clf.classes_ # In[57]: svm_clf.classes_[5] # In[58]: from sklearn.multiclass import OneVsRestClassifier ovr_clf = OneVsRestClassifier(SVC(gamma="auto", random_state=42)) ovr_clf.fit(X_train[:1000], y_train[:1000]) ovr_clf.predict([some_digit]) # In[59]: len(ovr_clf.estimators_) # In[60]: sgd_clf.fit(X_train, y_train) sgd_clf.predict([some_digit]) # In[61]: sgd_clf.decision_function([some_digit]) # **Warning**: the following two cells may take close to 30 minutes to run, or more depending on your hardware. # In[62]: cross_val_score(sgd_clf, X_train, y_train, cv=3, scoring="accuracy") # In[63]: 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") # # Error Analysis # In[64]: y_train_pred = cross_val_predict(sgd_clf, X_train_scaled, y_train, cv=3) conf_mx = confusion_matrix(y_train, y_train_pred) conf_mx # In[65]: # since sklearn 0.22, you can use sklearn.metrics.plot_confusion_matrix() def plot_confusion_matrix(matrix): """If you prefer color and a colorbar""" fig = plt.figure(figsize=(8,8)) ax = fig.add_subplot(111) cax = ax.matshow(matrix) fig.colorbar(cax) # In[66]: plt.matshow(conf_mx, cmap=plt.cm.gray) save_fig("confusion_matrix_plot", tight_layout=False) plt.show() # In[67]: row_sums = conf_mx.sum(axis=1, keepdims=True) norm_conf_mx = conf_mx / row_sums # In[68]: np.fill_diagonal(norm_conf_mx, 0) plt.matshow(norm_conf_mx, cmap=plt.cm.gray) save_fig("confusion_matrix_errors_plot", tight_layout=False) plt.show() # In[69]: cl_a, cl_b = 3, 5 X_aa = X_train[(y_train == cl_a) & (y_train_pred == cl_a)] X_ab = X_train[(y_train == cl_a) & (y_train_pred == cl_b)] X_ba = X_train[(y_train == cl_b) & (y_train_pred == cl_a)] X_bb = X_train[(y_train == cl_b) & (y_train_pred == cl_b)] plt.figure(figsize=(8,8)) plt.subplot(221); plot_digits(X_aa[:25], images_per_row=5) plt.subplot(222); plot_digits(X_ab[:25], images_per_row=5) plt.subplot(223); plot_digits(X_ba[:25], images_per_row=5) plt.subplot(224); plot_digits(X_bb[:25], images_per_row=5) save_fig("error_analysis_digits_plot") plt.show() # # Multilabel Classification # In[70]: from sklearn.neighbors import KNeighborsClassifier y_train_large = (y_train >= 7) y_train_odd = (y_train % 2 == 1) y_multilabel = np.c_[y_train_large, y_train_odd] knn_clf = KNeighborsClassifier() knn_clf.fit(X_train, y_multilabel) # In[71]: knn_clf.predict([some_digit]) # **Warning**: the following cell may take a very long time (possibly hours depending on your hardware). # In[72]: y_train_knn_pred = cross_val_predict(knn_clf, X_train, y_multilabel, cv=3) f1_score(y_multilabel, y_train_knn_pred, average="macro") # # Multioutput Classification # In[73]: noise = np.random.randint(0, 100, (len(X_train), 784)) X_train_mod = X_train + noise noise = np.random.randint(0, 100, (len(X_test), 784)) X_test_mod = X_test + noise y_train_mod = X_train y_test_mod = X_test # In[74]: some_index = 0 plt.subplot(121); plot_digit(X_test_mod[some_index]) plt.subplot(122); plot_digit(y_test_mod[some_index]) save_fig("noisy_digit_example_plot") plt.show() # In[75]: knn_clf.fit(X_train_mod, y_train_mod) clean_digit = knn_clf.predict([X_test_mod[some_index]]) plot_digit(clean_digit) save_fig("cleaned_digit_example_plot") # # Extra material # ## Dummy (ie. random) classifier # In[76]: from sklearn.dummy import DummyClassifier dmy_clf = DummyClassifier(strategy="prior") y_probas_dmy = cross_val_predict(dmy_clf, X_train, y_train_5, cv=3, method="predict_proba") y_scores_dmy = y_probas_dmy[:, 1] # In[77]: fprr, tprr, thresholdsr = roc_curve(y_train_5, y_scores_dmy) plot_roc_curve(fprr, tprr) # ## KNN classifier # In[78]: from sklearn.neighbors import KNeighborsClassifier knn_clf = KNeighborsClassifier(weights='distance', n_neighbors=4) knn_clf.fit(X_train, y_train) # In[79]: y_knn_pred = knn_clf.predict(X_test) # In[80]: from sklearn.metrics import accuracy_score accuracy_score(y_test, y_knn_pred) # In[81]: from scipy.ndimage.interpolation import shift def shift_digit(digit_array, dx, dy, new=0): return shift(digit_array.reshape(28, 28), [dy, dx], cval=new).reshape(784) plot_digit(shift_digit(some_digit, 5, 1, new=100)) # In[82]: X_train_expanded = [X_train] y_train_expanded = [y_train] for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)): shifted_images = np.apply_along_axis(shift_digit, axis=1, arr=X_train, dx=dx, dy=dy) X_train_expanded.append(shifted_images) y_train_expanded.append(y_train) X_train_expanded = np.concatenate(X_train_expanded) y_train_expanded = np.concatenate(y_train_expanded) X_train_expanded.shape, y_train_expanded.shape # In[83]: knn_clf.fit(X_train_expanded, y_train_expanded) # In[84]: y_knn_expanded_pred = knn_clf.predict(X_test) # In[85]: accuracy_score(y_test, y_knn_expanded_pred) # In[86]: ambiguous_digit = X_test[2589] knn_clf.predict_proba([ambiguous_digit]) # In[87]: plot_digit(ambiguous_digit) # # Exercise solutions # ## 1. An MNIST Classifier With Over 97% Accuracy # **Warning**: the next cell may take close to 16 hours to run, or more depending on your hardware. # In[88]: from sklearn.model_selection import GridSearchCV param_grid = [{'weights': ["uniform", "distance"], 'n_neighbors': [3, 4, 5]}] knn_clf = KNeighborsClassifier() grid_search = GridSearchCV(knn_clf, param_grid, cv=5, verbose=3) grid_search.fit(X_train, y_train) # In[89]: grid_search.best_params_ # In[90]: grid_search.best_score_ # In[91]: from sklearn.metrics import accuracy_score y_pred = grid_search.predict(X_test) accuracy_score(y_test, y_pred) # ## 2. Data Augmentation # In[92]: from scipy.ndimage.interpolation import shift # In[93]: def shift_image(image, dx, dy): image = image.reshape((28, 28)) shifted_image = shift(image, [dy, dx], cval=0, mode="constant") return shifted_image.reshape([-1]) # In[94]: image = X_train[1000] shifted_image_down = shift_image(image, 0, 5) shifted_image_left = shift_image(image, -5, 0) plt.figure(figsize=(12,3)) plt.subplot(131) plt.title("Original", fontsize=14) plt.imshow(image.reshape(28, 28), interpolation="nearest", cmap="Greys") plt.subplot(132) plt.title("Shifted down", fontsize=14) plt.imshow(shifted_image_down.reshape(28, 28), interpolation="nearest", cmap="Greys") plt.subplot(133) plt.title("Shifted left", fontsize=14) plt.imshow(shifted_image_left.reshape(28, 28), interpolation="nearest", cmap="Greys") plt.show() # In[95]: X_train_augmented = [image for image in X_train] y_train_augmented = [label for label in y_train] for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)): for image, label in zip(X_train, y_train): X_train_augmented.append(shift_image(image, dx, dy)) y_train_augmented.append(label) X_train_augmented = np.array(X_train_augmented) y_train_augmented = np.array(y_train_augmented) # In[96]: shuffle_idx = np.random.permutation(len(X_train_augmented)) X_train_augmented = X_train_augmented[shuffle_idx] y_train_augmented = y_train_augmented[shuffle_idx] # In[97]: knn_clf = KNeighborsClassifier(**grid_search.best_params_) # In[98]: knn_clf.fit(X_train_augmented, y_train_augmented) # **Warning**: the following cell may take close to an hour to run, depending on your hardware. # In[99]: y_pred = knn_clf.predict(X_test) accuracy_score(y_test, y_pred) # By simply augmenting the data, we got a 0.5% accuracy boost. :) # ## 3. Tackle the Titanic dataset # The goal is to predict whether or not a passenger survived based on attributes such as their age, sex, passenger class, where they embarked and so on. # Let's fetch the data and load it: # In[100]: import os import urllib.request TITANIC_PATH = os.path.join("datasets", "titanic") DOWNLOAD_URL = "https://raw.githubusercontent.com/ageron/handson-ml2/master/datasets/titanic/" def fetch_titanic_data(url=DOWNLOAD_URL, path=TITANIC_PATH): if not os.path.isdir(path): os.makedirs(path) for filename in ("train.csv", "test.csv"): filepath = os.path.join(path, filename) if not os.path.isfile(filepath): print("Downloading", filename) urllib.request.urlretrieve(url + filename, filepath) fetch_titanic_data() # In[101]: import pandas as pd def load_titanic_data(filename, titanic_path=TITANIC_PATH): csv_path = os.path.join(titanic_path, filename) return pd.read_csv(csv_path) # In[102]: train_data = load_titanic_data("train.csv") test_data = load_titanic_data("test.csv") # The data is already split into a training set and a test set. However, the test data does *not* contain the labels: your goal is to train the best model you can using the training data, then make your predictions on the test data and upload them to Kaggle to see your final score. # Let's take a peek at the top few rows of the training set: # In[103]: train_data.head() # The attributes have the following meaning: # * **PassengerId**: a unique identifier for each passenger # * **Survived**: that's the target, 0 means the passenger did not survive, while 1 means he/she survived. # * **Pclass**: passenger class. # * **Name**, **Sex**, **Age**: self-explanatory # * **SibSp**: how many siblings & spouses of the passenger aboard the Titanic. # * **Parch**: how many children & parents of the passenger aboard the Titanic. # * **Ticket**: ticket id # * **Fare**: price paid (in pounds) # * **Cabin**: passenger's cabin number # * **Embarked**: where the passenger embarked the Titanic # Let's explicitly set the `PassengerId` column as the index column: # In[104]: train_data = train_data.set_index("PassengerId") test_data = test_data.set_index("PassengerId") # Let's get more info to see how much data is missing: # In[105]: train_data.info() # In[106]: train_data[train_data["Sex"]=="female"]["Age"].median() # Okay, the **Age**, **Cabin** and **Embarked** attributes are sometimes null (less than 891 non-null), especially the **Cabin** (77% are null). We will ignore the **Cabin** for now and focus on the rest. The **Age** attribute has about 19% null values, so we will need to decide what to do with them. Replacing null values with the median age seems reasonable. We could be a bit smarter by predicting the age based on the other columns (for example, the median age is 37 in 1st class, 29 in 2nd class and 24 in 3rd class), but we'll keep things simple and just use the overall median age. # The **Name** and **Ticket** attributes may have some value, but they will be a bit tricky to convert into useful numbers that a model can consume. So for now, we will ignore them. # Let's take a look at the numerical attributes: # In[107]: train_data.describe() # * Yikes, only 38% **Survived**! 😭 That's close enough to 40%, so accuracy will be a reasonable metric to evaluate our model. # * The mean **Fare** was £32.20, which does not seem so expensive (but it was probably a lot of money back then). # * The mean **Age** was less than 30 years old. # Let's check that the target is indeed 0 or 1: # In[108]: train_data["Survived"].value_counts() # Now let's take a quick look at all the categorical attributes: # In[109]: train_data["Pclass"].value_counts() # In[110]: train_data["Sex"].value_counts() # In[111]: train_data["Embarked"].value_counts() # The Embarked attribute tells us where the passenger embarked: C=Cherbourg, Q=Queenstown, S=Southampton. # Now let's build our preprocessing pipelines, starting with the pipeline for numerical attributes: # In[112]: from sklearn.pipeline import Pipeline from sklearn.impute import SimpleImputer from sklearn.preprocessing import StandardScaler num_pipeline = Pipeline([ ("imputer", SimpleImputer(strategy="median")), ("scaler", StandardScaler()) ]) # Now we can build the pipeline for the categorical attributes: # In[113]: from sklearn.preprocessing import OneHotEncoder # In[114]: cat_pipeline = Pipeline([ ("imputer", SimpleImputer(strategy="most_frequent")), ("cat_encoder", OneHotEncoder(sparse=False)), ]) # Finally, let's join the numerical and categorical pipelines: # In[115]: from sklearn.compose import ColumnTransformer num_attribs = ["Age", "SibSp", "Parch", "Fare"] cat_attribs = ["Pclass", "Sex", "Embarked"] preprocess_pipeline = ColumnTransformer([ ("num", num_pipeline, num_attribs), ("cat", cat_pipeline, cat_attribs), ]) # Cool! Now we have a nice preprocessing pipeline that takes the raw data and outputs numerical input features that we can feed to any Machine Learning model we want. # In[116]: X_train = preprocess_pipeline.fit_transform( train_data[num_attribs + cat_attribs]) X_train # Let's not forget to get the labels: # In[117]: y_train = train_data["Survived"] # We are now ready to train a classifier. Let's start with a `RandomForestClassifier`: # In[118]: from sklearn.ensemble import RandomForestClassifier forest_clf = RandomForestClassifier(n_estimators=100, random_state=42) forest_clf.fit(X_train, y_train) # Great, our model is trained, let's use it to make predictions on the test set: # In[119]: X_test = preprocess_pipeline.transform(test_data[num_attribs + cat_attribs]) y_pred = forest_clf.predict(X_test) # And now we could just build a CSV file with these predictions (respecting the format excepted by Kaggle), then upload it and hope for the best. But wait! We can do better than hope. Why don't we use cross-validation to have an idea of how good our model is? # In[120]: from sklearn.model_selection import cross_val_score forest_scores = cross_val_score(forest_clf, X_train, y_train, cv=10) forest_scores.mean() # Okay, not too bad! Looking at the [leaderboard](https://www.kaggle.com/c/titanic/leaderboard) for the Titanic competition on Kaggle, you can see that our score is in the top 2%, woohoo! Some Kagglers reached 100% accuracy, but since you can easily find the [list of victims](https://www.encyclopedia-titanica.org/titanic-victims/) of the Titanic, it seems likely that there was little Machine Learning involved in their performance! 😆 # Let's try an `SVC`: # In[121]: from sklearn.svm import SVC svm_clf = SVC(gamma="auto") svm_scores = cross_val_score(svm_clf, X_train, y_train, cv=10) svm_scores.mean() # Great! This model looks better. # But instead of just looking at the mean accuracy across the 10 cross-validation folds, let's plot all 10 scores for each model, along with a box plot highlighting the lower and upper quartiles, and "whiskers" showing the extent of the scores (thanks to Nevin Yilmaz for suggesting this visualization). Note that the `boxplot()` function detects outliers (called "fliers") and does not include them within the whiskers. Specifically, if the lower quartile is $Q_1$ and the upper quartile is $Q_3$, then the interquartile range $IQR = Q_3 - Q_1$ (this is the box's height), and any score lower than $Q_1 - 1.5 \times IQR$ is a flier, and so is any score greater than $Q3 + 1.5 \times IQR$. # In[122]: import matplotlib.pyplot as plt plt.figure(figsize=(8, 4)) plt.plot([1]*10, svm_scores, ".") plt.plot([2]*10, forest_scores, ".") plt.boxplot([svm_scores, forest_scores], labels=("SVM","Random Forest")) plt.ylabel("Accuracy", fontsize=14) plt.show() # The random forest classifier got a very high score on one of the 10 folds, but overall it had a lower mean score, as well as a bigger spread, so it looks like the SVM classifier is more likely to generalize well. # To improve this result further, you could: # * Compare many more models and tune hyperparameters using cross validation and grid search, # * Do more feature engineering, for example: # * Try to convert numerical attributes to categorical attributes: for example, different age groups had very different survival rates (see below), so it may help to create an age bucket category and use it instead of the age. Similarly, it may be useful to have a special category for people traveling alone since only 30% of them survived (see below). # * Replace **SibSp** and **Parch** with their sum. # * Try to identify parts of names that correlate well with the **Survived** attribute. # * Use the **Cabin** column, for example take its first letter and treat it as a categorical attribute. # In[123]: train_data["AgeBucket"] = train_data["Age"] // 15 * 15 train_data[["AgeBucket", "Survived"]].groupby(['AgeBucket']).mean() # In[124]: train_data["RelativesOnboard"] = train_data["SibSp"] + train_data["Parch"] train_data[["RelativesOnboard", "Survived"]].groupby(['RelativesOnboard']).mean() # ## 4. Spam classifier # First, let's fetch the data: # In[125]: import os import tarfile import urllib.request DOWNLOAD_ROOT = "http://spamassassin.apache.org/old/publiccorpus/" HAM_URL = DOWNLOAD_ROOT + "20030228_easy_ham.tar.bz2" SPAM_URL = DOWNLOAD_ROOT + "20030228_spam.tar.bz2" SPAM_PATH = os.path.join("datasets", "spam") def fetch_spam_data(ham_url=HAM_URL, spam_url=SPAM_URL, spam_path=SPAM_PATH): if not os.path.isdir(spam_path): os.makedirs(spam_path) for filename, url in (("ham.tar.bz2", ham_url), ("spam.tar.bz2", spam_url)): path = os.path.join(spam_path, filename) if not os.path.isfile(path): urllib.request.urlretrieve(url, path) tar_bz2_file = tarfile.open(path) tar_bz2_file.extractall(path=spam_path) tar_bz2_file.close() # In[126]: fetch_spam_data() # Next, let's load all the emails: # In[127]: HAM_DIR = os.path.join(SPAM_PATH, "easy_ham") SPAM_DIR = os.path.join(SPAM_PATH, "spam") ham_filenames = [name for name in sorted(os.listdir(HAM_DIR)) if len(name) > 20] spam_filenames = [name for name in sorted(os.listdir(SPAM_DIR)) if len(name) > 20] # In[128]: len(ham_filenames) # In[129]: len(spam_filenames) # We can use Python's `email` module to parse these emails (this handles headers, encoding, and so on): # In[130]: import email import email.policy def load_email(is_spam, filename, spam_path=SPAM_PATH): directory = "spam" if is_spam else "easy_ham" with open(os.path.join(spam_path, directory, filename), "rb") as f: return email.parser.BytesParser(policy=email.policy.default).parse(f) # In[131]: ham_emails = [load_email(is_spam=False, filename=name) for name in ham_filenames] spam_emails = [load_email(is_spam=True, filename=name) for name in spam_filenames] # Let's look at one example of ham and one example of spam, to get a feel of what the data looks like: # In[132]: print(ham_emails[1].get_content().strip()) # In[133]: print(spam_emails[6].get_content().strip()) # Some emails are actually multipart, with images and attachments (which can have their own attachments). Let's look at the various types of structures we have: # In[134]: def get_email_structure(email): if isinstance(email, str): return email payload = email.get_payload() if isinstance(payload, list): return "multipart({})".format(", ".join([ get_email_structure(sub_email) for sub_email in payload ])) else: return email.get_content_type() # In[135]: from collections import Counter def structures_counter(emails): structures = Counter() for email in emails: structure = get_email_structure(email) structures[structure] += 1 return structures # In[136]: structures_counter(ham_emails).most_common() # In[137]: structures_counter(spam_emails).most_common() # It seems that the ham emails are more often plain text, while spam has quite a lot of HTML. Moreover, quite a few ham emails are signed using PGP, while no spam is. In short, it seems that the email structure is useful information to have. # Now let's take a look at the email headers: # In[138]: for header, value in spam_emails[0].items(): print(header,":",value) # There's probably a lot of useful information in there, such as the sender's email address (12a1mailbot1@web.de looks fishy), but we will just focus on the `Subject` header: # In[139]: spam_emails[0]["Subject"] # Okay, before we learn too much about the data, let's not forget to split it into a training set and a test set: # In[140]: import numpy as np from sklearn.model_selection import train_test_split X = np.array(ham_emails + spam_emails, dtype=object) y = np.array([0] * len(ham_emails) + [1] * len(spam_emails)) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Okay, let's start writing the preprocessing functions. First, we will need a function to convert HTML to plain text. Arguably the best way to do this would be to use the great [BeautifulSoup](https://www.crummy.com/software/BeautifulSoup/) library, but I would like to avoid adding another dependency to this project, so let's hack a quick & dirty solution using regular expressions (at the risk of [un̨ho͞ly radiańcé destro҉ying all enli̍̈́̂̈́ghtenment](https://stackoverflow.com/a/1732454/38626)). The following function first drops the `` section, then converts all `` tags to the word HYPERLINK, then it gets rid of all HTML tags, leaving only the plain text. For readability, it also replaces multiple newlines with single newlines, and finally it unescapes html entities (such as `>` or ` `): # In[141]: import re from html import unescape def html_to_plain_text(html): text = re.sub('.*?', '', html, flags=re.M | re.S | re.I) text = re.sub('', ' HYPERLINK ', text, flags=re.M | re.S | re.I) text = re.sub('<.*?>', '', text, flags=re.M | re.S) text = re.sub(r'(\s*\n)+', '\n', text, flags=re.M | re.S) return unescape(text) # Let's see if it works. This is HTML spam: # In[142]: html_spam_emails = [email for email in X_train[y_train==1] if get_email_structure(email) == "text/html"] sample_html_spam = html_spam_emails[7] print(sample_html_spam.get_content().strip()[:1000], "...") # And this is the resulting plain text: # In[143]: print(html_to_plain_text(sample_html_spam.get_content())[:1000], "...") # Great! Now let's write a function that takes an email as input and returns its content as plain text, whatever its format is: # In[144]: def email_to_text(email): html = None for part in email.walk(): ctype = part.get_content_type() if not ctype in ("text/plain", "text/html"): continue try: content = part.get_content() except: # in case of encoding issues content = str(part.get_payload()) if ctype == "text/plain": return content else: html = content if html: return html_to_plain_text(html) # In[145]: print(email_to_text(sample_html_spam)[:100], "...") # Let's throw in some stemming! For this to work, you need to install the Natural Language Toolkit ([NLTK](http://www.nltk.org/)). It's as simple as running the following command (don't forget to activate your virtualenv first; if you don't have one, you will likely need administrator rights, or use the `--user` option): # # `$ pip3 install nltk` # In[146]: try: import nltk stemmer = nltk.PorterStemmer() for word in ("Computations", "Computation", "Computing", "Computed", "Compute", "Compulsive"): print(word, "=>", stemmer.stem(word)) except ImportError: print("Error: stemming requires the NLTK module.") stemmer = None # We will also need a way to replace URLs with the word "URL". For this, we could use hard core [regular expressions](https://mathiasbynens.be/demo/url-regex) but we will just use the [urlextract](https://github.com/lipoja/URLExtract) library. You can install it with the following command (don't forget to activate your virtualenv first; if you don't have one, you will likely need administrator rights, or use the `--user` option): # # `$ pip3 install urlextract` # In[147]: # if running this notebook on Colab or Kaggle, we just pip install urlextract if IS_COLAB or IS_KAGGLE: get_ipython().run_line_magic('pip', 'install -q -U urlextract') # **Note:** inside a Jupyter notebook, always use `%pip` instead of `!pip`, as `!pip` may install the library inside the wrong environment, while `%pip` makes sure it's installed inside the currently running environment. # In[148]: try: import urlextract # may require an Internet connection to download root domain names url_extractor = urlextract.URLExtract() print(url_extractor.find_urls("Will it detect github.com and https://youtu.be/7Pq-S557XQU?t=3m32s")) except ImportError: print("Error: replacing URLs requires the urlextract module.") url_extractor = None # We are ready to put all this together into a transformer that we will use to convert emails to word counters. Note that we split sentences into words using Python's `split()` method, which uses whitespaces for word boundaries. This works for many written languages, but not all. For example, Chinese and Japanese scripts generally don't use spaces between words, and Vietnamese often uses spaces even between syllables. It's okay in this exercise, because the dataset is (mostly) in English. # In[149]: from sklearn.base import BaseEstimator, TransformerMixin class EmailToWordCounterTransformer(BaseEstimator, TransformerMixin): def __init__(self, strip_headers=True, lower_case=True, remove_punctuation=True, replace_urls=True, replace_numbers=True, stemming=True): self.strip_headers = strip_headers self.lower_case = lower_case self.remove_punctuation = remove_punctuation self.replace_urls = replace_urls self.replace_numbers = replace_numbers self.stemming = stemming def fit(self, X, y=None): return self def transform(self, X, y=None): X_transformed = [] for email in X: text = email_to_text(email) or "" if self.lower_case: text = text.lower() if self.replace_urls and url_extractor is not None: urls = list(set(url_extractor.find_urls(text))) urls.sort(key=lambda url: len(url), reverse=True) for url in urls: text = text.replace(url, " URL ") if self.replace_numbers: text = re.sub(r'\d+(?:\.\d*)?(?:[eE][+-]?\d+)?', 'NUMBER', text) if self.remove_punctuation: text = re.sub(r'\W+', ' ', text, flags=re.M) word_counts = Counter(text.split()) if self.stemming and stemmer is not None: stemmed_word_counts = Counter() for word, count in word_counts.items(): stemmed_word = stemmer.stem(word) stemmed_word_counts[stemmed_word] += count word_counts = stemmed_word_counts X_transformed.append(word_counts) return np.array(X_transformed) # Let's try this transformer on a few emails: # In[150]: X_few = X_train[:3] X_few_wordcounts = EmailToWordCounterTransformer().fit_transform(X_few) X_few_wordcounts # This looks about right! # Now we have the word counts, and we need to convert them to vectors. For this, we will build another transformer whose `fit()` method will build the vocabulary (an ordered list of the most common words) and whose `transform()` method will use the vocabulary to convert word counts to vectors. The output is a sparse matrix. # In[151]: from scipy.sparse import csr_matrix class WordCounterToVectorTransformer(BaseEstimator, TransformerMixin): def __init__(self, vocabulary_size=1000): self.vocabulary_size = vocabulary_size def fit(self, X, y=None): total_count = Counter() for word_count in X: for word, count in word_count.items(): total_count[word] += min(count, 10) most_common = total_count.most_common()[:self.vocabulary_size] self.vocabulary_ = {word: index + 1 for index, (word, count) in enumerate(most_common)} return self def transform(self, X, y=None): rows = [] cols = [] data = [] for row, word_count in enumerate(X): for word, count in word_count.items(): rows.append(row) cols.append(self.vocabulary_.get(word, 0)) data.append(count) return csr_matrix((data, (rows, cols)), shape=(len(X), self.vocabulary_size + 1)) # In[152]: vocab_transformer = WordCounterToVectorTransformer(vocabulary_size=10) X_few_vectors = vocab_transformer.fit_transform(X_few_wordcounts) X_few_vectors # In[153]: X_few_vectors.toarray() # What does this matrix mean? Well, the 99 in the second row, first column, means that the second email contains 99 words that are not part of the vocabulary. The 11 next to it means that the first word in the vocabulary is present 11 times in this email. The 9 next to it means that the second word is present 9 times, and so on. You can look at the vocabulary to know which words we are talking about. The first word is "the", the second word is "of", etc. # In[154]: vocab_transformer.vocabulary_ # We are now ready to train our first spam classifier! Let's transform the whole dataset: # In[155]: from sklearn.pipeline import Pipeline preprocess_pipeline = Pipeline([ ("email_to_wordcount", EmailToWordCounterTransformer()), ("wordcount_to_vector", WordCounterToVectorTransformer()), ]) X_train_transformed = preprocess_pipeline.fit_transform(X_train) # **Note**: to be future-proof, we set `solver="lbfgs"` since this will be the default value in Scikit-Learn 0.22. # In[156]: from sklearn.linear_model import LogisticRegression from sklearn.model_selection import cross_val_score log_clf = LogisticRegression(solver="lbfgs", max_iter=1000, random_state=42) score = cross_val_score(log_clf, X_train_transformed, y_train, cv=3, verbose=3) score.mean() # Over 98.5%, not bad for a first try! :) However, remember that we are using the "easy" dataset. You can try with the harder datasets, the results won't be so amazing. You would have to try multiple models, select the best ones and fine-tune them using cross-validation, and so on. # # But you get the picture, so let's stop now, and just print out the precision/recall we get on the test set: # In[157]: from sklearn.metrics import precision_score, recall_score X_test_transformed = preprocess_pipeline.transform(X_test) log_clf = LogisticRegression(solver="lbfgs", max_iter=1000, random_state=42) log_clf.fit(X_train_transformed, y_train) y_pred = log_clf.predict(X_test_transformed) print("Precision: {:.2f}%".format(100 * precision_score(y_test, y_pred))) print("Recall: {:.2f}%".format(100 * recall_score(y_test, y_pred))) # In[ ]: