-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathpca.py
52 lines (36 loc) · 1.27 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
""" A class for PCA using pyDAAL """
__author__ = 'Zhang Zhang'
__email__ = '[email protected]'
import daal.algorithms.pca as pca
from daal.data_management import HomogenNumericTable
import numpy as np
class PCA:
def __init__(self, method = 'correlation'):
"""Initialize class parameters
Args:
method: The default method is based on correation matrix. It
can also be the SVD method ('svd')
"""
if method != 'correlation' and method != 'svd':
warnings.warn(method +
' method is not supported. Default method is used',
UserWarning)
self.method_ = method
self.eigenvalues_ = None
self.eigenvectors_ = None
def compute(self, data):
"""Compute PCA the input data
Args:
data: Input data
"""
# Create an algorithm object for PCA
if self.method_ == 'svd':
pca_alg = pca.Batch_Float64SvdDense()
else:
pca_alg = pca.Batch_Float64CorrelationDense()
# Set input
pca_alg.input.setDataset(pca.data, data)
# compute
result = pca_alg.compute()
self.eigenvalues_ = result.get(pca.eigenvalues)
self.eigenvectors_ = result.get(pca.eigenvectors)