I have a problem, I'm trying to build my own class to put into a pipeline in python, but it doesn't work.
The problem I am trying to solve is a multiclass classification problem.
What I want to do this to add a step in the pipeline to detect and remove outliers. I found this detect and remove outliers in pipeline python which is very similar to what I did. This is my class:
from sklearn.neighbors import LocalOutlierFactor
from sklearn.base import BaseEstimator, TransformerMixin
import numpy as np
class OutlierExtraction(BaseEstimator, TransformerMixin):
def __init__(self, **kwargs ):
self.kwargs = kwargs
def transform(self, X, y):
"""
X should be of shape (n_samples, n_features)
y should be of shape (n_samples,)
"""
lof = LocalOutlierFactor(**self.kwargs)
lof.fit(X)
nof = lof.negative_outlier_factor_
return X[nof > np.quantile(nof, 0.95), :], y[nof > np.quantile(nof, 0.95)]
def fit(self, X, y = None):
return self
But i get this error in fit_transform return self.fit(X, y, **fit_params).transform(X) TypeError: transform() missing 1 required positional argument: 'y'
The following code is the code i use to call this class:
scaler = preprocessing.RobustScaler()
outlierExtractor = OutlierExtraction()
pca = PCA()
classfier = svm.SVC()
pipeline = [('scaler', scaler),
('outliers', outlierExtractor),
('reduce_dim', pca),
('classfier', classfier)]
pipe = Pipeline(pipeline)
params = {
'reduce_dim__n_components': [5, 15],
'classfier__kernel': ['rbf'],
'classfier__gamma': [0.1],
'classfier__C': [1],
'classfier__decision_function_shape':['ovo']}
my_scoring = 'f1_macro'
n_folds = 5
gscv = GridSearchCV(pipe, param_grid=params, scoring=my_scoring, n_jobs=-1, cv=n_folds, refit=True)
gscv.fit(train_x, train_y)