-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCA.py
More file actions
129 lines (102 loc) · 2.82 KB
/
PCA.py
File metadata and controls
129 lines (102 loc) · 2.82 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""Importing dependencies"""
import numpy as np
import matplotlib.pyplot as plt
import plotly.express as px
import pandas as pd
class PCA():
"""
This class performs Principle Component Analysis (PCA) on scaled data"""
def __init__(self,X):
"""
Initialaze
"""
self.X = X
self.covariance_matrix = self.cov()
self.vals = self.eig()[0]
self.vecs = self.eig()[1]
self.projections = self.get_projections()
def get_vals(self):
"""
Returns:
Eigenvalues
"""
return self.vals
def get_vecs(self):
"""
Returns:
Eigenvectors
"""
return self.vecs
def cov(self):
"""
arr: self.X
Returns:
Covariance matrix
"""
self.covariance_matrix = np.dot(self.X.T, self.X) / (self.X.shape[0] - 1)
return self.covariance_matrix
def eig(self):
"""
arr:
self.covariance_matrix
returns:
sorted eigenvalues highest to lowest
sortrd eigenvectors by eigenvalues
"""
self.vals, self.vecs = np.linalg.eig(self.covariance_matrix)
# Sort eigenvectors and values by eigenvalues
order = np.argsort(self.vals)[::-1]
self.vals =self. vals[order]
self.vecs = self.vecs[:, order]
return self.vals, self.vecs
def scree_plot(self):
"""
arr:
self.vals
self.vecs
returns:
scree plot
"""
# Scree plot -> Kaiser rule:
plt.bar(np.arange(len(self.vals)) + 1, self.vals, label = 'PC')
plt.axhline(1, label = 'eigenvalues > 1')
plt.title("Kaiser rule")
plt.ylabel('Eigenvalues')
plt.xlabel('No. of components')
plt.legend()
plt.show()
def get_projections(self):
"""
arr:
self.X
self.vecs
self.vals
returns:
self.projections as pandas dataframe
"""
pca_dict= {}
for n_vec in range(len(self.vecs)):
pca_dict['PC{}'.format(n_vec + 1)] = np.dot(self.X,self.vecs[:, n_vec])
self.projections = pd.DataFrame(pca_dict)
return self.projections
def plot_PCA(self, x = 'PC1', y = 'PC2', color = 'PC3'):
"""
arr:
self.projections
returns:
PCA plot
"""
fig = px.scatter(self.projections, x = x, y=y, color = color)
fig.update_layout(
width = 600,
height = 600,
title = "Priciple Component Analysis"
)
fig.show()
def main():
"""
main function
"""
print('PCA module')
if __name__ == "__main__":
main()