Skip to content

Instantly share code, notes, and snippets.

@RyotaBannai
Last active December 19, 2018 18:01
Show Gist options
  • Save RyotaBannai/0932071948746a30635f4be794f4433b to your computer and use it in GitHub Desktop.
Save RyotaBannai/0932071948746a30635f4be794f4433b to your computer and use it in GitHub Desktop.
from sklearn.datasets import fetch_lfw_people
faces = fetch_lfw_people(min_faces_per_person=60)
face_data_raw = faces.data
average_face = face_data_raw.mean(axis=0)
face_data = face_data_raw - average_face
pca = PCA(100).fit(face_data)
PC = pca.transform(face_data)
inversed = pca.inverse_transform(PC)
inversed_faces = inversed + average_face
fig, ax = plt.subplots(2, 5, figsize=(5, 2.5),
subplot_kw={'xticks':[], 'yticks':[]},
gridspec_kw=dict(hspace=0.1, wspace=0.1))
for i in range(5, 10):
ax[0, i-5].imshow(faces.data[i].reshape(62, 47), cmap='binary_r')
ax[1, i-5].imshow(inversed_faces[i].reshape(62, 47), cmap='binary_r')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment