<jupyter_start><jupyter_text>8<jupyter_code>import numpy as np
from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('MNIST original')
X = mnist['data'].astype(np.float32)
y = mnist['target'].astype(np.int32)
from sklearn.decomposition import PCA
pca = PCA(n_components=0.7)
X_trans = pca.fit_transform(X)
train = 4000
val = 5000
test = 6000
m, n = X_trans.shape
np.random.seed(42)
shuffle_idx = np.random.permutation(m)
X_trans = X_trans[shuffle_idx]
y = y[shuffle_idx]
X_train, y_train = X_trans[:train], y[:train]
X_val, y_val = X_trans[train:val], y[train:val]
X_test, y_test = X_trans[val:test], y[val:test]
from sklearn.ensemble import VotingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
rnd_clf = RandomForestClassifier(max_depth=2, n_estimators=400)
ext_clf = ExtraTreesClassifier(max_depth=2, n_estimators=500)
svm_clf = SVC()
vote_clf = VotingClassifier(
    estimators=[('rnd_clf', rnd_clf), ('ext_clf', ext_clf), ('svm_clf', svm_clf)],
    voting='hard'
)
for clf in (rnd_clf, ext_clf, svm_clf, vote_clf):
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_val)
    print(clf.__class__.__name__, accuracy_score(y_pred, y_val))<jupyter_output>RandomForestClassifier 0.684
ExtraTreesClassifier 0.717
SVC 0.12
VotingClassifier 0.629
{'fraction_non_alphanumeric': 0.11692969870875179, 'fraction_numerical': 0.03156384505021521, 'mean_word_length': 6.926136363636363, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '64538', 'license': 'MIT', 'n_tokens_mistral': 604, 'n_tokens_neox': 572, 'n_words': 109, 'path': 'Hand-on-Machine-Learning-with-Scikit-learning-and-Tensorflow/Chapter07/Exercise.ipynb', 'repo_name': 'gaufung/CodeBase', 'size': '1394'}