问题
I wrote an estimator that gets as parameters a model and model's kwargs, and initiate 2 models with this kwargs (for red wine and white wine), split the data to 2 populations, run the model on each and then combines the results. Unfourtunately, my code works well, but trying to implement GridSearch fails due to a failure in sanity check of the parameters of the clone.
class run_estimator (BaseEstimator, TransformerMixin):
def __init__(self, model=None, **kwargs):
self.model = model
self.model_args = kwargs
self.red_model = model()
self.white_model = model()
def fit (self, X, y):
self.red_model = self.red_model.set_params(**self.model_args)
self.white_model = self.white_model.set_params(**self.model_args)
X_red, y_red = splitter.transform(X, y, 0)
self.red_model.fit(X_red, y_red)
X_white, y_white = splitter.transform(X, y, 1)
self.white_model.fit(X_white, y_white)
return self
def predict(self, X, y=None):
X_red, y_red = splitter.transform(X, y, 0)
y_red_pred = pd.Series(self.red_model.predict(X_red),index=X_red.index)
X_white, y_white = splitter.transform(X, y, 1)
y_white_pred = pd.Series(self.white_model.predict(X_white),index=X_white.index)
y_pred = concator(y_red_pred, y_white_pred)
cm = confusion_matrix(y_true=y, y_pred=y_pred)
cm_plot(cm, self.white_model)
return y_pred
def get_params(self, deep=True):
return {'model': self.model,
"model_args": self.model_args,
"red_model": self.red_model,
"white_model": self.white_model}
def set_params(self, **parameters):
for parameter, value in parameters.items():
setattr(self, parameter, value)
return self
def score_cks(self,X, y):
return cohen_kappa_score(y, self.predict(X, y))
The code that works is:
model = run_estimator(LogisticRegression, multi_class='ovr')
model.fit(X_train, y_train)
my Error in GridSearch is:
RuntimeError('Cannot clone object %s, as the constructor '
'either does not set or modifies parameter %s'
Can someone help me to figure out what I did wrong? Thank you
回答1:
Your __init__
shouldn't set any instance attributes that aren't in the signature. You can make this work by setting red_model
and white_model
only at fit time.
来源:https://stackoverflow.com/questions/64945380/runtimeerror-cannot-clone-object-scikit-learn-custom-estimator