<jupyter_start><jupyter_text>MNIST Classifier  Get Datasets<jupyter_code>from sklearn.datasets import fetch_openml

mnist = fetch_openml('mnist_784', version=1)
# print shape of the data
X, y = mnist['data'], mnist['target']

print("X shape = ", X.shape)
print("y shape = ", y.shape)
# Show examples of datasets
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt

sample = X[1000]
sample = sample.reshape(28, 28) # 28 * 28 = 784

# Plot Data
plt.imshow(sample, cmap=matplotlib.cm.binary, interpolation='nearest')
plt.axis('off')
plt.show()

# Print Answer
print("Answer = ", y[1000])<jupyter_output><empty_output><jupyter_text>Test Set Separation<jupyter_code>X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]

import numpy as np

# Shuffle Training Set for Accuracy
shuffle_index = np.random.permutation(60000)
X_train, y_train = X_train[shuffle_index], y_train[shuffle_index]<jupyter_output><empty_output><jupyter_text>RandomForest Classifier Model<jupyter_code># using randomforest classifier
from sklearn.ensemble import RandomForestClassifier

clf = RandomForestClassifier()
clf.fit(X_train, y_train)
# make prediction using cross_validation score
from sklearn.model_selection import cross_val_score

cross_val_score(clf, X_train, y_train, cv=3, scoring="accuracy")
# to improve accuracy we have to tune training-test set
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X_train_scale = scaler.fit_transform(X_train)

clf.fit(X_train_scale, y_train)
cross_val_score(clf, X_train, y_train, cv=3, scoring="accuracy")
# test score (KNN takes too long to train)

X_test_scale = scaler.fit_transform(X_test)
prediction = clf.predict(X_test_scale)
result = (prediction == y_test).mean()

print(result)<jupyter_output>0.9602<jupyter_text>Visualizing Data<jupyter_code>fig = plt.figure()
random_pick = np.random.randint(low=0, high=70000, size=10)
print(y[random_pick])

for i in range(10):
    idx = random_pick[i]
    img = np.reshape(X[idx], (28, 28))
    plot = fig.add_subplot(1, 10, i+1)
    plot.imshow(img, cmap='binary')<jupyter_output>['5' '1' '6' '7' '7' '0' '3' '6' '1' '6']
{'fraction_non_alphanumeric': 0.1280148423005566, 'fraction_numerical': 0.03849721706864564, 'mean_word_length': 6.843636363636364, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '209264', 'license': 'MIT', 'n_tokens_mistral': 868, 'n_tokens_neox': 816, 'n_words': 179, 'path': 'MNIST/MNIST_Classifier.ipynb', 'repo_name': 'sckimynwa/Hands-On_ML', 'size': '2156'}