I'm working on a financial project that requires to write my own transformer and estimator, so I have a scores_ method under my transformer and a residuals_ method under my estimator:
Eg:
class my_transformer(base.BaseEstimator, base.TransformerMixin):
def __init__(self, some_arg):
self.some_arg=some_arg
def fit(self, X, y=None):
return self
def scores_(self, X):
return somefunc(X, y, self.some_arg)
def transform(self, X):
scores=self.scores_(X)
return factorSelect(scores,X)
and
class my_estimator(base.BaseEstimator, base.RegressorMixin):
def __init__(self, some_arg):
self.some_arg=some_arg
def fit(self, X, y):
some_other_func(X,y,self.some_arg)
return self
def predict(self, X):
result=some_other_other_func(X)
return result
def residuals(self, X, y):
result=self.predict(X)
return result.sub(y,axis='index')
So now I setup my pipeline like this:
pipe=Pipeline([
('transformer', my_transformer(some_arg)),
('Estimator',my_estimator(some_arg))
])
This works well when doing fit and predict:
pipe.fit(X,y)
pipe.predict(X)
However, I need to get the scores and residuals for the following steps. I can only get access by:
pipe['transformer'].scores(X)
pipe['estimator'].residuals(Not_X,y)
Here, the .scores works well, but for .residuals, I have to put in Not_X=pipe['transformer'].fit_transform(X) instead of X..
This is troublesome and contradicts the purpose of using the pipeline... So, how should I do it with PipeLine? If PipeLine won't do this, any other suggestions???
Thank you!