sklearn.multioutput
.MultiOutputClassifier¶
- class sklearn.multioutput.MultiOutputClassifier(estimator, *, n_jobs=None)[source]¶
Multi target classification.
This strategy consists of fitting one classifier per target. This is a simple strategy for extending classifiers that do not natively support multi-target classification.
- Parameters
- estimatorestimator object
An estimator object implementing fit, score and predict_proba.
- n_jobsint or None, optional (default=None)
The number of jobs to run in parallel.
fit
,predict
andpartial_fit
(if supported by the passed estimator) will be parallelized for each target.When individual estimators are fast to train or predict, using
n_jobs > 1
can result in slower performance due to the parallelism overhead.None
means1
unless in ajoblib.parallel_backend
context.-1
means using all available processes / threads. See Glossary for more details.Changed in version 0.20:
n_jobs
default changed from1
toNone
.
- Attributes
- classes_ndarray of shape (n_classes,)
Class labels.
- estimators_list of
n_output
estimators Estimators used for predictions.
- n_features_in_int
Number of features seen during fit. Only defined if the underlying
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. Only defined if the underlying estimators expose such an attribute when fit.
New in version 1.0.
See also
ClassifierChain
A multi-label model that arranges binary classifiers into a chain.
MultiOutputRegressor
Fits one regressor per target variable.
Examples
>>> import numpy as np >>> from sklearn.datasets import make_multilabel_classification >>> from sklearn.multioutput import MultiOutputClassifier >>> from sklearn.neighbors import KNeighborsClassifier >>> X, y = make_multilabel_classification(n_classes=3, random_state=0) >>> clf = MultiOutputClassifier(KNeighborsClassifier()).fit(X, y) >>> clf.predict(X[-2:]) array([[1, 1, 0], [1, 1, 1]])
Methods
fit
(X, Y[, sample_weight])Fit the model to data matrix X and targets Y.
get_params
([deep])Get parameters for this estimator.
partial_fit
(X, y[, classes, sample_weight])Incrementally fit a separate model for each class output.
predict
(X)Predict multi-output variable using model for each target variable.
Return prediction probabilities for each class of each output.
score
(X, y)Return the mean accuracy on the given test data and labels.
set_params
(**params)Set the parameters of this estimator.
- fit(X, Y, sample_weight=None, **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.
- sample_weightarray-like of shape (n_samples,), default=None
Sample weights. If
None
, then samples are equally weighted. Only supported if the underlying classifier supports sample weights.- **fit_paramsdict of string -> object
Parameters passed to the
estimator.fit
method of each step.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.
- partial_fit(X, y, classes=None, sample_weight=None)[source]¶
Incrementally fit a separate model for each class output.
- Parameters
- X{array-like, sparse matrix} of shape (n_samples, n_features)
The input data.
- y{array-like, sparse matrix} of shape (n_samples, n_outputs)
Multi-output targets.
- classeslist of ndarray of shape (n_outputs,), default=None
Each array is unique classes for one output in str/int. Can be obtained via
[np.unique(y[:, i]) for i in range(y.shape[1])]
, wherey
is the target matrix of the entire dataset. This argument is required for the first call to partial_fit and can be omitted in the subsequent calls. Note thaty
doesn’t need to contain all labels inclasses
.- sample_weightarray-like of shape (n_samples,), default=None
Sample weights. If
None
, then samples are equally weighted. Only supported if the underlying regressor supports sample weights.
- Returns
- selfobject
Returns a fitted instance.
- predict(X)[source]¶
Predict multi-output variable using model for each target variable.
- Parameters
- X{array-like, sparse matrix} of shape (n_samples, n_features)
The input data.
- Returns
- y{array-like, sparse matrix} of shape (n_samples, n_outputs)
Multi-output targets predicted across multiple predictors. Note: Separate models are generated for each predictor.
- predict_proba(X)[source]¶
Return prediction probabilities for each class of each output.
This method will raise a
ValueError
if any of the estimators do not havepredict_proba
.- Parameters
- Xarray-like of shape (n_samples, n_features)
The input data.
- Returns
- parray of shape (n_samples, n_classes), or a list of n_outputs such arrays if n_outputs > 1.
The class probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.
Changed in version 0.19: This function now returns a list of arrays where the length of the list is
n_outputs
, and each array is (n_samples
,n_classes
) for that particular output.
- score(X, y)[source]¶
Return the mean accuracy on the given test data and labels.
- Parameters
- Xarray-like of shape (n_samples, n_features)
Test samples.
- yarray-like of shape (n_samples, n_outputs)
True values for X.
- Returns
- scoresfloat
Mean accuracy of predicted target versus true target.
- 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.