访问BaggingClassifier中的每个估计器

假设我有以下代码:

model = BaggingClassifier(base_estimator=DecisionTreeClassifier(), n_estimators=31)

我想访问这31个估计器,但我不知道如何操作。我尝试过:

for _ in model.estimators_:    # 相关操作

但这会引发以下错误:

AttributeError: 'BaggingClassifier' object has no attribute 'estimators_'

然而,在BaggingClassifier指南中显示为

estimators_ : 估计器列表。拟合后的基础估计器集合。


回答:

属性estimators_需要模型先被拟合:

from sklearn.svm import SVCfrom sklearn.ensemble import BaggingClassifierfrom sklearn.datasets import make_classificationX, y = make_classification(n_samples=100, n_features=4,                            n_informative=2, n_redundant=0,                            random_state=0, shuffle=False)clf = BaggingClassifier(base_estimator=SVC(),                         n_estimators=3, random_state=0)clf.estimators_# AttributeError: 'BaggingClassifier' object has no attribute 'estimators_'clf.fit(X, y)clf.estimators_# result:[SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,     decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',     max_iter=-1, probability=False, random_state=2087557356, shrinking=True,     tol=0.001, verbose=False), SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,     decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',     max_iter=-1, probability=False, random_state=132990059, shrinking=True,     tol=0.001, verbose=False), SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,     decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',     max_iter=-1, probability=False, random_state=1109697837, shrinking=True,     tol=0.001, verbose=False)]

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注