RegressorChain#

class sklearn.multioutput.RegressorChain(estimator=None, *, order=None, cv=None, random_state=None, verbose=False, base_estimator='deprecated')[source]#

将回归器排列成链的多标签模型。

Each model makes a prediction in the order specified by the chain using all of the available features provided to the model plus the predictions of models that are earlier in the chain.

Read more in the User Guide.

0.20 版本新增。

参数:
estimatorestimator

The base estimator from which the regressor chain is built.

orderarray-like of shape (n_outputs,) or ‘random’, default=None

If None, the order will be determined by the order of columns in the label matrix Y.

order = [0, 1, 2, ..., Y.shape[1] - 1]

The order of the chain can be explicitly set by providing a list of integers. For example, for a chain of length 5.

order = [1, 3, 2, 4, 0]

means that the first model in the chain will make predictions for column 1 in the Y matrix, the second model will make predictions for column 3, etc.

If order is ‘random’ a random ordering will be used.

cvint, cross-validation generator or an iterable, default=None

Determines whether to use cross validated predictions or true labels for the results of previous estimators in the chain. Possible inputs for cv are

  • None, to use true labels when fitting,

  • integer, to specify the number of folds in a (Stratified)KFold,

  • CV 分割器,

  • 一个可迭代对象,产生索引数组形式的 (训练集, 测试集) 拆分。

random_stateint, RandomState instance or None, optional (default=None)

If order='random', determines random number generation for the chain order. In addition, it controls the random seed given at each base_estimator at each chaining iteration. Thus, it is only used when base_estimator exposes a random_state. Pass an int for reproducible output across multiple function calls. See Glossary.

verbosebool, default=False

If True, chain progress is output as each model is completed.

1.2 版本新增。

base_estimatorestimator, default=”deprecated”

Use estimator instead.

Deprecated since version 1.7: base_estimator is deprecated and will be removed in 1.9. Use estimator instead.

属性:
estimators_list

A list of clones of base_estimator.

order_list

The order of labels in the classifier chain.

n_features_in_int

Number of features seen during fit. Only defined if the underlying base_estimator exposes such an attribute when fit.

0.24 版本新增。

feature_names_in_shape 为 (n_features_in_,) 的 ndarray

fit 期间看到的特征名称。仅当 X 具有全部为字符串的特征名称时才定义。

1.0 版本新增。

另请参阅

ClassifierChain

Equivalent for classification.

MultiOutputRegressor

Learns each output independently rather than chaining.

示例

>>> from sklearn.multioutput import RegressorChain
>>> from sklearn.linear_model import LogisticRegression
>>> logreg = LogisticRegression(solver='lbfgs')
>>> X, Y = [[1, 0], [0, 1], [1, 1]], [[0, 2], [1, 1], [2, 0]]
>>> chain = RegressorChain(logreg, order=[0, 1]).fit(X, Y)
>>> chain.predict(X)
array([[0., 2.],
       [1., 1.],
       [2., 0.]])
fit(X, Y, **fit_params)[source]#

将模型拟合到数据矩阵 X 和目标 Y。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

输入数据。

Y形状为 (n_samples, n_classes) 的类数组

目标值。

**fit_paramsdict of string -> object

Parameters passed to the fit method at each step of the regressor chain.

0.23 版本新增。

返回:
selfobject

返回拟合的实例。

get_metadata_routing()[source]#

获取此对象的元数据路由。

请查阅 用户指南,了解路由机制如何工作。

在版本 1.3 中新增。

返回:
routingMetadataRouter

封装路由信息的 MetadataRouter

get_params(deep=True)[source]#

获取此估计器的参数。

参数:
deepbool, default=True

如果为 True,将返回此估计器以及包含的子对象(如果它们是估计器)的参数。

返回:
paramsdict

参数名称映射到其值。

predict(X)[source]#

Predict on the data matrix X using the ClassifierChain model.

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

输入数据。

返回:
Y_predarray-like of shape (n_samples, n_classes)

预测值。

score(X, y, sample_weight=None)[source]#

返回测试数据的 决定系数

The coefficient of determination, \(R^2\), is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)** 2).sum() and \(v\) is the total sum of squares ((y_true - y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.

参数:
Xshape 为 (n_samples, n_features) 的 array-like

测试样本。对于某些估计器,这可能是一个预先计算的核矩阵或一个通用对象列表,形状为 (n_samples, n_samples_fitted),其中 n_samples_fitted 是用于估计器拟合的样本数。

yshape 为 (n_samples,) 或 (n_samples, n_outputs) 的 array-like

X 的真实值。

sample_weightshape 为 (n_samples,) 的 array-like, default=None

样本权重。

返回:
scorefloat

self.predict(X) 相对于 y\(R^2\)

注意事项

The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

set_params(**params)[source]#

设置此估计器的参数。

此方法适用于简单的估计器以及嵌套对象(如 Pipeline)。后者具有 <component>__<parameter> 形式的参数,以便可以更新嵌套对象的每个组件。

参数:
**paramsdict

估计器参数。

返回:
selfestimator instance

估计器实例。

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') RegressorChain[source]#

配置是否应请求元数据以传递给 score 方法。

请注意,此方法仅在以下情况下相关:此估计器用作 元估计器 中的子估计器,并且通过 enable_metadata_routing=True 启用了元数据路由(请参阅 sklearn.set_config)。请查看 用户指南 以了解路由机制的工作原理。

每个参数的选项如下:

  • True:请求元数据,如果提供则传递给 score。如果未提供元数据,则忽略该请求。

  • False:不请求元数据,元估计器不会将其传递给 score

  • None:不请求元数据,如果用户提供元数据,元估计器将引发错误。

  • str:应将元数据以给定别名而不是原始名称传递给元估计器。

默认值 (sklearn.utils.metadata_routing.UNCHANGED) 保留现有请求。这允许您更改某些参数的请求而不更改其他参数。

在版本 1.3 中新增。

参数:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

score 方法中 sample_weight 参数的元数据路由。

返回:
selfobject

更新后的对象。