<jupyter_start><jupyter_text>Sample images and merge into dataset<jupyter_code>number_of_samples = 2000
np.random.seed(seed=1)
sample_index = np.random.choice(len(all_images),number_of_samples)
sample_images = np.array(all_images)[sample_index]

PCA_data = []
for i in sample_images:
    this_image = list(np.asarray(Image.open(data_root+i)).flatten())
    PCA_data.append(this_image)
PCA_data = np.array(PCA_data)


# Add randy's protrait to selected iamge
this_image = np.asarray(Image.open(data_root+"randy.jpg"))
from skimage.transform import resize
resized_image = resize(this_image, (218, 178, 3))* 255
plt.imshow((resized_image).astype(int),
              cmap = plt.cm.gray, interpolation='nearest',
              clim=(0, 255));
plt.xlabel("All channels", fontsize = 12)
plt.title('Original Image', fontsize = 13);
PCA_data[0] = list(resized_image.flatten())

# fit multiple pca with different percentage of explained Variance

pca6 = PCA(.6)
lower_dimensional_data = pca6.fit_transform(PCA_data)
n_components6 = len(pca6.explained_variance_ratio_)
approximation6 = pca6.inverse_transform(lower_dimensional_data)

pca8 = PCA(.8)
lower_dimensional_data = pca8.fit_transform(PCA_data)
n_components8 = len(pca8.explained_variance_ratio_)
approximation8 = pca8.inverse_transform(lower_dimensional_data)

pca95 = PCA(.95)
lower_dimensional_data = pca95.fit_transform(PCA_data)
n_components95 = len(pca95.explained_variance_ratio_)
approximation95 = pca95.inverse_transform(lower_dimensional_data)

pca99 = PCA(.99)
lower_dimensional_data = pca99.fit_transform(PCA_data)
n_components99 = len(pca99.explained_variance_ratio_)
approximation99 = pca99.inverse_transform(lower_dimensional_data)<jupyter_output><empty_output><jupyter_text>Plot the decay of the singular values<jupyter_code>plt.figure(figsize=(10,7))
plt.plot(pca8.singular_values_[:20])
plt.plot(pca8.singular_values_[:20] ,'bo')
plt.xlabel("Number of Singular Values")
plt.ylabel("Singular Values")
plt.xticks(range(20))
plt.title("Decay of the singular values",fontsize = 16)<jupyter_output><empty_output><jupyter_text>Visualize two sets of results<jupyter_code>plt.figure(figsize=(18,6));

# Original Image
plt.subplot(1, 4, 1);
plt.imshow(PCA_data[0].reshape((218, 178, 3)),
#               cmap = plt.cm.gray, interpolation='nearest',
              clim=(0, 255));
plt.xlabel("All channels", fontsize = 12)
plt.title('Original Image: Prof.Paffenroth', fontsize = 13);




plt.subplot(1, 4, 2);
plt.imshow(approximation8[0].reshape((218, 178, 3)).astype(int),
#               cmap = plt.cm.gray, interpolation='nearest',
              clim=(0, 255));
plt.xlabel(str(n_components8) + ' components', fontsize = 12)
plt.title('80% of Explained Variance', fontsize = 13);


plt.subplot(1, 4, 3);
plt.imshow(approximation95[0].reshape((218, 178, 3)).astype(int),
#               cmap = plt.cm.gray, interpolation='nearest',
              clim=(0, 255));
plt.xlabel(str(n_components95) + ' components', fontsize = 12)
plt.title('95% of Explained Variance', fontsize = 13);



plt.subplot(1, 4, 4);
plt.imshow(approximation99[0].reshape((218, 178, 3)).astype(int),
#               cmap = plt.cm.gray, interpolation='nearest',
              clim=(0, 255));
plt.xlabel(str(n_components99) + ' components', fontsize = 12)
plt.title('99% of Explained Variance', fontsize = 13);
plt.figure(figsize=(18,6));

# Original Image
plt.subplot(1, 4, 1);
plt.imshow(PCA_data[30].reshape((218, 178, 3)),
#               cmap = plt.cm.gray, interpolation='nearest',
              clim=(0, 255));
plt.xlabel("All channels", fontsize = 12)
plt.title('Original Image', fontsize = 13);



plt.subplot(1, 4, 2);
plt.imshow(approximation8[30].reshape((218, 178, 3)).astype(int),
#               cmap = plt.cm.gray, interpolation='nearest',
              clim=(0, 255));
plt.xlabel(str(n_components8) + ' components', fontsize = 12)
plt.title('80% of Explained Variance', fontsize = 13);



plt.subplot(1, 4, 3);
plt.imshow(approximation95[30].reshape((218, 178, 3)).astype(int),
#               cmap = plt.cm.gray, interpolation='nearest',
              clim=(0, 255));
plt.xlabel(str(n_components95) + ' components', fontsize = 12)
plt.title('95% of Explained Variance', fontsize = 13);



plt.subplot(1, 4, 4);
plt.imshow(approximation99[30].reshape((218, 178, 3)).astype(int),
#               cmap = plt.cm.gray, interpolation='nearest',
              clim=(0, 255));
plt.xlabel(str(n_components99) + ' components', fontsize = 12)
plt.title('99% of Explained Variance', fontsize = 13);<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
{'fraction_non_alphanumeric': 0.14895363151415675, 'fraction_numerical': 0.05970455478046779, 'mean_word_length': 4.79667063020214, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '617893', 'license': 'MIT', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1724, 'n_words': 349, 'path': '.ipynb_checkpoints/PCA_demo-checkpoint.ipynb', 'repo_name': 'Cuilie/PCA-on-CelebA', 'size': '4874'}