【问题标题】:TypeError: Cannot clone object. You should provide an instance of scikit-learn estimator instead of a class类型错误:无法克隆对象。您应该提供一个 scikit-learn 估计器的实例而不是一个类
【发布时间】:2021-11-09 07:03:10
【问题描述】:

我正在尝试使用堆叠分类器以及 3 个随机森林基础学习器、提升和 SVM 以及 1 个逻辑回归元学习器。

但是我不断收到此错误消息。

from sklearn.ensemble import StackingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler


rf = RandomForestClassifier(n_estimators=100,random_state=100)
gb = GradientBoostingClassifier(n_estimators=100,random_state=100)
svm = make_pipeline(StandardScaler(), SVC(random_state=100))

estimators = [('RF', rf),
          ('GB', gb),
          ('SVM', svm)]

Model = StackingClassifier(estimators=estimators, final_estimator=LogisticRegression)

Model.fit(X_train,y_train).score(X_val,y_val)

但我不断收到此错误。

TypeError                                 Traceback (most recent call last)
<ipython-input-47-40186fb4189e> in <module>
----> 1 Model.fit(X_train,y_train).score(X_val,y_val)

~\anaconda3\lib\site-packages\sklearn\ensemble\_stacking.py in fit(self, X, y, sample_weight)
    437         self._le = LabelEncoder().fit(y)
    438         self.classes_ = self._le.classes_
--> 439         return super().fit(X, self._le.transform(y), sample_weight)
    440 
    441     @if_delegate_has_method(delegate='final_estimator_')

~\anaconda3\lib\site-packages\sklearn\ensemble\_stacking.py in fit(self, X, y, sample_weight)
    138         # 'drop' string.
    139         names, all_estimators = self._validate_estimators()
--> 140         self._validate_final_estimator()
    141 
    142         stack_method = [self.stack_method] * len(all_estimators)

~\anaconda3\lib\site-packages\sklearn\ensemble\_stacking.py in _validate_final_estimator(self)
    406 
    407     def _validate_final_estimator(self):
--> 408         self._clone_final_estimator(default=LogisticRegression())
    409         if not is_classifier(self.final_estimator_):
    410             raise ValueError(

~\anaconda3\lib\site-packages\sklearn\ensemble\_stacking.py in _clone_final_estimator(self, 
default)
     55     def _clone_final_estimator(self, default):
     56         if self.final_estimator is not None:
 --> 57             self.final_estimator_ = clone(self.final_estimator)
     58         else:
     59             self.final_estimator_ = clone(default)

~\anaconda3\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     61             extra_args = len(args) - len(all_args)
     62             if extra_args <= 0:
---> 63                 return f(*args, **kwargs)
     64 
     65             # extra_args > 0

~\anaconda3\lib\site-packages\sklearn\base.py in clone(estimator, safe)
     62             if isinstance(estimator, type):
     63                 raise TypeError("Cannot clone object. " +
 --> 64                                 "You should provide an instance of " +
     65                                 "scikit-learn estimator instead of a class.")
     66             else:

 TypeError: Cannot clone object. You should provide an instance of scikit-learn estimator 
 instead 
 of a class.

我将它应用到 Titanic 数据集上,以使用我可以使用的所有算法的强大功能。 我以前从未使用过堆叠分类或回归,因此这是我第一次使用。

感谢和问候

【问题讨论】:

  • X_trainy_trainX_valy_val 定义/设置在哪里?
  • 我猜你应该将LogisticRegression() 传递给final_estimator 而不是LogisticRegression
  • 这行得通。谢谢!

标签: python scikit-learn ensemble-learning


【解决方案1】:

作为@amiola pointed out,您缺少LogisticRegression 后面的括号,它将创建该类的新实例:

Model = StackingClassifier(estimators=estimators, final_estimator=LogisticRegression)

应该是

Model = StackingClassifier(estimators=estimators, final_estimator=LogisticRegression())
                                                                                    ^^

【讨论】:

    猜你喜欢
    • 2020-10-01
    • 2021-03-04
    • 2023-02-16
    • 2011-07-26
    • 2021-02-13
    • 2016-02-08
    • 2013-04-01
    • 1970-01-01
    • 2016-01-21
    相关资源
    最近更新 更多