-
Notifications
You must be signed in to change notification settings - Fork 2
/
pca_features.py
25 lines (19 loc) · 887 Bytes
/
pca_features.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
import numpy as np
from sklearn.decomposition import PCA
class PCAAnalysis(object):
def pca_components(self, data, n_components):
mean, std = self.moments(data)
normalized_x = (data - mean) / std # You need to normalize your data first
pca_fit = PCA(n_components=n_components).fit(
normalized_x) # n_components is the components number after reduction
print_components = "components:{}, variance{}".format(pca_fit.components_.shape,
pca_fit.explained_variance_ratio_)
# print(print_components)
return pca_fit
def moments(self, data):
mean = np.mean(data, 0)
constant = 1e-10
std = np.std(data, 0) + constant
return mean, std
def transform_inputs(self, components, data):
return np.dot(data, components.T)