BaggingClassifier#
- class sklearn.ensemble.BaggingClassifier(estimator=None, n_estimators=10, *, max_samples=1.0, max_features=1.0, bootstrap=True, bootstrap_features=False, oob_score=False, warm_start=False, n_jobs=None, random_state=None, verbose=0)#
一个Bagging分类器。
Bagging分类器是一个集成元估计器,它将基本分类器分别拟合到原始数据集的随机子集上,然后通过投票或平均的方式汇总它们的个体预测,形成最终预测。这样的元估计器通常可以用来减少黑箱估计器(例如,决策树)的方差,通过在其构造过程中引入随机化,然后从中形成一个集成。
该算法涵盖了文献中的几项工作。当数据集的随机子集作为样本的随机子集绘制时,该算法被称为Pasting [1]。如果样本是带替换绘制的,则该方法被称为Bagging [2]。当数据集的随机子集作为特征的随机子集绘制时,该方法被称为随机子空间 [3]。最后,当基本估计器建立在样本和特征的子集上时,该方法被称为随机补丁 [4]。
更多信息请参阅 用户指南 。
Added in version 0.15.
- Parameters:
- estimatorobject, default=None
用于拟合数据集随机子集的基本估计器。 如果为None,则基本估计器是
DecisionTreeClassifier
。Added in version 1.2:
base_estimator
被重命名为estimator
。- n_estimatorsint, default=10
集成中基本估计器的数量。
- max_samplesint or float, default=1.0
从X中抽取的样本数量,用于训练每个基本估计器(默认情况下带替换,详见
bootstrap
)。如果为int,则抽取
max_samples
个样本。如果为float,则抽取
max_samples * X.shape[0]
个样本。
- max_featuresint or float, default=1.0
从X中抽取的特征数量,用于训练每个基本估计器(默认情况下不带替换,详见
bootstrap_features
)。如果为int,则抽取
max_features
个特征。如果为float,则抽取
max(1, int(max_features * n_features_in_))
个特征。
- bootstrapbool, default=True
是否带替换抽取样本。如果为False,则进行不带替换的抽样。
- bootstrap_featuresbool, default=False
是否带替换抽取特征。
- oob_scorebool, default=False
是否使用袋外样本来估计泛化误差。仅在
bootstrap=True
时可用。- warm_startbool, default=False
当设置为True时,重用上一次调用fit的解决方案,并在集成中添加更多估计器,否则,拟合一个全新的集成。详见 术语表 。
Added in version 0.17: warm_start 构造函数参数。
- n_jobsint, default=None
用于并行运行
fit
和predict
的作业数量。None
表示1,除非在joblib.parallel_backend
上下文中。-1
表示使用所有处理器。详见 术语表 。- random_stateint, RandomState instance or None, default=None
控制原始数据集的随机重采样(样本和特征方面)。 如果基本估计器接受
random_state
属性,则为集成中的每个实例生成不同的种子。 传递一个int以在多次函数调用中获得可重复的输出。详见 术语表 。- verboseint, default=0
拟合和预测时的详细程度控制。
- Attributes:
- estimator_estimator
从该集成中生长的基本估计器。
Added in version 1.2:
base_estimator_
被重命名为estimator_
。- n_features_in_int
在 fit 期间看到的特征数量。
Added in version 0.24.
- feature_names_in_ndarray of shape (
n_features_in_
,) 在 fit 期间看到的特征名称。仅当
X
的特征名称均为字符串时定义。Added in version 1.0.
- estimators_list of estimators
拟合的基本估计器集合。
estimators_samples_
list of arrays子集,每个基估计器绘制的样本。
- estimators_features_list of arrays
每个基本估计器的抽取特征子集。
- classes_ndarray of shape (n_classes,)
类别标签。
- n_classes_int or list
类别数量。
- oob_score_float
使用袋外估计获得的训练数据集得分。仅在
oob_score
为True时存在此属性。- oob_decision_function_ndarray of shape (n_samples, n_classes)
在训练集上使用袋外估计计算的决策函数。如果n_estimators很小,可能会有数据点从未在bootstrap过程中被遗漏。在这种情况下,
oob_decision_function_
可能包含NaN。仅在oob_score
为True时存在此属性。
See also
BaggingRegressor
一个Bagging回归器。
References
[1]L. Breiman, “Pasting small votes for classification in large databases and on-line”, Machine Learning, 36(1), 85-103, 1999.
[2]L. Breiman, “Bagging predictors”, Machine Learning, 24(2), 123-140, 1996.
[3]T. Ho, “The random subspace method for constructing decision forests”, Pattern Analysis and Machine Intelligence, 20(8), 832-844, 1998.
[4]G. Louppe and P. Geurts, “Ensembles on Random Patches”, Machine Learning and Knowledge Discovery in Databases, 346-361, 2012.
Examples
>>> from sklearn.svm import SVC >>> from sklearn.ensemble import BaggingClassifier >>> from sklearn.datasets import make_classification >>> X, y = make_classification(n_samples=100, n_features=4, ... n_informative=2, n_redundant=0, ... random_state=0, shuffle=False) >>> clf = BaggingClassifier(estimator=SVC(), ... n_estimators=10, random_state=0).fit(X, y) >>> clf.predict([[0, 0, 0, 0]]) array([1])
- decision_function(X)#
平均基分类器决策函数的平均值。
- Parameters:
- X{array-like, sparse matrix},形状为 (n_samples, n_features)
训练输入样本。仅当基估计器支持时,才接受稀疏矩阵。
- Returns:
- scorendarray,形状为 (n_samples, k)
输入样本的决策函数。列对应于按属性
classes_
中出现的顺序排序的类。回归和二分类是特殊情况,其中k == 1
,否则k==n_classes
。
- property estimators_samples_#
子集,每个基估计器绘制的样本。
返回一个动态生成的索引列表,标识用于拟合集成中每个成员的样本,即,袋内样本。
注意:为了减少对象的内存占用,不在存储采样数据,每次调用该属性时都会重新创建列表。因此,获取该属性可能比预期的要慢。
- fit(X, y, *, sample_weight=None, **fit_params)#
构建一个从训练集(X,y)中估计器的Bagging集成。
- Parameters:
- X{array-like, sparse matrix},形状为 (n_samples, n_features)
训练输入样本。仅当基础估计器支持时,才接受稀疏矩阵。
- yarray-like,形状为 (n_samples,)
目标值(分类中的类标签,回归中的实数)。
- sample_weightarray-like,形状为 (n_samples,),默认=None
样本权重。如果为None,则样本等权重。 注意,这仅在基础估计器支持样本权重时才受支持。
- **fit_paramsdict
传递给基础估计器的参数。
Added in version 1.5: 仅在
enable_metadata_routing=True
时可用, 可以通过使用sklearn.set_config(enable_metadata_routing=True)
设置。 有关更多详细信息,请参阅 Metadata Routing User Guide 。
- Returns:
- selfobject
拟合的估计器。
- get_metadata_routing()#
获取此对象的元数据路由。
请查看 用户指南 以了解路由机制的工作原理。
Added in version 1.5.
- Returns:
- routingMetadataRouter
MetadataRouter
封装的 路由信息。
- get_params(deep=True)#
获取此估计器的参数。
- Parameters:
- deepbool, 默认=True
如果为True,将返回此估计器和包含的子对象(也是估计器)的参数。
- Returns:
- paramsdict
参数名称映射到它们的值。
- predict(X)#
预测X的类别。
输入样本的预测类别是计算为具有最高平均预测概率的类别。如果基础估计器没有实现
predict_proba
方法,则它会采用投票方式。- Parameters:
- X{array-like, sparse matrix},形状为(n_samples, n_features)
训练输入样本。仅当基础估计器支持时,才接受稀疏矩阵。
- Returns:
- yndarray,形状为(n_samples,)
预测的类别。
- predict_log_proba(X)#
预测X的类别对数概率。
输入样本的预测类别对数概率是通过计算集成中基本估计器预测类别概率的对数平均值来获得的。
- Parameters:
- X{array-like, sparse matrix},形状为 (n_samples, n_features)
训练输入样本。仅当基本估计器支持时,才接受稀疏矩阵。
- Returns:
- pndarray,形状为 (n_samples, n_classes)
输入样本的类别对数概率。类别的顺序与属性 classes_ 中的顺序相对应。
- predict_proba(X)#
预测X的类别概率。
输入样本的预测类别概率是通过集成中基本估计器的平均预测类别概率计算的。如果基本估计器没有实现
predict_proba
方法,则它会采用投票方式,输入样本的预测类别概率表示预测每个类别的估计器比例。- Parameters:
- X{array-like, sparse matrix},形状为 (n_samples, n_features)
训练输入样本。仅当基本估计器支持时,才接受稀疏矩阵。
- Returns:
- pndarray,形状为 (n_samples, n_classes)
输入样本的类别概率。类别的顺序与属性 classes_ 中的顺序相对应。
- score(X, y, sample_weight=None)#
返回给定测试数据和标签的平均准确率。
在多标签分类中,这是子集准确率,这是一个严格的指标,因为你要求每个样本的每个标签集都被正确预测。
- Parameters:
- X形状为 (n_samples, n_features) 的类数组
测试样本。
- y形状为 (n_samples,) 或 (n_samples, n_outputs) 的类数组
` X`的真实标签。
- sample_weight形状为 (n_samples,) 的类数组,默认=None
样本权重。
- Returns:
- scorefloat
self.predict(X)
相对于y
的平均准确率。
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') BaggingClassifier #
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it tofit
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter infit
.
- Returns:
- selfobject
The updated object.
- set_params(**params)#
设置此估计器的参数。
该方法适用于简单估计器以及嵌套对象(例如
Pipeline
)。后者具有形式为<component>__<parameter>
的参数,以便可以更新嵌套对象的每个组件。- Parameters:
- **paramsdict
估计器参数。
- Returns:
- selfestimator instance
估计器实例。
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') BaggingClassifier #
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter inscore
.
- Returns:
- selfobject
The updated object.