sklearn.multiclass.OneVsRestClassifier
class sklearn.multiclass.OneVsRestClassifier(estimator, *, n_jobs=None) [source]

One-vs-the-rest (OvR) multiclass strategy. Also known as one-vs-all, this strategy consists in fitting one classifier per class. For each classifier, the class is fitted against all the other classes. In addition to its computational efficiency (only n_classes classifiers are needed), one advantage of this approach is its interpretability. Since each class is represented by one and one classifier only, it is possible to gain knowledge about the class by inspecting its corresponding classifier. This is the most commonly used strategy for multiclass classification and is a fair default choice. OneVsRestClassifier can also be used for multilabel classification. To use this feature, provide an indicator matrix for the target y when calling .fit. In other words, the target labels should be formatted as a 2D binary (0/1) matrix, where [i, j] == 1 indicates the presence of label j in sample i. This estimator uses the binary relevance method to perform multilabel classification, which involves training one binary classifier independently for each label. Read more in the User Guide.  Parameters

estimatorestimator object

An estimator object implementing fit and one of decision_function or predict_proba.
n_jobsint, default=None

The number of jobs to use for the computation: the n_classes one-vs-rest problems are computed in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.  Changed in version v0.20: n_jobs default changed from 1 to None     Attributes

estimators_list of n_classes estimators

Estimators used for predictions.
coef_ndarray of shape (1, n_features) or (n_classes, n_features)

Coefficient of the features in the decision function. This attribute exists only if the estimators_ defines coef_.  Deprecated since version 0.24: This attribute is deprecated in 0.24 and will be removed in 1.1 (renaming of 0.26). If you use this attribute in RFE or SelectFromModel, you may pass a callable to the importance_getter parameter that extracts feature the importances from estimators_.
intercept_ndarray of shape (1, 1) or (n_classes, 1)

If y is binary, the shape is (1, 1) else (n_classes, 1) This attribute exists only if the estimators_ defines intercept_.  Deprecated since version 0.24: This attribute is deprecated in 0.24 and will be removed in 1.1 (renaming of 0.26). If you use this attribute in RFE or SelectFromModel, you may pass a callable to the importance_getter parameter that extracts feature the importances from estimators_.
classes_array, shape = [n_classes]

Class labels.
n_classes_int

Number of classes.
label_binarizer_LabelBinarizer object

Object used to transform multiclass labels to binary labels and vice-versa.
multilabel_boolean

Whether this is a multilabel classifier      See also
sklearn.multioutput.MultiOutputClassifier

Alternate way of extending an estimator for multilabel classification.
sklearn.preprocessing.MultiLabelBinarizer

Transform iterable of iterables to binary indicator matrix.    Examples >>> import numpy as np
>>> from sklearn.multiclass import OneVsRestClassifier
>>> from sklearn.svm import SVC
>>> X = np.array([
...     [10, 10],
...     [8, 10],
...     [-5, 5.5],
...     [-5.4, 5.5],
...     [-20, -20],
...     [-15, -20]
... ])
>>> y = np.array([0, 0, 1, 1, 2, 2])
>>> clf = OneVsRestClassifier(SVC()).fit(X, y)
>>> clf.predict([[-19, -20], [9, 9], [-5, 5]])
array([2, 0, 1])
 Methods
decision_function(X) Returns the distance of each sample from the decision boundary for each class.
fit(X, y) Fit underlying estimators.
get_params([deep]) Get parameters for this estimator.
partial_fit(X, y[, classes]) Partially fit underlying estimators
predict(X) Predict multi-class targets using underlying estimators.
predict_proba(X) Probability estimates.
score(X, y[, sample_weight]) Return the mean accuracy on the given test data and labels.
set_params(**params) Set the parameters of this estimator.
decision_function(X) [source]

Returns the distance of each sample from the decision boundary for each class. This can only be used with estimators which implement the decision_function method.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Tarray-like of shape (n_samples, n_classes) or (n_samples,) for binary classification.

 Changed in version 0.19: output shape changed to (n_samples,) to conform to scikit-learn conventions for binary classification.

fit(X, y) [source]

Fit underlying estimators.  Parameters

X(sparse) array-like of shape (n_samples, n_features)

Data.
y(sparse) array-like of shape (n_samples,) or (n_samples, n_classes)

Multi-class targets. An indicator matrix turns on multilabel classification.    Returns
 self

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.

property multilabel_
Whether this is a multilabel classifier

partial_fit(X, y, classes=None) [source]

Partially fit underlying estimators Should be used when memory is inefficient to train all data. Chunks of data can be passed in several iteration.  Parameters

X(sparse) array-like of shape (n_samples, n_features)

Data.
y(sparse) array-like of shape (n_samples,) or (n_samples, n_classes)

Multi-class targets. An indicator matrix turns on multilabel classification.
classesarray, shape (n_classes, )

Classes across all calls to partial_fit. Can be obtained via np.unique(y_all), where y_all is the target vector of the entire dataset. This argument is only required in the first call of partial_fit and can be omitted in the subsequent calls.    Returns
 self

predict(X) [source]

Predict multi-class targets using underlying estimators.  Parameters

X(sparse) array-like of shape (n_samples, n_features)

Data.    Returns

y(sparse) array-like of shape (n_samples,) or (n_samples, n_classes)

Predicted multi-class targets.

predict_proba(X) [source]

Probability estimates. The returned estimates for all classes are ordered by label of classes. Note that in the multilabel case, each sample can have any number of labels. This returns the marginal probability that the given sample has the label in question. For example, it is entirely consistent that two labels both have a 90% probability of applying to a given sample. In the single label multiclass case, the rows of the returned matrix sum to 1.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

T(sparse) array-like of shape (n_samples, n_classes)

Returns the probability of the sample for each class in the model, where classes are ordered as they are in self.classes_.

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) wrt. 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.

 Examples using sklearn.multiclass.OneVsRestClassifier

  Multilabel classification

  Receiver Operating Characteristic (ROC)

  Precision-Recall

  Classifier Chain


decision_function(X) [source]

Returns the distance of each sample from the decision boundary for each class. This can only be used with estimators which implement the decision_function method.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Tarray-like of shape (n_samples, n_classes) or (n_samples,) for binary classification.

 Changed in version 0.19: output shape changed to (n_samples,) to conform to scikit-learn conventions for binary classification.


fit(X, y) [source]

Fit underlying estimators.  Parameters

X(sparse) array-like of shape (n_samples, n_features)

Data.
y(sparse) array-like of shape (n_samples,) or (n_samples, n_classes)

Multi-class targets. An indicator matrix turns on multilabel classification.    Returns
 self


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.


property multilabel_
Whether this is a multilabel classifier


partial_fit(X, y, classes=None) [source]

Partially fit underlying estimators Should be used when memory is inefficient to train all data. Chunks of data can be passed in several iteration.  Parameters

X(sparse) array-like of shape (n_samples, n_features)

Data.
y(sparse) array-like of shape (n_samples,) or (n_samples, n_classes)

Multi-class targets. An indicator matrix turns on multilabel classification.
classesarray, shape (n_classes, )

Classes across all calls to partial_fit. Can be obtained via np.unique(y_all), where y_all is the target vector of the entire dataset. This argument is only required in the first call of partial_fit and can be omitted in the subsequent calls.    Returns
 self


predict(X) [source]

Predict multi-class targets using underlying estimators.  Parameters

X(sparse) array-like of shape (n_samples, n_features)

Data.    Returns

y(sparse) array-like of shape (n_samples,) or (n_samples, n_classes)

Predicted multi-class targets.


predict_proba(X) [source]

Probability estimates. The returned estimates for all classes are ordered by label of classes. Note that in the multilabel case, each sample can have any number of labels. This returns the marginal probability that the given sample has the label in question. For example, it is entirely consistent that two labels both have a 90% probability of applying to a given sample. In the single label multiclass case, the rows of the returned matrix sum to 1.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

T(sparse) array-like of shape (n_samples, n_classes)

Returns the probability of the sample for each class in the model, where classes are ordered as they are in self.classes_.


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) wrt. 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.


class sklearn.multiclass.OutputCodeClassifier(estimator, *, code_size=1.5, random_state=None, n_jobs=None) [source]

(Error-Correcting) Output-Code multiclass strategy Output-code based strategies consist in representing each class with a binary code (an array of 0s and 1s). At fitting time, one binary classifier per bit in the code book is fitted. At prediction time, the classifiers are used to project new points in the class space and the class closest to the points is chosen. The main advantage of these strategies is that the number of classifiers used can be controlled by the user, either for compressing the model (0 < code_size < 1) or for making the model more robust to errors (code_size > 1). See the documentation for more details. Read more in the User Guide.  Parameters

estimatorestimator object

An estimator object implementing fit and one of decision_function or predict_proba.
code_sizefloat

Percentage of the number of classes to be used to create the code book. A number between 0 and 1 will require fewer classifiers than one-vs-the-rest. A number greater than 1 will require more classifiers than one-vs-the-rest.
random_stateint, RandomState instance, default=None

The generator used to initialize the codebook. Pass an int for reproducible output across multiple function calls. See Glossary.
n_jobsint, default=None

The number of jobs to use for the computation: the multiclass problems are computed in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.    Attributes

estimators_list of int(n_classes * code_size) estimators

Estimators used for predictions.
classes_numpy array of shape [n_classes]

Array containing labels.
code_book_numpy array of shape [n_classes, code_size]

Binary array containing the code of each class.     References
1
“Solving multiclass learning problems via error-correcting output codes”, Dietterich T., Bakiri G., Journal of Artificial Intelligence Research 2, 1995.
2
“The error coding method and PICTs”, James G., Hastie T., Journal of Computational and Graphical statistics 7, 1998.
3
“The Elements of Statistical Learning”, Hastie T., Tibshirani R., Friedman J., page 606 (second-edition) 2008.   Examples >>> from sklearn.multiclass import OutputCodeClassifier
>>> from sklearn.ensemble import RandomForestClassifier
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=100, n_features=4,
...                            n_informative=2, n_redundant=0,
...                            random_state=0, shuffle=False)
>>> clf = OutputCodeClassifier(
...     estimator=RandomForestClassifier(random_state=0),
...     random_state=0).fit(X, y)
>>> clf.predict([[0, 0, 0, 0]])
array([1])
 Methods
fit(X, y) Fit underlying estimators.
get_params([deep]) Get parameters for this estimator.
predict(X) Predict multi-class targets using underlying estimators.
score(X, y[, sample_weight]) Return the mean accuracy on the given test data and labels.
set_params(**params) Set the parameters of this estimator.
fit(X, y) [source]

Fit underlying estimators.  Parameters

X(sparse) array-like of shape (n_samples, n_features)

Data.
ynumpy array of shape [n_samples]

Multi-class targets.    Returns
 self

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 multi-class targets using underlying estimators.  Parameters

X(sparse) array-like of shape (n_samples, n_features)

Data.    Returns

ynumpy array of shape [n_samples]

Predicted multi-class targets.

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) wrt. 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.


sklearn.multiclass.OutputCodeClassifier
class sklearn.multiclass.OutputCodeClassifier(estimator, *, code_size=1.5, random_state=None, n_jobs=None) [source]

(Error-Correcting) Output-Code multiclass strategy Output-code based strategies consist in representing each class with a binary code (an array of 0s and 1s). At fitting time, one binary classifier per bit in the code book is fitted. At prediction time, the classifiers are used to project new points in the class space and the class closest to the points is chosen. The main advantage of these strategies is that the number of classifiers used can be controlled by the user, either for compressing the model (0 < code_size < 1) or for making the model more robust to errors (code_size > 1). See the documentation for more details. Read more in the User Guide.  Parameters

estimatorestimator object

An estimator object implementing fit and one of decision_function or predict_proba.
code_sizefloat

Percentage of the number of classes to be used to create the code book. A number between 0 and 1 will require fewer classifiers than one-vs-the-rest. A number greater than 1 will require more classifiers than one-vs-the-rest.
random_stateint, RandomState instance, default=None

The generator used to initialize the codebook. Pass an int for reproducible output across multiple function calls. See Glossary.
n_jobsint, default=None

The number of jobs to use for the computation: the multiclass problems are computed in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.    Attributes

estimators_list of int(n_classes * code_size) estimators

Estimators used for predictions.
classes_numpy array of shape [n_classes]

Array containing labels.
code_book_numpy array of shape [n_classes, code_size]

Binary array containing the code of each class.     References
1
“Solving multiclass learning problems via error-correcting output codes”, Dietterich T., Bakiri G., Journal of Artificial Intelligence Research 2, 1995.
2
“The error coding method and PICTs”, James G., Hastie T., Journal of Computational and Graphical statistics 7, 1998.
3
“The Elements of Statistical Learning”, Hastie T., Tibshirani R., Friedman J., page 606 (second-edition) 2008.   Examples >>> from sklearn.multiclass import OutputCodeClassifier
>>> from sklearn.ensemble import RandomForestClassifier
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=100, n_features=4,
...                            n_informative=2, n_redundant=0,
...                            random_state=0, shuffle=False)
>>> clf = OutputCodeClassifier(
...     estimator=RandomForestClassifier(random_state=0),
...     random_state=0).fit(X, y)
>>> clf.predict([[0, 0, 0, 0]])
array([1])
 Methods
fit(X, y) Fit underlying estimators.
get_params([deep]) Get parameters for this estimator.
predict(X) Predict multi-class targets using underlying estimators.
score(X, y[, sample_weight]) Return the mean accuracy on the given test data and labels.
set_params(**params) Set the parameters of this estimator.
fit(X, y) [source]

Fit underlying estimators.  Parameters

X(sparse) array-like of shape (n_samples, n_features)

Data.
ynumpy array of shape [n_samples]

Multi-class targets.    Returns
 self

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 multi-class targets using underlying estimators.  Parameters

X(sparse) array-like of shape (n_samples, n_features)

Data.    Returns

ynumpy array of shape [n_samples]

Predicted multi-class targets.

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) wrt. 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.


fit(X, y) [source]

Fit underlying estimators.  Parameters

X(sparse) array-like of shape (n_samples, n_features)

Data.
ynumpy array of shape [n_samples]

Multi-class targets.    Returns
 self


predict(X) [source]

Predict multi-class targets using underlying estimators.  Parameters

X(sparse) array-like of shape (n_samples, n_features)

Data.    Returns

ynumpy array of shape [n_samples]

Predicted multi-class targets.


class sklearn.multioutput.ClassifierChain(base_estimator, *, order=None, cv=None, random_state=None) [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. Read more in the User Guide.  New 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,
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.    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.      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.0625...]])
 Methods
decision_function(X) Evaluate the decision_function of the models in the chain.
fit(X, Y) 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.
predict_proba(X) Predict probability estimates.
score(X, y[, sample_weight]) Return the mean accuracy on the given test data and labels.
set_params(**params) Set the parameters of this estimator.
decision_function(X) [source]

Evaluate the decision_function of the models in the chain.  Parameters

Xarray-like of shape (n_samples, n_features)
  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) [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.    Returns

selfobject

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_proba(X) [source]

Predict probability estimates.  Parameters

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

Y_probarray-like of shape (n_samples, n_classes)

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) wrt. 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.


sklearn.multioutput.ClassifierChain
class sklearn.multioutput.ClassifierChain(base_estimator, *, order=None, cv=None, random_state=None) [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. Read more in the User Guide.  New 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,
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.    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.      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.0625...]])
 Methods
decision_function(X) Evaluate the decision_function of the models in the chain.
fit(X, Y) 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.
predict_proba(X) Predict probability estimates.
score(X, y[, sample_weight]) Return the mean accuracy on the given test data and labels.
set_params(**params) Set the parameters of this estimator.
decision_function(X) [source]

Evaluate the decision_function of the models in the chain.  Parameters

Xarray-like of shape (n_samples, n_features)
  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) [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.    Returns

selfobject

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_proba(X) [source]

Predict probability estimates.  Parameters

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

Y_probarray-like of shape (n_samples, n_classes)

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) wrt. 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.

 Examples using sklearn.multioutput.ClassifierChain

  Classifier Chain


decision_function(X) [source]

Evaluate the decision_function of the models in the chain.  Parameters

Xarray-like of shape (n_samples, n_features)
  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) [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.    Returns

selfobject


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_proba(X) [source]

Predict probability estimates.  Parameters

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

Y_probarray-like of shape (n_samples, n_classes)


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 and partial_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 means 1 unless in a joblib.parallel_backend context. -1 means using all available processes / threads. See Glossary for more details.  Changed in version 0.20: n_jobs default changed from 1 to None     Attributes

classes_ndarray of shape (n_classes,)

Class labels.
estimators_list of n_output estimators

Estimators used for predictions.     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 the model to data.
predict(X) Predict multi-output variable using a model
score(X, y) Returns 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

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 the model to data. Fit a separate model for each output variable.  Parameters

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

Data.
y{array-like, sparse matrix} of shape (n_samples, n_outputs)

Multi-output targets.
classeslist of ndarray of shape (n_outputs,)

Each array is unique classes for one output in str/int Can be obtained by via [np.unique(y[:, i]) for i in range(y.shape[1])], where y 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 that y doesn’t need to contain all labels in classes.
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

predict(X) [source]

 Predict multi-output variable using a model

trained for each target variable.    Parameters

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

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.

property predict_proba
Probability estimates. Returns prediction probabilities for each class of each output. This method will raise a ValueError if any of the estimators do not have predict_proba.  Parameters

Xarray-like of shape (n_samples, n_features)

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]

Returns 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

accuracy_score of self.predict(X) versus 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.


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 and partial_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 means 1 unless in a joblib.parallel_backend context. -1 means using all available processes / threads. See Glossary for more details.  Changed in version 0.20: n_jobs default changed from 1 to None     Attributes

classes_ndarray of shape (n_classes,)

Class labels.
estimators_list of n_output estimators

Estimators used for predictions.     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 the model to data.
predict(X) Predict multi-output variable using a model
score(X, y) Returns 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

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 the model to data. Fit a separate model for each output variable.  Parameters

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

Data.
y{array-like, sparse matrix} of shape (n_samples, n_outputs)

Multi-output targets.
classeslist of ndarray of shape (n_outputs,)

Each array is unique classes for one output in str/int Can be obtained by via [np.unique(y[:, i]) for i in range(y.shape[1])], where y 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 that y doesn’t need to contain all labels in classes.
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

predict(X) [source]

 Predict multi-output variable using a model

trained for each target variable.    Parameters

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

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.

property predict_proba
Probability estimates. Returns prediction probabilities for each class of each output. This method will raise a ValueError if any of the estimators do not have predict_proba.  Parameters

Xarray-like of shape (n_samples, n_features)

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]

Returns 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

accuracy_score of self.predict(X) versus 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.


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


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 the model to data. Fit a separate model for each output variable.  Parameters

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

Data.
y{array-like, sparse matrix} of shape (n_samples, n_outputs)

Multi-output targets.
classeslist of ndarray of shape (n_outputs,)

Each array is unique classes for one output in str/int Can be obtained by via [np.unique(y[:, i]) for i in range(y.shape[1])], where y 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 that y doesn’t need to contain all labels in classes.
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


predict(X) [source]

 Predict multi-output variable using a model

trained for each target variable.    Parameters

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

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.


property predict_proba
Probability estimates. Returns prediction probabilities for each class of each output. This method will raise a ValueError if any of the estimators do not have predict_proba.  Parameters

Xarray-like of shape (n_samples, n_features)

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]

Returns 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

accuracy_score of self.predict(X) versus 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.


class sklearn.multioutput.MultiOutputRegressor(estimator, *, n_jobs=None) [source]

Multi target regression This strategy consists of fitting one regressor per target. This is a simple strategy for extending regressors that do not natively support multi-target regression.  New in version 0.18.   Parameters

estimatorestimator object

An estimator object implementing fit and predict.
n_jobsint or None, optional (default=None)

The number of jobs to run in parallel. fit, predict and partial_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 means 1 unless in a joblib.parallel_backend context. -1 means using all available processes / threads. See Glossary for more details.  Changed in version 0.20: n_jobs default changed from 1 to None     Attributes

estimators_list of n_output estimators

Estimators used for predictions.     Examples >>> import numpy as np
>>> from sklearn.datasets import load_linnerud
>>> from sklearn.multioutput import MultiOutputRegressor
>>> from sklearn.linear_model import Ridge
>>> X, y = load_linnerud(return_X_y=True)
>>> clf = MultiOutputRegressor(Ridge(random_state=123)).fit(X, y)
>>> clf.predict(X[[0]])
array([[176..., 35..., 57...]])
 Methods
fit(X, y[, sample_weight]) Fit the model to data.
get_params([deep]) Get parameters for this estimator.
partial_fit(X, y[, sample_weight]) Incrementally fit the model to data.
predict(X) Predict multi-output variable using a model
score(X, y[, sample_weight]) Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params) Set the parameters of this estimator.
fit(X, y, sample_weight=None, **fit_params) [source]

Fit the model to data. Fit a separate model for each output variable.  Parameters

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

Data.
y{array-like, sparse matrix} of shape (n_samples, n_outputs)

Multi-output targets. An indicator matrix turns on multilabel estimation.
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.
**fit_paramsdict of string -> object

Parameters passed to the estimator.fit method of each step.  New in version 0.23.     Returns

selfobject

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, sample_weight=None) [source]

Incrementally fit the model to data. Fit a separate model for each output variable.  Parameters

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

Data.
y{array-like, sparse matrix} of shape (n_samples, n_outputs)

Multi-output targets.
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

predict(X) [source]

 Predict multi-output variable using a model

trained for each target variable.    Parameters

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

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.

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

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(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) wrt. 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.


sklearn.multioutput.MultiOutputRegressor
class sklearn.multioutput.MultiOutputRegressor(estimator, *, n_jobs=None) [source]

Multi target regression This strategy consists of fitting one regressor per target. This is a simple strategy for extending regressors that do not natively support multi-target regression.  New in version 0.18.   Parameters

estimatorestimator object

An estimator object implementing fit and predict.
n_jobsint or None, optional (default=None)

The number of jobs to run in parallel. fit, predict and partial_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 means 1 unless in a joblib.parallel_backend context. -1 means using all available processes / threads. See Glossary for more details.  Changed in version 0.20: n_jobs default changed from 1 to None     Attributes

estimators_list of n_output estimators

Estimators used for predictions.     Examples >>> import numpy as np
>>> from sklearn.datasets import load_linnerud
>>> from sklearn.multioutput import MultiOutputRegressor
>>> from sklearn.linear_model import Ridge
>>> X, y = load_linnerud(return_X_y=True)
>>> clf = MultiOutputRegressor(Ridge(random_state=123)).fit(X, y)
>>> clf.predict(X[[0]])
array([[176..., 35..., 57...]])
 Methods
fit(X, y[, sample_weight]) Fit the model to data.
get_params([deep]) Get parameters for this estimator.
partial_fit(X, y[, sample_weight]) Incrementally fit the model to data.
predict(X) Predict multi-output variable using a model
score(X, y[, sample_weight]) Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params) Set the parameters of this estimator.
fit(X, y, sample_weight=None, **fit_params) [source]

Fit the model to data. Fit a separate model for each output variable.  Parameters

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

Data.
y{array-like, sparse matrix} of shape (n_samples, n_outputs)

Multi-output targets. An indicator matrix turns on multilabel estimation.
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.
**fit_paramsdict of string -> object

Parameters passed to the estimator.fit method of each step.  New in version 0.23.     Returns

selfobject

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, sample_weight=None) [source]

Incrementally fit the model to data. Fit a separate model for each output variable.  Parameters

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

Data.
y{array-like, sparse matrix} of shape (n_samples, n_outputs)

Multi-output targets.
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

predict(X) [source]

 Predict multi-output variable using a model

trained for each target variable.    Parameters

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

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.

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

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(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) wrt. 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.

 Examples using sklearn.multioutput.MultiOutputRegressor

  Comparing random forests and the multi-output meta estimator


fit(X, y, sample_weight=None, **fit_params) [source]

Fit the model to data. Fit a separate model for each output variable.  Parameters

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

Data.
y{array-like, sparse matrix} of shape (n_samples, n_outputs)

Multi-output targets. An indicator matrix turns on multilabel estimation.
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.
**fit_paramsdict of string -> object

Parameters passed to the estimator.fit method of each step.  New in version 0.23.     Returns

selfobject


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

Incrementally fit the model to data. Fit a separate model for each output variable.  Parameters

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

Data.
y{array-like, sparse matrix} of shape (n_samples, n_outputs)

Multi-output targets.
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


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

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(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) wrt. 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).


class sklearn.multioutput.RegressorChain(base_estimator, *, order=None, cv=None, random_state=None) [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 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,
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.    Attributes

estimators_list

A list of clones of base_estimator.
order_list

The order of labels in the classifier chain.      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 \(R^2\) 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

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 \(R^2\) of the prediction. The coefficient \(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) wrt. 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.


sklearn.multioutput.RegressorChain
class sklearn.multioutput.RegressorChain(base_estimator, *, order=None, cv=None, random_state=None) [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 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,
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.    Attributes

estimators_list

A list of clones of base_estimator.
order_list

The order of labels in the classifier chain.      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 \(R^2\) 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

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 \(R^2\) of the prediction. The coefficient \(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) wrt. 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.


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


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.


class sklearn.naive_bayes.BernoulliNB(*, alpha=1.0, binarize=0.0, fit_prior=True, class_prior=None) [source]

Naive Bayes classifier for multivariate Bernoulli models. Like MultinomialNB, this classifier is suitable for discrete data. The difference is that while MultinomialNB works with occurrence counts, BernoulliNB is designed for binary/boolean features. Read more in the User Guide.  Parameters

alphafloat, default=1.0

Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
binarizefloat or None, default=0.0

Threshold for binarizing (mapping to booleans) of sample features. If None, input is presumed to already consist of binary vectors.
fit_priorbool, default=True

Whether to learn class prior probabilities or not. If false, a uniform prior will be used.
class_priorarray-like of shape (n_classes,), default=None

Prior probabilities of the classes. If specified the priors are not adjusted according to the data.    Attributes

class_count_ndarray of shape (n_classes)

Number of samples encountered for each class during fitting. This value is weighted by the sample weight when provided.
class_log_prior_ndarray of shape (n_classes)

Log probability of each class (smoothed).
classes_ndarray of shape (n_classes,)

Class labels known to the classifier
coef_ndarray of shape (n_classes, n_features)

Mirrors feature_log_prob_ for interpreting BernoulliNB as a linear model.
feature_count_ndarray of shape (n_classes, n_features)

Number of samples encountered for each (class, feature) during fitting. This value is weighted by the sample weight when provided.
feature_log_prob_ndarray of shape (n_classes, n_features)

Empirical log probability of features given a class, P(x_i|y).
intercept_ndarray of shape (n_classes,)

Mirrors class_log_prior_ for interpreting BernoulliNB as a linear model.
n_features_int

Number of features of each sample.     References C.D. Manning, P. Raghavan and H. Schuetze (2008). Introduction to Information Retrieval. Cambridge University Press, pp. 234-265. https://nlp.stanford.edu/IR-book/html/htmledition/the-bernoulli-model-1.html A. McCallum and K. Nigam (1998). A comparison of event models for naive Bayes text classification. Proc. AAAI/ICML-98 Workshop on Learning for Text Categorization, pp. 41-48. V. Metsis, I. Androutsopoulos and G. Paliouras (2006). Spam filtering with naive Bayes – Which naive Bayes? 3rd Conf. on Email and Anti-Spam (CEAS). Examples >>> import numpy as np
>>> rng = np.random.RandomState(1)
>>> X = rng.randint(5, size=(6, 100))
>>> Y = np.array([1, 2, 3, 4, 4, 5])
>>> from sklearn.naive_bayes import BernoulliNB
>>> clf = BernoulliNB()
>>> clf.fit(X, Y)
BernoulliNB()
>>> print(clf.predict(X[2:3]))
[3]
 Methods
fit(X, y[, sample_weight]) Fit Naive Bayes classifier according to X, y
get_params([deep]) Get parameters for this estimator.
partial_fit(X, y[, classes, sample_weight]) Incremental fit on a batch of samples.
predict(X) Perform classification on an array of test vectors X.
predict_log_proba(X) Return log-probability estimates for the test vector X.
predict_proba(X) Return probability estimates for the test vector X.
score(X, y[, sample_weight]) 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) [source]

Fit Naive Bayes classifier according to X, y  Parameters

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

Training vectors, where n_samples is the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,)

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

Weights applied to individual samples (1. for unweighted).    Returns

selfobject

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]

Incremental fit on a batch of samples. This method is expected to be called several times consecutively on different chunks of a dataset so as to implement out-of-core or online learning. This is especially useful when the whole dataset is too big to fit in memory at once. This method has some performance overhead hence it is better to call partial_fit on chunks of data that are as large as possible (as long as fitting in the memory budget) to hide the overhead.  Parameters

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

Training vectors, where n_samples is the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,)

Target values.
classesarray-like of shape (n_classes), default=None

List of all the classes that can possibly appear in the y vector. Must be provided at the first call to partial_fit, can be omitted in subsequent calls.
sample_weightarray-like of shape (n_samples,), default=None

Weights applied to individual samples (1. for unweighted).    Returns

selfobject

predict(X) [source]

Perform classification on an array of test vectors X.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Cndarray of shape (n_samples,)

Predicted target values for X

predict_log_proba(X) [source]

Return log-probability estimates for the test vector X.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Carray-like of shape (n_samples, n_classes)

Returns the log-probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.

predict_proba(X) [source]

Return probability estimates for the test vector X.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Carray-like of shape (n_samples, n_classes)

Returns the probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.

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) wrt. 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.


sklearn.naive_bayes.BernoulliNB
class sklearn.naive_bayes.BernoulliNB(*, alpha=1.0, binarize=0.0, fit_prior=True, class_prior=None) [source]

Naive Bayes classifier for multivariate Bernoulli models. Like MultinomialNB, this classifier is suitable for discrete data. The difference is that while MultinomialNB works with occurrence counts, BernoulliNB is designed for binary/boolean features. Read more in the User Guide.  Parameters

alphafloat, default=1.0

Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
binarizefloat or None, default=0.0

Threshold for binarizing (mapping to booleans) of sample features. If None, input is presumed to already consist of binary vectors.
fit_priorbool, default=True

Whether to learn class prior probabilities or not. If false, a uniform prior will be used.
class_priorarray-like of shape (n_classes,), default=None

Prior probabilities of the classes. If specified the priors are not adjusted according to the data.    Attributes

class_count_ndarray of shape (n_classes)

Number of samples encountered for each class during fitting. This value is weighted by the sample weight when provided.
class_log_prior_ndarray of shape (n_classes)

Log probability of each class (smoothed).
classes_ndarray of shape (n_classes,)

Class labels known to the classifier
coef_ndarray of shape (n_classes, n_features)

Mirrors feature_log_prob_ for interpreting BernoulliNB as a linear model.
feature_count_ndarray of shape (n_classes, n_features)

Number of samples encountered for each (class, feature) during fitting. This value is weighted by the sample weight when provided.
feature_log_prob_ndarray of shape (n_classes, n_features)

Empirical log probability of features given a class, P(x_i|y).
intercept_ndarray of shape (n_classes,)

Mirrors class_log_prior_ for interpreting BernoulliNB as a linear model.
n_features_int

Number of features of each sample.     References C.D. Manning, P. Raghavan and H. Schuetze (2008). Introduction to Information Retrieval. Cambridge University Press, pp. 234-265. https://nlp.stanford.edu/IR-book/html/htmledition/the-bernoulli-model-1.html A. McCallum and K. Nigam (1998). A comparison of event models for naive Bayes text classification. Proc. AAAI/ICML-98 Workshop on Learning for Text Categorization, pp. 41-48. V. Metsis, I. Androutsopoulos and G. Paliouras (2006). Spam filtering with naive Bayes – Which naive Bayes? 3rd Conf. on Email and Anti-Spam (CEAS). Examples >>> import numpy as np
>>> rng = np.random.RandomState(1)
>>> X = rng.randint(5, size=(6, 100))
>>> Y = np.array([1, 2, 3, 4, 4, 5])
>>> from sklearn.naive_bayes import BernoulliNB
>>> clf = BernoulliNB()
>>> clf.fit(X, Y)
BernoulliNB()
>>> print(clf.predict(X[2:3]))
[3]
 Methods
fit(X, y[, sample_weight]) Fit Naive Bayes classifier according to X, y
get_params([deep]) Get parameters for this estimator.
partial_fit(X, y[, classes, sample_weight]) Incremental fit on a batch of samples.
predict(X) Perform classification on an array of test vectors X.
predict_log_proba(X) Return log-probability estimates for the test vector X.
predict_proba(X) Return probability estimates for the test vector X.
score(X, y[, sample_weight]) 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) [source]

Fit Naive Bayes classifier according to X, y  Parameters

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

Training vectors, where n_samples is the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,)

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

Weights applied to individual samples (1. for unweighted).    Returns

selfobject

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]

Incremental fit on a batch of samples. This method is expected to be called several times consecutively on different chunks of a dataset so as to implement out-of-core or online learning. This is especially useful when the whole dataset is too big to fit in memory at once. This method has some performance overhead hence it is better to call partial_fit on chunks of data that are as large as possible (as long as fitting in the memory budget) to hide the overhead.  Parameters

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

Training vectors, where n_samples is the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,)

Target values.
classesarray-like of shape (n_classes), default=None

List of all the classes that can possibly appear in the y vector. Must be provided at the first call to partial_fit, can be omitted in subsequent calls.
sample_weightarray-like of shape (n_samples,), default=None

Weights applied to individual samples (1. for unweighted).    Returns

selfobject

predict(X) [source]

Perform classification on an array of test vectors X.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Cndarray of shape (n_samples,)

Predicted target values for X

predict_log_proba(X) [source]

Return log-probability estimates for the test vector X.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Carray-like of shape (n_samples, n_classes)

Returns the log-probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.

predict_proba(X) [source]

Return probability estimates for the test vector X.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Carray-like of shape (n_samples, n_classes)

Returns the probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.

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) wrt. 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.

 Examples using sklearn.naive_bayes.BernoulliNB

  Hashing feature transformation using Totally Random Trees

  Classification of text documents using sparse features


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

Fit Naive Bayes classifier according to X, y  Parameters

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

Training vectors, where n_samples is the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,)

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

Weights applied to individual samples (1. for unweighted).    Returns

selfobject


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]

Incremental fit on a batch of samples. This method is expected to be called several times consecutively on different chunks of a dataset so as to implement out-of-core or online learning. This is especially useful when the whole dataset is too big to fit in memory at once. This method has some performance overhead hence it is better to call partial_fit on chunks of data that are as large as possible (as long as fitting in the memory budget) to hide the overhead.  Parameters

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

Training vectors, where n_samples is the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,)

Target values.
classesarray-like of shape (n_classes), default=None

List of all the classes that can possibly appear in the y vector. Must be provided at the first call to partial_fit, can be omitted in subsequent calls.
sample_weightarray-like of shape (n_samples,), default=None

Weights applied to individual samples (1. for unweighted).    Returns

selfobject


predict(X) [source]

Perform classification on an array of test vectors X.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Cndarray of shape (n_samples,)

Predicted target values for X


predict_log_proba(X) [source]

Return log-probability estimates for the test vector X.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Carray-like of shape (n_samples, n_classes)

Returns the log-probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.


predict_proba(X) [source]

Return probability estimates for the test vector X.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Carray-like of shape (n_samples, n_classes)

Returns the probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.


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) wrt. 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.


class sklearn.naive_bayes.CategoricalNB(*, alpha=1.0, fit_prior=True, class_prior=None, min_categories=None) [source]

Naive Bayes classifier for categorical features The categorical Naive Bayes classifier is suitable for classification with discrete features that are categorically distributed. The categories of each feature are drawn from a categorical distribution. Read more in the User Guide.  Parameters

alphafloat, default=1.0

Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
fit_priorbool, default=True

Whether to learn class prior probabilities or not. If false, a uniform prior will be used.
class_priorarray-like of shape (n_classes,), default=None

Prior probabilities of the classes. If specified the priors are not adjusted according to the data.
min_categoriesint or array-like of shape (n_features,), default=None

Minimum number of categories per feature.  integer: Sets the minimum number of categories per feature to n_categories for each features. array-like: shape (n_features,) where n_categories[i] holds the minimum number of categories for the ith column of the input. None (default): Determines the number of categories automatically from the training data.   New in version 0.24.     Attributes

category_count_list of arrays of shape (n_features,)

Holds arrays of shape (n_classes, n_categories of respective feature) for each feature. Each array provides the number of samples encountered for each class and category of the specific feature.
class_count_ndarray of shape (n_classes,)

Number of samples encountered for each class during fitting. This value is weighted by the sample weight when provided.
class_log_prior_ndarray of shape (n_classes,)

Smoothed empirical log probability for each class.
classes_ndarray of shape (n_classes,)

Class labels known to the classifier
feature_log_prob_list of arrays of shape (n_features,)

Holds arrays of shape (n_classes, n_categories of respective feature) for each feature. Each array provides the empirical log probability of categories given the respective feature and class, P(x_i|y).
n_features_int

Number of features of each sample.
n_categories_ndarray of shape (n_features,), dtype=np.int64

Number of categories for each feature. This value is inferred from the data or set by the minimum number of categories.  New in version 0.24.      Examples >>> import numpy as np
>>> rng = np.random.RandomState(1)
>>> X = rng.randint(5, size=(6, 100))
>>> y = np.array([1, 2, 3, 4, 5, 6])
>>> from sklearn.naive_bayes import CategoricalNB
>>> clf = CategoricalNB()
>>> clf.fit(X, y)
CategoricalNB()
>>> print(clf.predict(X[2:3]))
[3]
 Methods
fit(X, y[, sample_weight]) Fit Naive Bayes classifier according to X, y
get_params([deep]) Get parameters for this estimator.
partial_fit(X, y[, classes, sample_weight]) Incremental fit on a batch of samples.
predict(X) Perform classification on an array of test vectors X.
predict_log_proba(X) Return log-probability estimates for the test vector X.
predict_proba(X) Return probability estimates for the test vector X.
score(X, y[, sample_weight]) 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) [source]

Fit Naive Bayes classifier according to X, y  Parameters

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

Training vectors, where n_samples is the number of samples and n_features is the number of features. Here, each feature of X is assumed to be from a different categorical distribution. It is further assumed that all categories of each feature are represented by the numbers 0, …, n - 1, where n refers to the total number of categories for the given feature. This can, for instance, be achieved with the help of OrdinalEncoder.
yarray-like of shape (n_samples,)

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

Weights applied to individual samples (1. for unweighted).    Returns

selfobject

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]

Incremental fit on a batch of samples. This method is expected to be called several times consecutively on different chunks of a dataset so as to implement out-of-core or online learning. This is especially useful when the whole dataset is too big to fit in memory at once. This method has some performance overhead hence it is better to call partial_fit on chunks of data that are as large as possible (as long as fitting in the memory budget) to hide the overhead.  Parameters

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

Training vectors, where n_samples is the number of samples and n_features is the number of features. Here, each feature of X is assumed to be from a different categorical distribution. It is further assumed that all categories of each feature are represented by the numbers 0, …, n - 1, where n refers to the total number of categories for the given feature. This can, for instance, be achieved with the help of OrdinalEncoder.
yarray-like of shape (n_samples)

Target values.
classesarray-like of shape (n_classes), default=None

List of all the classes that can possibly appear in the y vector. Must be provided at the first call to partial_fit, can be omitted in subsequent calls.
sample_weightarray-like of shape (n_samples), default=None

Weights applied to individual samples (1. for unweighted).    Returns

selfobject

predict(X) [source]

Perform classification on an array of test vectors X.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Cndarray of shape (n_samples,)

Predicted target values for X

predict_log_proba(X) [source]

Return log-probability estimates for the test vector X.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Carray-like of shape (n_samples, n_classes)

Returns the log-probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.

predict_proba(X) [source]

Return probability estimates for the test vector X.  Parameters

Xarray-like of shape (n_samples, n_features)
  Returns

Carray-like of shape (n_samples, n_classes)

Returns the probability of the samples for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.

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) wrt. 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.