-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCA.py
More file actions
27 lines (18 loc) · 665 Bytes
/
PCA.py
File metadata and controls
27 lines (18 loc) · 665 Bytes
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
'''Principal Component Analysis'''
from sklearn.decomposition import PCA
# --- Load instance of PCA transformation ---
pca = PCA()
# --- Load 1 component ---
pca = PCA(n_components=1)
# --- Fit the instance ---
pca.fit(<data>)
# --- Display principal components ---
pca.components_
# --- Generate new features ---
PC = pca.transform(X_scaled)
# --- Display explained variance ratio ---
pca.explained_variance_ratio_
# --- Cumulative explained variance ---
cumulative_explained_variance = np.cumsum(pca.explained_variance_ratio_)
# --- Plot cumulative explained variance ---
plt.plot(range(len(cumulative_explained_variance)), cumulative_explained_variance)