MultiOutputClassifier#
- classsklearn.multioutput.MultiOutputClassifier(estimator,*,n_jobs=None)[source]#
Multi target classification.
This strategy consists of fitting one classifier per target. This is asimple strategy for extending classifiers that do not natively supportmulti-target classification.
- Parameters:
- estimatorestimator object
An estimator object implementingfit andpredict.Apredict_proba method will be exposed only if
estimatorimplementsit.- n_jobsint or None, optional (default=None)
The number of jobs to run in parallel.
fit,predictandpartial_fit(if supportedby the passed estimator) will be parallelized for each target.When individual estimators are fast to train or predict,using
n_jobs>1can result in slower performance dueto the parallelism overhead.Nonemeans1unless in ajoblib.parallel_backendcontext.-1means using all available processes / threads.SeeGlossary for more details.Changed in version 0.20:
n_jobsdefault changed from1toNone.
- Attributes:
- classes_ndarray of shape (n_classes,)
Class labels.
- estimators_list of
n_outputestimators Estimators used for predictions.
- n_features_in_int
Number of features seen duringfit. Only defined if theunderlying
estimatorexposes such an attribute when fit.Added in version 0.24.
- feature_names_in_ndarray of shape (
n_features_in_,) Names of features seen duringfit. Only defined if theunderlying estimators expose such an attribute when fit.
Added in version 1.0.
See also
ClassifierChainA multi-label model that arranges binary classifiers into a chain.
MultiOutputRegressorFits one regressor per target variable.
Examples
>>>importnumpyasnp>>>fromsklearn.datasetsimportmake_multilabel_classification>>>fromsklearn.multioutputimportMultiOutputClassifier>>>fromsklearn.linear_modelimportLogisticRegression>>>X,y=make_multilabel_classification(n_classes=3,random_state=0)>>>clf=MultiOutputClassifier(LogisticRegression()).fit(X,y)>>>clf.predict(X[-2:])array([[1, 1, 1], [1, 0, 1]])
- 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 sampleweights.- **fit_paramsdict of string -> object
Parameters passed to the
estimator.fitmethod of each step.Added in version 0.23.
- Returns:
- selfobject
Returns a fitted instance.
- get_metadata_routing()[source]#
Get metadata routing of this object.
Please checkUser Guide on how the routingmechanism works.
Added in version 1.3.
- Returns:
- routingMetadataRouter
A
MetadataRouterencapsulatingrouting information.
- get_params(deep=True)[source]#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator andcontained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- partial_fit(X,y,classes=None,sample_weight=None,**partial_fit_params)[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])foriinrange(y.shape[1])], whereyis the target matrix of the entire dataset.This argument is required for the first call to partial_fitand can be omitted in the subsequent calls.Note thatydoesn’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 sampleweights.- **partial_fit_paramsdict of str -> object
Parameters passed to the
estimator.partial_fitmethod of eachsub-estimator.Only available if
enable_metadata_routing=True. See theUser Guide.Added in version 1.3.
- 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
ValueErrorif any of theestimators 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 theclasses corresponds to that in the attributeclasses_.
Changed in version 0.19:This function now returns a list of arrays where the length ofthe 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_fit_request(*,sample_weight:bool|None|str='$UNCHANGED$')→MultiOutputClassifier[source]#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as asub-estimator within ameta-estimator and metadata routing is enabledwith
enable_metadata_routing=True(seesklearn.set_config).Please check theUser Guide on how the routingmechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif 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 theexisting request. This allows you to change the request for someparameters and not others.Added in version 1.3.
- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter infit.
- Returns:
- selfobject
The updated object.
- 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 haveparameters of the form<component>__<parameter>so that it’spossible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.
- set_partial_fit_request(*,classes:bool|None|str='$UNCHANGED$',sample_weight:bool|None|str='$UNCHANGED$')→MultiOutputClassifier[source]#
Configure whether metadata should be requested to be passed to the
partial_fitmethod.Note that this method is only relevant when this estimator is used as asub-estimator within ameta-estimator and metadata routing is enabledwith
enable_metadata_routing=True(seesklearn.set_config).Please check theUser Guide on how the routingmechanism works.The options for each parameter are:
True: metadata is requested, and passed topartial_fitif 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 theexisting request. This allows you to change the request for someparameters and not others.Added in version 1.3.
- Parameters:
- classesstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
classesparameter inpartial_fit.- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter inpartial_fit.
- Returns:
- selfobject
The updated object.
