-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpca_impl.py
61 lines (49 loc) · 1.84 KB
/
pca_impl.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
"""Implementing PCA"""
import numpy as np
from scipy import linalg
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
data = np.random.rand(1000, 50)
print(data.shape)
# Column standardize the data
data_ = (data - np.mean(data, axis=0)) / np.std(data, axis=0)
########################################################
# Scratch PCA #
########################################################
# Covariance matrix
# covariance = 1/n-1 * summation((x-mu_x)*(y-mu_y)), as we column standardized
# cov becomes 1/n-1 * summation((x)*(y)) and var is 1/n-1 * summation((x^2))
covariance = (1 / (data_.shape[0] - 1)) * (np.matmul(data_.T, data))
# computing eigen values, eigen vectors
eigen_val, eigen_vect = linalg.eigh(covariance)
# Note: eigen values are in ascending order
eigen_val = eigen_val[::-1]
eigen_vect = np.fliplr(eigen_vect)
# reducing dimensions of the actual data
transformed_data = np.matmul(data_, eigen_vect)
exp_var = np.sum(eigen_val[:2]) / np.sum(eigen_val)
print(f"Explained variance Imp. PCA: {exp_var}")
plt.subplot(121)
sns.scatterplot(
x=transformed_data[:, 0], y=transformed_data[:, 1], label=exp_var
)
plt.title("Imp. PCA")
plt.xlabel("Dimension 1")
plt.ylabel("Dimension 2")
########################################################
# PCA #
########################################################
pca = PCA(n_components=50, random_state=0)
transformed_data = pca.fit_transform(data_)
exp_var = np.sum(pca.explained_variance_[:2]) / np.sum(pca.explained_variance_)
print(f"Explained variance Act. PCA: {exp_var}")
plt.subplot(122)
sns.scatterplot(
x=transformed_data[:, 0], y=transformed_data[:, 1], label=exp_var
)
plt.title("Act. PCA")
plt.xlabel("Dimension 1")
plt.ylabel("Dimension 2")
plt.legend()
plt.show()