本文介绍了运行时错误:无法克隆对象:Scikit-Learn 自定义估算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个估计器,它获取模型和模型的 kwargs 作为参数,并用这个 kwargs(红葡萄酒和白葡萄酒)启动 2 个模型,将数据分成 2 个群体,在每个群体上运行模型,然后组合结果.不幸的是,我的代码运行良好,但由于克隆参数的完整性检查失败,尝试实现 GridSearch 失败.

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))

有效的代码是:

model = run_estimator(LogisticRegression, multi_class='ovr')
model.fit(X_train, y_train)

我在 GridSearch 中的错误是:

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

推荐答案

您的 __init__ 不应设置任何不在签名中的实例属性.您可以通过仅在适合时设置 red_modelwhite_model 来实现此目的.

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.

这篇关于运行时错误:无法克隆对象:Scikit-Learn 自定义估算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 19:07