I have a sklearn pipeline that uses custom column transformer, estimator and different lambda functions.
Because Pickle cannot serialize the lambda functions, I am using dill.
Here is the custom estimator I have:
class customOLS(BaseEstimator):
def __init__(self, ols):
self.estimator_ols = ols
def fit(self, X, y):
X = pd.DataFrame(X)
y = pd.DataFrame(y)
print('---- Training OLS')
self.estimator_ols = self.estimator_ols(y,X).fit()
#print('---- Training LR')
#self.estimator_lr = self.estimator_lr.fit(X,y)
return self
def get_estimators(self):
return self.estimator_ols #, self.estimator_lr
def predict_ols(self, X):
res = self.estimator_ols.predict(X)
return res
pipeline2 = Pipeline(
steps=[
('dropper', drop_cols),
('remover',feature_remover),
("preprocessor", preprocess_ppl),
("estimator", customOLS(sm.OLS))
]
)
This is how I serilize it (I have to use open() otherwise it gives unsupportedOperation read write):
with open('data/baseModel_LR.joblib',"wb") as f:
dill.dump(pipeline2, f)
But when I try to load the pickled object:
with open('data/baseModel_LR.joblib',"rb") as f:
model = dill.load(f)
model
I get this error related to custom estimator:
AttributeError: 'customOLS' object has no attribute 'ols'
