This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
U, s, Vt = LA.svd(face_data, full_matrices=False) | |
S = np.diag(s) | |
#U: 500 *500, S: 500 *500, Vt:500 *2914 | |
dim = 100 | |
US = U[:, 0:dim].dot(S[0:dim, 0:dim]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
truncated = faces.data[:500] # 500 *2914 Matrix | |
average_face = truncted.mean(axis=0) | |
face_data = truncted - average_face |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pca = PCA(2) | |
PC = pca.fit_transform(digits.data) | |
inversed = pca.inverse_transform(PC) | |
_, axes = plt.subplots(4, 10, figsize=(10, 4), | |
subplot_kw={'xticks':[], 'yticks':[]}, | |
gridspec_kw=dict(hspace=0.1, wspace=0.1)) | |
for i, ax in enumerate(axes.flat): | |
ax.imshow(inversed[i].reshape(8, 8), | |
cmap='binary', interpolation='nearest', clim=(0, 16)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_, axes = plt.subplots(2, 8, figsize=(8, 2), | |
subplot_kw={'xticks':[], 'yticks':[]}, | |
gridspec_kw=dict(hspace=0.1, wspace=0.1)) | |
for i, ax in enumerate(axes.flat): | |
ax.imshow(digits.data[i].reshape(8, 8), | |
cmap='binary', interpolation='nearest', clim=(0, 16)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import seaborn as sns | |
sns.set() | |
sns.despine(left=True) | |
fig, axes = plt.subplots(2, 2, figsize=(15, 10), gridspec_kw=dict(hspace=.4, wspace=.3)) | |
title_settings={'fontsize':16} | |
subtitles=['Inversed samples with {} components']*3 | |
# Plot Heatmap 1 | |
ax = sns.heatmap(digits.data, cbar=False, ax=axes[0, 0]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pca = PCA().fit(digits.data) # Blank inside the closed bracket | |
plt.plot(np.cumsum(pca.explained_variance_ratio_),'o-', c='#663399', alpha=.5) | |
plt.xlabel('Number of components') | |
plt.ylabel('Cumulative explained variance') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plt.plot(np.cumsum(pca.explained_variance_ratio_),'o-', c='#663399', alpha=.5) | |
plt.xlabel('Number of components') | |
plt.ylabel('Cumulative explained variance') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.datasets import load_digits | |
digits = load_digits() | |
pca = PCA(n_components=10).fit(digits.data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ax = plt.gca(); ax.set_xlabel('X'); ax.set_ylabel('Y') | |
plt.scatter(X[:, 0], X[:, 1], alpha=0.3, color="#191970") | |
plt.scatter(pca.mean_[0], pca.mean_[1], c='red', s=50) | |
plt.axis('equal') | |
for length, vector in zip(pca.explained_variance_, pca.components_): | |
dir_ = vector * 3 * np.sqrt(length) | |
start = pca.mean_; end = start + dir_ | |
arrowprops = dict(arrowstyle='->',linewidth=2, | |
shrinkA=0, shrinkB=0, color='red', alpha=0.5) |