<jupyter_start><jupyter_text>Mnist Dataset with PCA<jupyter_code>import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
from sklearn import metrics, datasets
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression<jupyter_output><empty_output><jupyter_text>Dataset and Parameters<jupyter_code>data = datasets.fetch_openml('mnist_784')
X = data.data
y = data.target
X.shape, y.shape
X.head(2)
X.iloc[:,0].describe()
28 * 28 # width and height
random_state = 123
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=random_state)
X_train.shape, X_test.shape, y_train.shape, y_test.shape<jupyter_output><empty_output><jupyter_text>PCA Requirements<jupyter_code># Convert data into unit scale 
scaler = StandardScaler()
X_tr  = scaler.fit_transform(X_train, y_train)
X_tr.shape
X_tr[:3]<jupyter_output><empty_output><jupyter_text>Dimensionality Reduction with PCA<jupyter_code># pca will retain % 95 variance of the original dataset in principal components.
pca = PCA(n_components=0.95, random_state=random_state) 
X_new = pca.fit_transform(X_tr)

# pca will retain % 70 variance of the original dataset in principal components.
pca_70 = PCA(n_components=0.70, random_state=random_state) 
X_new_70 = pca_70.fit_transform(X_tr)

# pca will retain % 100 variance of the original dataset in principal components.
pca_100 = PCA(random_state=random_state) 
X_new_100 = pca_100.fit_transform(X_tr)
pca.n_components_, pca_70.n_components_, pca_100.n_components_
sum(pca.explained_variance_ratio_), len(pca.explained_variance_ratio_)
plt.figure(figsize=(10, 5))
plt.step(range(pca_100.n_components_), np.cumsum(pca_100.explained_variance_ratio_ *
         100), where='mid', label='Cumulative Explained Variance')
plt.axhline(y=95, color='g', linestyle='--', label='% 95 Explained Variance')
plt.axhline(y=80, color='c', linestyle='--', label='% 80 Explained Variance')
plt.axhline(y=70, color='r', linestyle='--', label='% 70 Explained Variance')
plt.title('Cumulative Explained Variance as a Function of the Number of Components')
plt.legend(loc='best')
plt.show()
print('Data shapes before and after transformation with PCA: ', X_train.shape, X_new.shape)
print('Avg features shape: ', pca.components_.shape, pca.components_.reshape((pca.n_components_, 28, 28)).shape)
features_average = pca.components_.reshape((pca.n_components_, 28, 28))
plt.imshow(features_average[4])
plt.title('Eigenvalue for PC 4')
plt.show()
image_no = 100
image_x_train = X_train.values[image_no].reshape(1, -1).reshape(28, 28)
image_x_new = pca.inverse_transform(X_new[image_no]).reshape(28, 28)
image_x_new_70 = pca_70.inverse_transform(X_new_70[image_no]).reshape(28, 28)

plt.figure(figsize=(20, 4))
# Original Image (784 components)
ax = plt.subplot(1, 5, 1)
plt.imshow(image_x_train)
plt.title('%100')
# Transformed image with PCA (%95 of variance retained)
ax = plt.subplot(1, 5, 2)
plt.imshow(image_x_new)
plt.title('%95 Variance Retained')
# Transformed image with PCA (%70 of variance retained)
ax = plt.subplot(1, 5, 3)
plt.imshow(image_x_new_70)
plt.title('%70 Variance Retained')
plt.show()<jupyter_output><empty_output><jupyter_text>Building Clasifier for Mnist Dataset with Transformed Data<jupyter_code>clf = LogisticRegression(solver='saga')
clf.fit(X_new_70, y_train)
clf.score(X_new_70, y_train)
y_pred = clf.predict(X_new_70)
sample = X_new_70[0].reshape(1, -1)
print(X_new_70[0].shape, sample.shape)
clf.predict(sample), y_train[0]
print(metrics.classification_report(y_train, y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.95      0.96      0.96      6214
           1       0.93      0.97      0.95      7044
           2       0.92      0.89      0.90      6311
           3       0.90      0.88      0.89      6432
           4       0.91      0.93      0.92      6161
           5       0.88      0.86      0.87      5652
           6       0.94      0.95      0.94      6197
           7       0.92      0.93      0.92      6573
           8       0.88      0.85      0.87      6133
           9       0.89      0.89      0.89      6283

    accuracy                           0.91     63000
   macro avg       0.91      0.91      0.91     63000
weighted avg       0.91      0.91      0.91     63000
{'fraction_non_alphanumeric': 0.1104572204617474, 'fraction_numerical': 0.07356269805341784, 'mean_word_length': 3.676190476190476, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '551738', 'license': 'MIT', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1599, 'n_words': 355, 'path': 'contents/pca/Digits.ipynb', 'repo_name': 'metinsenturk/class-materials', 'size': '4418'}