Compare commits
2 Commits
master
...
feature/so
Author | SHA1 | Date | |
---|---|---|---|
|
efda0cd115 | ||
|
bbf2e2ac67 |
22
pyFTS/common/transformations/pca.py
Normal file
22
pyFTS/common/transformations/pca.py
Normal file
@ -0,0 +1,22 @@
|
||||
from sklearn.decomposition import PCA
|
||||
from pyFTS.common.Transformations import Transformation
|
||||
import pandas as pd
|
||||
|
||||
class PCATransformation(Transformation):
|
||||
def __init__(self):
|
||||
self.pca = PCA(n_components=2)
|
||||
self.is_multivariate = True
|
||||
|
||||
|
||||
def apply(self, data, param=None, **kwargs):
|
||||
endogen_variable = kwargs.get('endogen_variable', None)
|
||||
names = kwargs.get('names', ('x', 'y'))
|
||||
if endogen_variable not in data.columns:
|
||||
endogen_variable = None
|
||||
cols = data.columns[:-1] if endogen_variable is None else [col for col in data.columns if
|
||||
col != endogen_variable]
|
||||
self.pca.fit(data[cols].values)
|
||||
transformed = self.pca.transform(data[cols])
|
||||
new = pd.DataFrame(transformed, columns=list(names))
|
||||
new[endogen_variable] = data[endogen_variable].values
|
||||
return new
|
Loading…
Reference in New Issue
Block a user