<jupyter_start><jupyter_text>MNIST classification using structured-RerFThis was adapted from: https://scikit-learn.org/stable/auto_examples/linear_model/plot_sparse_logistic_regression_mnist.html<jupyter_code>import time
import matplotlib.pyplot as plt
import numpy as np

from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.utils import check_random_state

from rerf.rerfClassifier import rerfClassifier
# Author: <NAME> <<EMAIL>>
# License: BSD 3 clause

# Turn down for faster convergence
train_samples = 5000

# Load data from https://www.openml.org/d/554
X, y = fetch_openml('mnist_784', version=1, return_X_y=True)
random_state = check_random_state(0)
permutation = random_state.permutation(X.shape[0])
X = X[permutation]
y = y[permutation]
X = X.reshape((X.shape[0], -1))

X_train, X_test, y_train, y_test = train_test_split(
    X, y, train_size=train_samples, test_size=10000)

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
clf_rerf = rerfClassifier(projection_matrix="Base", n_jobs=8, n_estimators=100)
clf_s_rerf = rerfClassifier(projection_matrix="S-RerF",
                             image_height=28,
                             image_width=28,
                             n_estimators=100,
                             patch_height_min=1,
                             patch_width_min=1,
                             patch_height_max=5,
                             patch_width_max=5,
                             n_jobs=8, 
                           )
print(clf_rerf)
print(clf_s_rerf)
t0 = time.time()
clf_rerf.fit(X_train, y_train)
run_time = time.time() - t0
print("run time rerf", run_time)
t0 = time.time()
clf_s_rerf.fit(X_train, y_train)
run_time = time.time() - t0
print("run time s_rerf", run_time)
train_acc_rerf = clf_rerf.score(X_train, y_train.astype(int))
print("train_acc rerf", train_acc_rerf)

train_acc_s_rerf = clf_s_rerf.score(X_train, y_train.astype(int))
print("train_acc s_rerf", train_acc_s_rerf)
# sparsity = np.mean(clf.coef_ == 0) * 100
score = clf_rerf.score(X_test, y_test.astype(int))
print("score rerf", score)

# sparsity = np.mean(clf.coef_ == 0) * 100
score = clf_s_rerf.score(X_test, y_test.astype(int))
print("score s_rerf", score)<jupyter_output>score rerf 0.9361
score s_rerf 0.9427
{'fraction_non_alphanumeric': 0.1047142261159783, 'fraction_numerical': 0.024614100959532748, 'mean_word_length': 3.602687140115163, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, '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': '530161', 'license': 'Apache-2.0', 'n_tokens_mistral': 931, 'n_tokens_neox': 883, 'n_words': 161, 'path': 'docs/demos/S-RerF/MNIST_classification_using_structured_RerF.ipynb', 'repo_name': 'cshih14/SPORF', 'size': '2418'}