Created
June 10, 2021 22:46
-
-
Save alik604/609d5ef29dbfffa96430991029a2b76c to your computer and use it in GitHub Desktop.
PCA vs IncrementalPCA
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 numpy as np | |
import matplotlib.pyplot as plt | |
from sklearn.datasets import load_digits | |
from sklearn.decomposition import PCA, IncrementalPCA | |
iris = load_iris() | |
X = iris.data | |
y = iris.target | |
n_components = 2 | |
ipca = IncrementalPCA(n_components=n_components, batch_size=3) | |
X_ipca = ipca.fit_transform(X) | |
pca = PCA(n_components=n_components) | |
X_pca = pca.fit_transform(X) | |
rev_ipca = ipca.inverse_transform(X_ipca) | |
rev_pca = pca.inverse_transform(X_pca) | |
print(X.shape) | |
print("icrementalPCA.inv(x) MAE with X: ", np.abs(rev_ipca - X).mean()) | |
print("PCA.inv(x) MAE with X: ", np.abs(rev_pca - X).mean()) | |
# icrementalPCA.inv(x) MAE with X: 2.4990431209038713 | |
# PCA.inv(x) MAE with X: 2.479100754678159 | |
colors = ['navy', 'turquoise', 'darkorange'] | |
for X_transformed, title in [(X_ipca, "Incremental PCA"), (X_pca, "PCA")]: | |
plt.figure(figsize=(8, 8)) | |
for color, i, target_name in zip(colors, [0, 1, 2], iris.target_names): | |
plt.scatter(X_transformed[y == i, 0], X_transformed[y == i, 1], | |
color=color, lw=2, label=target_name) | |
if "Incremental" in title: | |
err = np.abs(X_pca - X_ipca).mean() | |
plt.title(title + " of iris dataset\nMean absolute error " | |
"%.6f" % err) | |
else: | |
plt.title(title + " of iris dataset") | |
plt.legend(loc="best", shadow=False, scatterpoints=1) | |
plt.axis([-4, 4, -1.5, 1.5]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment