-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathincreamental_pca.py
More file actions
62 lines (47 loc) · 1.64 KB
/
increamental_pca.py
File metadata and controls
62 lines (47 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA, IncrementalPCA
# Load iris dataset
iris = load_iris()
X = iris.data
y = iris.target
n_components = 2
# Define batch size
batch_size = 10
# Initialize IncrementalPCA without using the batch_size parameter
ipca = IncrementalPCA(n_components=n_components)
# Manually divide data into batches and fit incrementally
n_samples = X.shape[0]
n_batches = int(np.ceil(n_samples / batch_size))
# Fit in batches
for i in range(n_batches):
start_idx = i * batch_size
end_idx = min((i + 1) * batch_size, n_samples)
batch = X[start_idx:end_idx]
ipca.partial_fit(batch)
# Transform the entire dataset at once after fitting
X_ipca = ipca.transform(X)
# Standard PCA for comparison
pca = PCA(n_components=n_components)
X_pca = pca.fit_transform(X)
# Visualization
colors = ["navy", "turquoise", "darkorange"]
for X_transformed, title in [(X_ipca, "Incremental PCA (Manual Batching)"), (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(np.abs(X_pca) - np.abs(X_ipca)).mean()
plt.title(title + " of iris dataset\nMean absolute unsigned 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()