<jupyter_start><jupyter_text>LFW - EingenfacesLoading the Labeled Faces in the Wild dataset<jupyter_code>from sklearn.datasets import fetch_lfw_people
faces = fetch_lfw_people(min_faces_per_person=60)
print(faces.target_names)
print(faces.images.shape)
len(faces.data[0])<jupyter_output><empty_output><jupyter_text>Create a PCA with 150 components:<jupyter_code>from sklearn.decomposition import PCA
pca = PCA(300)
pca.fit(faces.data)
len(pca.components_[i])<jupyter_output><empty_output><jupyter_text>Visualize the images associated with the first principal components (the "eigenfaces")<jupyter_code>import matplotlib.pyplot as plt

fig, axes = plt.subplots(3, 8, figsize=(9, 4),
                         subplot_kw={'xticks':[], 'yticks':[]},
                         gridspec_kw=dict(hspace=0.1, wspace=0.1))
for i, ax in enumerate(axes.flat):
    ax.imshow(pca.components_[i].reshape(62, 47), cmap='bone')<jupyter_output><empty_output><jupyter_text>Cumulative variance of these componentsWe need 150 components account for over 90% of the variance<jupyter_code>import numpy as np

plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('number of components')
plt.ylabel('cumulative explained variance');<jupyter_output><empty_output><jupyter_text>Compare the input images with the images reconstructed from these 150 components<jupyter_code>components = pca.transform(faces.data)
projected = pca.inverse_transform(components)
# Plot the results
fig, ax = plt.subplots(2, 10, figsize=(10, 2.5),
                       subplot_kw={'xticks':[], 'yticks':[]},
                       gridspec_kw=dict(hspace=0.1, wspace=0.1))
for i in range(10):
    ax[0, i].imshow(faces.data[i].reshape(62, 47), cmap='binary_r')
    ax[1, i].imshow(projected[i].reshape(62, 47), cmap='binary_r')
    
ax[0, 0].set_ylabel('original\nimage')
ax[1, 0].set_ylabel('150-d\nreconstruction');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1331236897274633, 'fraction_numerical': 0.030922431865828093, 'mean_word_length': 5.582758620689655, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '290220', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 723, 'n_tokens_neox': 673, 'n_words': 127, 'path': 'notebooks/PCA/LFW - PCA.ipynb', 'repo_name': 'JPMagalhaesCESAR/MLIntro', 'size': '1908'}