-
Notifications
You must be signed in to change notification settings - Fork 89
/
pca.py
194 lines (172 loc) · 7.38 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""sklearn PCA applied as transformation."""
__author__ = ["prockenschaub", "fkiraly", "aiwalter"]
__all__ = ["PCATransformer"]
import pandas as pd
from sklearn.decomposition import PCA
from aeon.transformations.base import BaseTransformer
class PCATransformer(BaseTransformer):
"""Principal Components Analysis applied as transformer.
Provides a simple wrapper around ``sklearn.decomposition.PCA``.
Parameters
----------
n_components : int, float or 'mle', default=None
Number of components to keep.
if n_components is not set all components are kept::
n_components == min(n_samples, n_features)
If ``n_components == 'mle'`` and ``svd_solver == 'full'``, Minka's
MLE is used to guess the dimension. Use of ``n_components == 'mle'``
will interpret ``svd_solver == 'auto'`` as ``svd_solver == 'full'``.
If ``0 < n_components < 1`` and ``svd_solver == 'full'``, select the
number of components such that the amount of variance that needs to be
explained is greater than the percentage specified by n_components.
If ``svd_solver == 'arpack'``, the number of components must be
strictly less than the minimum of n_features and n_samples.
Hence, the None case results in::
n_components == min(n_samples, n_features) - 1
copy : bool, default=True
If False, data passed to fit are overwritten and running
fit(X).transform(X) will not yield the expected results,
use fit_transform(X) instead.
whiten : bool, default=False
When True (False by default) the `components_` vectors are multiplied
by the square root of n_samples and then divided by the singular values
to ensure uncorrelated outputs with unit component-wise variances.
Whitening will remove some information from the transformed signal
(the relative variance scales of the components) but can sometime
improve the predictive accuracy of the downstream estimators by
making their data respect some hard-wired assumptions.
svd_solver : {'auto', 'full', 'arpack', 'randomized'}, default='auto'
If auto :
The solver is selected by a default policy based on `X.shape` and
`n_components`: if the input data is larger than 500x500 and the
number of components to extract is lower than 80% of the smallest
dimension of the data, then the more efficient 'randomized'
method is enabled. Otherwise the exact full SVD is computed and
optionally truncated afterwards.
If full :
run exact full SVD calling the standard LAPACK solver via
`scipy.linalg.svd` and select the components by postprocessing
If arpack :
run SVD truncated to n_components calling ARPACK solver via
`scipy.sparse.linalg.svds`. It requires strictly
0 < n_components < min(X.shape)
If randomized :
run randomized SVD by the method of Halko et al.
tol : float, default=0.0
Tolerance for singular values computed by svd_solver == 'arpack'.
Must be of range [0.0, infinity).
iterated_power : int or 'auto', default='auto'
Number of iterations for the power method computed by
svd_solver == 'randomized'.
Must be of range [0, infinity).
n_oversamples : int, default=10
This parameter is only relevant when `svd_solver="randomized"`.
It corresponds to the additional number of random vectors to sample the
range of `X` so as to ensure proper conditioning. See
:func:`~sklearn.utils.extmath.randomized_svd` for more details.
power_iteration_normalizer : {'auto', 'QR', 'LU', 'none'}, default='auto'
Power iteration normalizer for randomized SVD solver.
Not used by ARPACK. See :func:`~sklearn.utils.extmath.randomized_svd`
for more details.
random_state : int, RandomState instance or None, default=None
Used when the 'arpack' or 'randomized' solvers are used. Pass an int
for reproducible results across multiple function calls.
Attributes
----------
pca_ : sklearn.decomposition.PCA
The fitted PCA object
Examples
--------
>>> # skip DOCTEST if Python < 3.8
>>> import sys, pytest
>>> if sys.version_info < (3, 8):
... pytest.skip("PCATransformer requires Python >= 3.8")
>>>
>>> from aeon.transformations.pca import PCATransformer
>>> from aeon.datasets import load_longley
>>> _, X = load_longley()
>>> transformer = PCATransformer(n_components=2)
>>> X_hat = transformer.fit_transform(X)
References
----------
# noqa: E501
.. [1] https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html
"""
_tags = {
"input_data_type": "Series",
# what is the abstract type of X: Series, or Panel
"output_data_type": "Series",
# what abstract type is returned: Primitives, Series, Panel
"instancewise": False, # is this an instance-wise transform?
"X_inner_type": "pd.DataFrame",
"y_inner_type": "None",
"univariate-only": False,
"fit_is_empty": False,
"capability:missing_values": False,
"python_version": ">=3.8",
}
def __init__(
self,
n_components=None,
copy=True,
whiten=False,
svd_solver="auto",
tol=0.0,
n_oversamples=10,
power_iteration_normalizer="auto",
iterated_power="auto",
random_state=None,
):
self.n_components = n_components
self.copy = copy
self.whiten = whiten
self.svd_solver = svd_solver
self.tol = tol
self.n_oversamples = n_oversamples
self.power_iteration_normalizer = power_iteration_normalizer
self.iterated_power = iterated_power
self.random_state = random_state
super().__init__()
def _fit(self, X, y=None):
"""Fit transformer to X and y.
private _fit containing the core logic, called from fit
Parameters
----------
X: data structure of type X_inner_type
if X_inner_type is list, _fit must support all types in it
Data to fit transform to
y : Ignored
Returns
-------
self: reference to self
"""
self.pca_ = PCA(
n_components=self.n_components,
copy=self.copy,
whiten=self.whiten,
svd_solver=self.svd_solver,
tol=self.tol,
n_oversamples=self.n_oversamples,
power_iteration_normalizer=self.power_iteration_normalizer,
iterated_power=self.iterated_power,
random_state=self.random_state,
)
self.pca_.fit(X=X)
return self
def _transform(self, X, y=None):
"""Transform X and return a transformed version.
private _transform containing core logic, called from transform
Parameters
----------
X: data structure of type X_inner_type
if X_inner_type is list, _transform must support all types in it
Data to be transformed
y : Ignored
Returns
-------
transformed version of X
"""
Xt = self.pca_.transform(X=X)
columns = [f"PC_{i}" for i in range(Xt.shape[1])]
Xt = pd.DataFrame(Xt, index=X.index, columns=columns)
return Xt