MultiOutputClassifier#
- class sklearn.multioutput.MultiOutputClassifier(estimator, *, n_jobs=None)#
多目标分类。
这种策略包括为每个目标拟合一个分类器。这是一种简单的策略,用于扩展本身不支持多目标分类的分类器。
- Parameters:
- estimatorestimator object
一个实现 fit 和 predict 方法的估计器对象。 只有当
estimator
实现时,才会公开 predict_proba 方法。- n_jobsint or None, 可选 (default=None)
并行运行的作业数。
fit
,predict
和partial_fit
(如果传递的估计器支持)将对每个目标并行化。当单个估计器训练或预测速度很快时,使用
n_jobs > 1
可能会由于并行开销而导致性能变慢。None
意味着1
,除非在joblib.parallel_backend
上下文中。-1
意味着使用所有可用的进程/线程。 有关更多详细信息,请参见 Glossary 。Changed in version 0.20:
n_jobs
默认值从1
改为None
。
- Attributes:
See also
ClassifierChain
一种多标签模型,将二元分类器排列成链。
MultiOutputRegressor
为每个目标变量拟合一个回归器。
Examples
>>> import numpy as np >>> from sklearn.datasets import make_multilabel_classification >>> from sklearn.multioutput import MultiOutputClassifier >>> from sklearn.linear_model import LogisticRegression >>> X, y = make_multilabel_classification(n_classes=3, random_state=0) >>> clf = MultiOutputClassifier(LogisticRegression()).fit(X, y) >>> clf.predict(X[-2:]) array([[1, 1, 1], [1, 0, 1]])
- fit(X, Y, sample_weight=None, **fit_params)#
拟合模型到数据矩阵X和目标Y。
- Parameters:
- X{array-like, sparse matrix},形状为 (n_samples, n_features)
输入数据。
- Yarray-like,形状为 (n_samples, n_classes)
目标值。
- sample_weightarray-like,形状为 (n_samples,),默认=None
样本权重。如果为
None
,则样本等权重。 仅在基础分类器支持样本权重时受支持。- **fit_paramsdict of string -> object
传递给每个步骤的
estimator.fit
方法的参数。Added in version 0.23.
- Returns:
- selfobject
返回一个已拟合的实例。
- get_metadata_routing()#
获取此对象的元数据路由。
请查看 用户指南 以了解路由机制的工作原理。
Added in version 1.3.
- Returns:
- routingMetadataRouter
MetadataRouter
封装的 路由信息。
- get_params(deep=True)#
获取此估计器的参数。
- Parameters:
- deepbool, 默认=True
如果为True,将返回此估计器和包含的子对象(也是估计器)的参数。
- Returns:
- paramsdict
参数名称映射到它们的值。
- partial_fit(X, y, classes=None, sample_weight=None, **partial_fit_params)#
逐步为每个类别输出拟合一个单独的模型。
- Parameters:
- X{array-like, sparse matrix},形状为 (n_samples, n_features)
输入数据。
- y{array-like, sparse matrix},形状为 (n_samples, n_outputs)
多输出目标。
- classes形状为 (n_outputs,) 的 ndarray 列表,默认=None
每个数组是字符串/整数中一个输出的唯一类别。 可以通过以下方式获得:
[np.unique(y[:, i]) for i in range(y.shape[1])]
,其中y
是整个数据集的目标矩阵。 此参数在第一次调用 partial_fit 时是必需的,在后续调用中可以省略。 注意,y
不需要包含classes
中的所有标签。- sample_weight形状为 (n_samples,) 的 array-like,默认=None
样本权重。如果为
None
,则样本等权重。 仅在基础回归器支持样本权重时才支持。- **partial_fit_params字符串 -> 对象的字典
传递给每个子估计器的
estimator.partial_fit
方法的参数。仅在
enable_metadata_routing=True
时可用。请参阅 用户指南 。Added in version 1.3.
- Returns:
- selfobject
返回一个已拟合的实例。
- predict(X)#
使用每个目标变量的模型预测多输出变量。
- Parameters:
- X{array-like, sparse matrix},形状为 (n_samples, n_features)
输入数据。
- Returns:
- y{array-like, sparse matrix},形状为 (n_samples, n_outputs)
跨多个预测器预测的多输出目标。 注意:每个预测器生成单独的模型。
- predict_proba(X)#
返回每个输出类别对应的预测概率。
如果任何一个估计器没有
predict_proba
方法,此方法将引发ValueError
。- Parameters:
- X形状为 (n_samples, n_features) 的类数组
输入数据。
- Returns:
- p形状为 (n_samples, n_classes) 的数组,或者当 n_outputs > 1 时为这样的数组列表
输入样本的类别概率。类别的顺序与属性 classes_ 中的顺序相对应。
Changed in version 0.19: 此函数现在返回一个数组列表,列表的长度为
n_outputs
,每个数组为 (n_samples
,n_classes
),对应于特定的输出。
- score(X, y)#
返回给定测试数据和标签的平均准确率。
- Parameters:
- X形状为 (n_samples, n_features) 的类数组
测试样本。
- y形状为 (n_samples, n_outputs) 的类数组
X 的真实值。
- Returns:
- scoresfloat
预测目标与真实目标的平均准确率。
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MultiOutputClassifier #
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_partial_fit_request(*, classes: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') MultiOutputClassifier #
Request metadata passed to the
partial_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 topartial_fit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it topartial_fit
.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:
- classesstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
classes
parameter inpartial_fit
.- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter inpartial_fit
.
- Returns:
- selfobject
The updated object.