<jupyter_start><jupyter_text><jupyter_code>import numpy as np
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
data = fetch_openml('mnist_784',version=1)
X_train,X_test, y_train, y_test = train_test_split(data['data'],data['target'],test_size=10000,random_state =42)
import timeit
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier()

#t = timeit.Timer(setup="clf",stmt="RandomForestClassifier.fit(X_train,y_train)")

%%timeit
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
accuracy_score(y_pred,y_test)
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline

pca = PCA(n_components=0.95)
X_train = pca.fit_transform(X_train)
X_test = pca.fit_transform(X_test)
len(X_train)
%%timeit
clf.fit(X_train,y_train)

pipe = Pipeline([
                 ("pca",PCA(n_components=0.95)),
                 ("clf",RandomForestClassifier())
])

pipe.fit(X_train,y_train)
import time
t0 = time.time()
pipe.fit(X_train,y_train)
t1 = time.time()
print("traninig took {: .2f}s".format(t1-t0))
from sklearn.decomposition import PCA
pca = PCA(n_components=154)
X_test = pca.fit_transform(X_test)
y_pred2 = pipe.predict(X_test)

accuracy_score(y_pred2,y_test)<jupyter_output><empty_output><jupyter_text>Using TSNE<jupyter_code># Generate a random subset of 10000 training set from the trainning set loaded above
np.random.seed(42)
random_index = np.random.randint(0,60000,10000,dtype=int)
#Reload the trainning set
X_train,X_test, y_train, y_test = train_test_split(data['data'],data['target'],test_size=10000,random_state =42)
X_train = X_train[random_index,:]
y=y_train[random_index]
from sklearn.manifold import TSNE

tsne = TSNE(n_components=2,random_state=42)
X_reduced = tsne.fit_transform(X_train)<jupyter_output><empty_output><jupyter_text>Matplotlib didn't accept y for being an array of strings. Therefore the conversion to int<jupyter_code>y_int =[]
for i in y:
    y_int.append(int(i))
import matplotlib.pyplot as plt

plt.figure(figsize=(22,12))
plt.scatter(X_reduced[:,0],X_reduced[:,1],c=y_int,cmap="jet")
plt.colorbar()
plt.axis("off")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12539467749210645, 'fraction_numerical': 0.02751465944970681, 'mean_word_length': 7.732283464566929, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '572028', 'license': 'Unlicense', 'n_tokens_mistral': 900, 'n_tokens_neox': 849, 'n_words': 147, 'path': 'Answer_to_Dimensionality_Reduction_Questions.ipynb', 'repo_name': 'guruboy001/machine_learning_codes', 'size': '2217'}