python - TypeError : Cannot clone object. 您应该提供 scikit-learn 估计器的实例而不是类

标签 python scikit-learn ensemble-learning

我正在尝试使用堆叠分类器以及随机森林、Boosting 和 SVM 的 3 个基础学习器以及逻辑回归的 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.

我将其应用于泰坦尼克号数据集,以利用我所掌握的所有算法的力量。 我以前从未使用过堆叠分类或回归,因此这是我第一次。

感谢和问候

最佳答案

@amiola pointed out ,您缺少 LogisticRegression 之后的括号,这将创建该类的新实例:

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

应该是

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

关于python - TypeError : Cannot clone object. 您应该提供 scikit-learn 估计器的实例而不是类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69894022/

相关文章:

python - GridSearchCV 不工作?

Python 随机森林 sklearn : stuck for a few hours, 是怎么回事?

python - scikit-learn/Gaussian Process 不是尺度不变的

python - 我们能否在不增加分类时间的情况下创建深度学习模型的集合?

python - 在 Django 中截断链接/<a> 标签?

python - 基数为 10 : '[' when doing a filter with request. 主体的文字 int() 无效

python - Pandas 按行中的值和其他列中的值在行之间进行比较

python - Python中阈值检测函数的实现

python - 属性错误: module "sklearn.utils" has no attribute "_joblib" when inheriting class `sklearn.ensemble.BaggingClassifier.`

python - 使用部分拟合的 sklearn 投票合奏