-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCA.py
68 lines (51 loc) · 1.83 KB
/
PCA.py
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
63
64
65
66
67
68
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import numpy as np
seed = 9000
seeded_state = np.random.RandomState(seed=seed)
# Returns a random 150 points (x, y pairs) in a gaussian distribution,
# IE most of the points fall close to the average with a few outliers
rand_points = seeded_state.randn(150, 2)
points = rand_points @ seeded_state.rand(2, 2)
x = points[:, 0]
y = points[:, 1]
# Now we have a sample dataset of 150 points to perform PCA and display this in a plot.
plt.scatter(x, y, alpha=0.5)
plt.title("Sample Dataset")
print("Plotting our created dataset...\n")
print("Points:")
for p in points[:10, :]:
print("({:7.4f}, {:7.4f})".format(p[0], p[1]))
print("...\n")
plt.show()
# Find two principal components from our given dataset
pca = PCA(n_components = 2)
pca.fit(points)
# Once we are fitted, we have access to inner mean_, components_, and explained_variance_ variables
# add some arrows to plot
plt.scatter(x, y, alpha=0.5)
plt.title("Sample Dataset with Principal Component Lines")
for var, component in zip(pca.explained_variance_, pca.components_):
plt.annotate(
"",
component * np.sqrt(var) * 2 + pca.mean_,
pca.mean_,
arrowprops = {
"arrowstyle": "->",
"linewidth": 2
}
)
print("Plotting our calculated principal components...\n")
plt.show()
# Reduce the dimensionality of our data using a PCA transformation
pca = PCA(n_components = 1)
transformed_points = pca.fit_transform(points)
inverse = pca.inverse_transform(transformed_points)
t_x = inverse[:, 0]
t_y = inverse[:, 0]
# Plot the original and transformed data sets
plt.scatter(x, y, alpha=0.3)
plt.scatter(t_x, t_y, alpha=0.7)
plt.title("Sample Dataset (Blue) and Transformed Dataset (Orange)")
print("Plotting our dataset with a dimensionality reduction...")
plt.show()