MultiOutputRegressor#
- class sklearn.multioutput.MultiOutputRegressor(estimator, *, n_jobs=None)#
多目标回归。
这种策略包括为每个目标拟合一个回归器。这是一种简单的策略,用于扩展本身不支持多目标回归的回归器。
Added in version 0.18.
- Parameters:
- estimatorestimator object
- n_jobsint or None, optional (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
RegressorChain
一种多标签模型,将回归排列成链。
MultiOutputClassifier
独立分类每个输出,而不是链式处理。
Examples
>>> import numpy as np >>> from sklearn.datasets import load_linnerud >>> from sklearn.multioutput import MultiOutputRegressor >>> from sklearn.linear_model import Ridge >>> X, y = load_linnerud(return_X_y=True) >>> regr = MultiOutputRegressor(Ridge(random_state=123)).fit(X, y) >>> regr.predict(X[[0]]) array([[176..., 35..., 57...]])
- fit(X, y, sample_weight=None, **fit_params)#
拟合模型到数据,分别针对每个输出变量。
- Parameters:
- X{array-like, sparse matrix},形状为 (n_samples, n_features)
输入数据。
- y{array-like, sparse matrix},形状为 (n_samples, n_outputs)
多输出目标。一个指示矩阵开启多标签估计。
- 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, sample_weight=None, **partial_fit_params)#
Incrementally fit the model to data, for each output variable.
- Parameters:
- X{array-like, sparse matrix} of shape (n_samples, n_features)
输入数据。
- y{array-like, sparse matrix} of shape (n_samples, n_outputs)
多输出目标。
- sample_weightarray-like of shape (n_samples,), default=None
样本权重。如果为
None
,则样本权重相等。仅在底层回归器支持样本权重时支持。- **partial_fit_paramsdict of str -> object
传递给每个子估计器的
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)
跨多个预测器预测的多输出目标。 注意:每个预测器生成单独的模型。
- score(X, y, sample_weight=None)#
返回预测的决定系数。
决定系数 \(R^2\) 定义为 \((1 - rac{u}{v})\) ,其中 \(u\) 是残差平方和
((y_true - y_pred)** 2).sum()
,而 \(v\) 是总平方和((y_true - y_true.mean()) ** 2).sum()
。最好的可能得分是 1.0,它可能是负的(因为模型可能任意地差)。一个总是预测y
的期望值的常数模型,忽略输入特征,将得到 \(R^2\) 得分为 0.0。- Parameters:
- Xarray-like of shape (n_samples, n_features)
测试样本。对于某些估计器,这可能是一个预计算的核矩阵或一个形状为
(n_samples, n_samples_fitted)
的通用对象列表,其中n_samples_fitted
是估计器拟合中使用的样本数量。- yarray-like of shape (n_samples,) or (n_samples, n_outputs)
X
的真实值。- sample_weightarray-like of shape (n_samples,), default=None
样本权重。
- Returns:
- scorefloat
\(R^2\) 相对于
y
的self.predict(X)
。
Notes
在调用回归器的
score
时使用的 \(R^2\) 得分从 0.23 版本开始使用multioutput='uniform_average'
以保持与r2_score
默认值一致。 这影响了所有多输出回归器的score
方法(除了MultiOutputRegressor
)。
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MultiOutputRegressor #
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(*, sample_weight: bool | None | str = '$UNCHANGED$') MultiOutputRegressor #
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:
- 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.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MultiOutputRegressor #
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.