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_raw[:, 0], X_raw[:, 1], c='#B8860B', alpha=0.5) | |
| plt.scatter(X_mean[0], X_mean[1], c='red', s=50) | |
| plt.axis('equal') | |
| for length, vector in zip(e_values, V): | |
| dir_ = -vector * 3 * np.sqrt(length) # Tweak the sign | |
| start = X_mean; end = start + dir_ | |
| arrowprops = dict(arrowstyle='->',linewidth=2, |
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 matplotlib.patches import Ellipse | |
| import matplotlib as mpl | |
| ax = plt.gca(); ax.set_xlabel('X'); ax.set_ylabel('Y') | |
| plt.scatter(X_raw[:, 0], X_raw[:, 1], c='#B8860B', alpha=0.5) | |
| plt.scatter(X_mean[0], X_mean[1], c='red', s=50) | |
| U_, s_, _ = LA.svd(np.cov(X, rowvar=False), full_matrices=False) | |
| angle = np.degrees(np.arctan2(U_[1,0], U_[0,0])) | |
| width, height = 2 * np.sqrt(s_) |
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.decomposition import PCA | |
| pca = PCA(n_components=2) | |
| pca.fit(X) # Apply PCA |
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) |
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
| 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
| 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
| 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
| _, 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
| 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)) |