<jupyter_start><jupyter_text>This program does the multiclass classification of MNIST digits using Random Forest ClassifierThe recognition of hand-written digits which is common problem in optical character recognition problem. This problem includes both locating and identifying the digit in the image. Below is the example code that uses Random Forest class of sklearn package to classify the digits to one of the 10 classes (0 to 9). The code gives the programming steps to achieve this and finally reports the evaluation metrics discussed above.<jupyter_code>from sklearn.datasets import load_digits
handw_digits = load_digits()
print (handw_digits.images.shape)

import matplotlib.pyplot as plt


fig, axes = plt.subplots(1, 10, figsize=(8, 8),
                     subplot_kw={'xticks':[], 'yticks':[]},
                     gridspec_kw=dict(hspace=0.1, wspace=0.1))

for i, ax in enumerate(axes.flat):
    ax.imshow(handw_digits.images[i], cmap='binary', interpolation='nearest')
    ax.text(0.05, 0.05, str(handw_digits.target[i]),
            transform=ax.transAxes, color='green')

plt.show()


X = handw_digits.data
print ("The input shape is: ", X.shape)

y = handw_digits.target
print ("The output shape is: ", y.shape)


from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

Xtrain, Xtest, ytrain, ytest = train_test_split(handw_digits.data, handw_digits.target,
                                    random_state=0)
print ("The training input data shape is: ", Xtrain.shape)
print ("The training target data shape is: ", ytrain.shape)
print ("The testing input data  shape is: ", Xtest.shape)
print ("The testing target data  shape is: ", ytest.shape)


model = RandomForestClassifier(n_estimators=1000)
model.fit(Xtrain, ytrain)
print (model.get_params())
ypred = model.predict(Xtest)

import seaborn as sns; sns.set()

from sklearn.metrics import confusion_matrix
mat = confusion_matrix(ytest, ypred)
sns.heatmap(mat.T, square=False, annot=True, fmt='d', cbar=False)
plt.xlabel('target or actual label')
plt.ylabel('predicted label');
plt.show()<jupyter_output>{'bootstrap': True, 'class_weight': None, 'criterion': 'gini', 'max_depth': None, 'max_features': 'auto', 'max_leaf_nodes': None, 'min_impurity_decrease': 0.0, 'min_impurity_split': None, 'min_samples_leaf': 1, 'min_samples_split': 2, 'min_weight_fraction_leaf': 0.0, 'n_estimators': 1000, 'n_jobs': None, 'oob_score': False, 'random_state': None, 'verbose': 0, 'warm_start': False}
{'fraction_non_alphanumeric': 0.1147410358565737, 'fraction_numerical': 0.013944223107569721, 'mean_word_length': 5.007177033492823, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '315300', 'license': 'MIT', 'n_tokens_mistral': 841, 'n_tokens_neox': 793, 'n_words': 244, 'path': 'Chapter 02/RandomForest_MNIST_ch2.ipynb', 'repo_name': 'bpbpublications/Essentials-of-Deep-Learning-and-AI', 'size': '2510'}