ClassifierChain#
- class sklearn.multioutput.ClassifierChain(base_estimator, *, order=None, cv=None, chain_method='predict', random_state=None, verbose=False)[source]#
A multi-label model that arranges binary classifiers 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.
For an example of how to use
ClassifierChain
and benefit from its ensemble, see ClassifierChain on a yeast dataset example.Read more in the User guide.
Added in version 0.19.
- Parameters:
- base_estimatorestimator
The base estimator from which the classifier 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,
An iterable yielding (train, test) splits as arrays of indices.
- chain_method{‘predict’, ‘predict_proba’, ‘predict_log_proba’, ‘decision_function’} or list of such str’s, default=’predict’
Prediction method to be used by estimators in the chain for the ‘prediction’ features of previous estimators in the chain.
if
str
, name of the method;if a list of
str
, provides the method names in order of preference. The method used corresponds to the first method in the list that is implemented bybase_estimator
.
Added in version 1.5.
- 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 eachbase_estimator
at each chaining iteration. Thus, it is only used whenbase_estimator
exposes arandom_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.
Added in version 1.2.
- Attributes:
- classes_list
A list of arrays of length
len(estimators_)
containing the class labels for each estimator in the chain.- estimators_list
A list of clones of base_estimator.
- order_list
The order of labels in the classifier chain.
- chain_method_str
Prediction method used by estimators in the chain for the prediction features.
- n_features_in_int
Number of features seen during fit. Only defined if the underlying
base_estimator
exposes such an attribute when fit.Added 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.Added in version 1.0.
See also
RegressorChain
Equivalent for regression.
MultiOutputClassifier
Classifies each output independently rather than chaining.
References
Jesse Read, Bernhard Pfahringer, geoff Holmes, Eibe Frank, “Classifier Chains for Multi-label Classification”, 2009.
Examples
>>> from sklearn.datasets import make_multilabel_classification >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.model_selection import train_test_split >>> from sklearn.multioutput import ClassifierChain >>> X, Y = make_multilabel_classification( ... n_samples=12, n_classes=3, random_state=0 ... ) >>> X_train, X_test, Y_train, Y_test = train_test_split( ... X, Y, random_state=0 ... ) >>> base_lr = LogisticRegression(solver='lbfgs', random_state=0) >>> chain = ClassifierChain(base_lr, order='random', random_state=0) >>> chain.fit(X_train, Y_train).predict(X_test) array([[1., 1., 0.], [1., 0., 0.], [0., 1., 0.]]) >>> chain.predict_proba(X_test) array([[0.8387..., 0.9431..., 0.4576...], [0.8878..., 0.3684..., 0.2640...], [0.0321..., 0.9935..., 0.0626...]])
- decision_function(X)[source]#
Evaluate the decision_function of the models in the chain.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
The input data.
- Returns:
- Y_decisionarray-like of shape (n_samples, n_classes)
Returns the decision function of the sample for each model in the chain.
- 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 of each step.Only available if
enable_metadata_routing=True
. See the User guide.Added in version 1.3.
- Returns:
- selfobject
Class instance.
- get_metadata_routing()[source]#
get metadata routing of this object.
Please check User guide on how the routing mechanism works.
Added in version 1.3.
- Returns:
- routingMetadataRouter
A
MetadataRouter
encapsulating routing information.
- 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.
- predict_log_proba(X)[source]#
Predict logarithm of probability estimates.
- Parameters:
- X{array-like, sparse matrix} of shape (n_samples, n_features)
The input data.
- Returns:
- Y_log_probarray-like of shape (n_samples, n_classes)
The predicted logarithm of the probabilities.
- predict_proba(X)[source]#
Predict probability estimates.
- Parameters:
- X{array-like, sparse matrix} of shape (n_samples, n_features)
The input data.
- Returns:
- Y_probarray-like of shape (n_samples, n_classes)
The predicted probabilities.
- score(X, y, sample_weight=None)[source]#
Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Test samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs)
True labels for
X
.- sample_weightarray-like of shape (n_samples,), default=None
Sample weights.
- Returns:
- scorefloat
Mean accuracy of
self.predict(X)
w.r.t.y
.
- 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.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANgED$') ClassifierChain [source]#
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.
gallery examples#
Multilabel classification using a classifier chain