<jupyter_start><jupyter_text>Import Data<jupyter_code>df = pd.read_csv("data/small_mnist.csv")
df.head()
df.describe()<jupyter_output><empty_output><jupyter_text>Dataset Splitting<jupyter_code>X = df.drop(columns="label").values / 255
y = df.label.values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)
X_train.shape, X_test.shape, y_train.shape, y_test.shape<jupyter_output><empty_output><jupyter_text>Visualize Random Data<jupyter_code>import matplotlib.pyplot as plt
N = len(X_train)
idx = np.random.choice(N)
img, label = X_train[idx], y_train[idx]

plt.imshow(img.reshape(28, 28), cmap='gray')
plt.title(label)
plt.axis('off');<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import RandomizedSearchCV
from jcopml.tuning import random_search_params as rsp
pipeline = Pipeline([
    ('algo', RandomForestClassifier(n_jobs=-1, random_state=42))
])

model = RandomizedSearchCV(pipeline, rsp.rf_params, cv=3, n_iter=50, n_jobs=-1, verbose=1, random_state=42)
model.fit(X_train, y_train)

print(model.best_params_)
print(model.score(X_train, y_train), model.best_score_, model.score(X_test, y_test))<jupyter_output>Fitting 3 folds for each of 50 candidates, totalling 150 fits<jupyter_text>Visualize Prediction<jupyter_code>idx = np.random.choice(len(X_test), size=36, replace=False)
images, labels = X_test[idx], y_test[idx]
preds = model.predict(images)

fig, axes = plt.subplots(6, 6, figsize=(15, 15))
for img, label, pred, ax in zip(images, labels, preds, axes.flatten()):
    font = {'color':'g'} if label == pred else {'color':'r'}
    
    ax.imshow(img.reshape(28, 28), cmap='gray')
    ax.set_title(f'Label: {label} | Pred: {pred}', fontdict=font)
    ax.axis('off');<jupyter_output><empty_output><jupyter_text>Why it works?<jupyter_code>fig, axes = plt.subplots(5, 2, figsize=(16, 12))

for idx, ax in enumerate(axes.flatten()):
    ax.imshow(df.iloc[idx*200:(idx+1)*200], cmap='gray')
    ax.set_title(f'Labl: {idx}')
    ax.axis('off');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15431519699812382, 'fraction_numerical': 0.024390243902439025, 'mean_word_length': 7.397637795275591, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '348965', 'license': 'MIT', 'n_tokens_mistral': 886, 'n_tokens_neox': 840, 'n_words': 144, 'path': 'Digit Recognition - Random Forest Classifier.ipynb', 'repo_name': 'raynaldydk/ds_case_study', 'size': '2132'}