BaggingClassifier 意外的关键字参数 'max_depth'

BaggingClassifier unexpected keyword argument 'max_depth'

我在 scikit-learn 0.22.2.post1 中遇到 BaggingClassifier 错误。我正在使用 python 3.8.2.

from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import BaggingClassifier
bag_clf = BaggingClassifier(
                            DecisionTreeClassifier(
                                                    random_state=0,
                                                    criterion='entropy'
                                                    ),
                              n_estimators=100,
                              max_samples=100,
                              max_depth=2,
                              bootstrap=True,
                              random_state=0
                              )

TypeError: init() got an unexpected keyword argument 'max_depth'

如果我从我的代码中删除 max_depth=2,我可以创建该对象。 max_depth=2 是我收到错误的唯一参数。

有人知道这是怎么回事吗?

max_depthDecisionTreeClassifier (docs), not of BaggingClassifier (docs) 的参数;您应该将定义更改为

bag_clf = BaggingClassifier(
                            DecisionTreeClassifier( max_depth=2,
                                                    random_state=0,
                                                    criterion='entropy'
                                                    ),
                              n_estimators=100,
                              max_samples=100,
                              bootstrap=True,
                              random_state=0
                              )