Reputation: 57
How can I build a sklearn pipeline to do the following?
What I have:
A, B = getAB(X_train)
X_train = transform(X_train)
model(A, B, X_train)
What I want:
pipe = Pipeline([
(‘ab’, getAB),
(‘tranf’, transform),
(‘net’, net)
]
pipe.fit(X_train, y_train)
Please help!
Upvotes: 2
Views: 2458
Reputation: 7765
Yes it is doable by writing a custom transformer that has a fit/transform function. This can be your class:
from sklearn.base import BaseEstimator, TransformerMixin
def getABTransformer(BaseEstimator, TransformerMixin):
def __init__(self): # no *args or **kargs
pass
def fit(self, X, y=None):
return self # nothing else to do
def transform(self, X, y=None):
return getAB(X)
Then you can create your ColumnTransformer
as following:
from sklearn.compose import ColumnTransformer
clm_pipe = ColumnTransformer([
(‘ab’, getABTransformer, np.arange(0, len(X_train)), # list of columns indices
(‘tranf’, transform, np.arange(0, len(X_train))), # list of columns indices
]
and a final pipeline with the model:
pipe = Pipeline([
(‘clm_pipe’, clm_pipe),
(‘net’, net)
]
You can read more about ColumnTransformer
Upvotes: 1