1
0
Fork 0
QuaPy/quapy/classification/methods.py

44 lines
1.4 KiB
Python
Raw Normal View History

2021-01-22 09:58:12 +01:00
from sklearn.base import BaseEstimator
from sklearn.decomposition import TruncatedSVD
from sklearn.linear_model import LogisticRegression
2021-01-22 09:58:12 +01:00
class PCALR(BaseEstimator):
"""
An example of a classification method that also generates embedded inputs, as those required for QuaNet.
This example simply combines a Principal Component Analysis (PCA) with Logistic Regression (LR).
"""
2021-01-22 09:58:12 +01:00
def __init__(self, n_components=100, **kwargs):
self.n_components = n_components
self.learner = LogisticRegression(**kwargs)
def get_params(self):
params = {'n_components': self.n_components}
params.update(self.learner.get_params())
return params
def set_params(self, **params):
if 'n_components' in params:
self.n_components = params['n_components']
del params['n_components']
self.learner.set_params(**params)
def fit(self, X, y):
2021-01-22 09:58:12 +01:00
self.learner.fit(X, y)
self.pca = TruncatedSVD(self.n_components).fit(X, y)
# embedded = self.pca.transform(X)
self.classes_ = self.learner.classes_
return self
def predict(self, X):
2021-01-22 09:58:12 +01:00
# X = self.transform(X)
return self.learner.predict(X)
def predict_proba(self, X):
2021-01-22 09:58:12 +01:00
# X = self.transform(X)
return self.learner.predict_proba(X)
def transform(self, X):
return self.pca.transform(X)