假设我有以下代码:
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)]