<jupyter_start><jupyter_text>Exercises chapter 3 Exercise 1Try to build a classifier for the MNIST dataset that achieves over 97% accuracy on the test set. Hint: the KNeighborsClassifier works quite well for this task; you just need to find good hyperparameter values (try a grid search on the weights and n_neighbors hyperparameters).<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

%matplotlib inline
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version=1)
X, y = mnist.data, mnist.target
y = y.astype(np.uint8)
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]
plt.imshow(X_train[np.random.choice(60000, size=1)].reshape(28, 28))
from sklearn.base import BaseEstimator, TransformerMixin

class ManualScaler(BaseEstimator, TransformerMixin):
    
    def fit(self, X, y=None):
        return self
    
    def transform(self, X, y=None):
        return X / 255.
from sklearn.preprocessing import MinMaxScaler
from sklearn.pipeline import Pipeline
from sklearn.neighbors import KNeighborsClassifier

preprocess = Pipeline(steps=[
    ('scale', MinMaxScaler())
])

full_pipeline = Pipeline(steps=[
    ('preprocess', preprocess),
    ('classifier', KNeighborsClassifier(n_neighbors=3, weights='uniform'))
])
param_grid = dict(classifier__n_neighbors=[3, 5])
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

grid_search = GridSearchCV(full_pipeline, param_grid=param_grid, n_jobs=2, cv=5, verbose=2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09509011808576756, 'fraction_numerical': 0.02983219390926041, 'mean_word_length': 5.96969696969697, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '74160', 'license': 'Apache-2.0', 'n_tokens_mistral': 571, 'n_tokens_neox': 526, 'n_words': 149, 'path': 'exercises/exercise 3.1.ipynb', 'repo_name': 'gdario/handsonml', 'size': '1609'}