<jupyter_start><jupyter_text>Exercises for Chapter 3: Classification  1) Build a classifier for the MNIST dataset that predicts at 97% accuracyJust in the interest of time, I won't be too focused on achieving 97% accuracy. I want to keep moving on to other parts in the book. I know what I would do and I'm just practicing the basics of what would be done.<jupyter_code>from sklearn.datasets import fetch_mldata
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, RandomizedSearchCV
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
from sklearn.neighbors import KNeighborsClassifier
from scipy.stats import randint
mnist = fetch_mldata('MNIST original')
X, y = mnist['data'], mnist['target']
X = X.astype(np.float64)
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
idx = 501

digit = X[idx].reshape(28, 28)
plt.imshow(digit, cmap=matplotlib.cm.binary, interpolation='nearest')
plt.axis('off')
plt.title("Digit {}".format(y[idx]))
plt.show()
X_train, X_test, y_train, y_test = train_test_split(X, y)
X_train.shape, X_test.shape, y_train.shape, y_test.shape
pipe = Pipeline([
    ('scale', StandardScaler()),
    ('decom', PCA(n_components=64)),
    ('model', KNeighborsClassifier(n_jobs=-1))
])

rand_dists = {
    'model__n_neighbors': randint(3, 10),
    'model__weights': ['uniform', 'distance'],
    'model__algorithm': ['ball_tree', 'kd_tree', 'brute']
}

rand_grid = RandomizedSearchCV(pipe, param_distributions=rand_dists, verbose=2, n_iter=5, cv=2)
rand_grid.fit(X_train, y_train)
est = rand_grid.best_estimator_
est
est.score(X_train, y_train)
est.score(X_test, y_test)
from sklearn.externals import joblib
joblib.dump(est, r'..\saved_models\03_knn_best_est.joblib')<jupyter_output><empty_output><jupyter_text>2) Write a function that shifts the MNIST image in each cardinal direction. Then add a shifted image for each image to the training set.<jupyter_code># https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.shift.html#scipy.ndimage.shift

def im_shift_one(arr, direction):
    dir_map = {'up': [0, 1], 'down': [0, -1], 'left': [-1, 0], 'right': [1, 0]}<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1210762331838565, 'fraction_numerical': 0.01569506726457399, 'mean_word_length': 6.082539682539682, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '487939', 'license': 'Apache-2.0', 'n_tokens_mistral': 823, 'n_tokens_neox': 777, 'n_words': 213, 'path': 'taylor_completed_exercises/exercises_03_classification.ipynb', 'repo_name': 'taylordelehanty/handson-ml', 'size': '2230'}