sklearn.multioutput.RegressorChain

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

A multi-label model that arranges regressions into a chain.

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.

New in version 0.20.

Parameters:
base_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 splitter,

  • An iterable yielding (train, test) splits as arrays of indices.

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.

New in version 1.2.

Attributes:
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.

New in version 0.24.

feature_names_in_ndarray of shape (n_features_in_,)

Names of features seen during fit. Defined only when X has feature names that are all strings.

New in version 1.0.

See also

ClassifierChain

Equivalent for classification.

MultiOutputRegressor

Learns each output independently rather than chaining.

Examples

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

Methods

fit(X, Y, **fit_params)

Fit the model to data matrix X and targets Y.

get_params([deep])

Get parameters for this estimator.

predict(X)

Predict on the data matrix X using the ClassifierChain model.

score(X, y[, sample_weight])

Return the coefficient of determination of the prediction.

set_params(**params)

Set the parameters of this estimator.

fit(X, Y, **fit_params)[source]

Fit the model to data matrix X and targets Y.

Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)

The input data.

Yarray-like of shape (n_samples, n_classes)

The target values.

**fit_paramsdict of string -> object

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

New in version 0.23.

Returns:
selfobject

Returns a fitted instance.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

predict(X)[source]

Predict on the data matrix X using the ClassifierChain model.

Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)

The input data.

Returns:
Y_predarray-like of shape (n_samples, n_classes)

The predicted values.

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

Return the coefficient of determination of the prediction.

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.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True values for X.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

\(R^2\) of self.predict(X) w.r.t. y.

Notes

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]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.