<jupyter_start><jupyter_text>PCA for data compressionWe will show an example of lossy image compression.<jupyter_code># Get digit images
import ssl
import tensorflow
from tensorflow.keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
original_shape=X_train.shape
original_shape
# Transform to 784 features (feature=pixel).
num_pixels = 784
X_train1D = X_train.reshape(X_train.shape[0],num_pixels)
print(X_train1D.shape)
from sklearn.decomposition import PCA
# How many dimension is the right number?
pca = PCA()
pca.fit(X_train1D)
explained = pca.explained_variance_ratio_
explained[:5]
pca = PCA(n_components=0.95)
pca.fit(X_train1D)
explained = pca.explained_variance_ratio_
len(explained)
X_reduced1D = pca.fit_transform(X_train1D)
X_recovered1D = pca.inverse_transform(X_reduced1D)
X_recovered = X_recovered1D.reshape(original_shape)
import matplotlib as mpl
import matplotlib.pyplot as plt
sample_0 = X_train[0]
plt.imshow(sample_0,cmap="binary")
plt.show()
sample_0_recovered = X_recovered[0]
plt.imshow(sample_0_recovered,cmap="binary")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09758281110116383, 'fraction_numerical': 0.023276633840644583, 'mean_word_length': 8.163934426229508, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '405764', 'license': 'MIT', 'n_tokens_mistral': 447, 'n_tokens_neox': 430, 'n_words': 85, 'path': 'Geron/Geron_ch8_DimensionalityReduction/DimenReduc_02_Compression.ipynb', 'repo_name': 'ShepherdCode/ShepherdML', 'size': '1117'}