Summary
Number of targets | Target cardinality | Valid | |
---|---|---|---|
Multiclass classification | 1 | >2 |
|
Multilabel classification | >1 | 2 (0 or 1) |
|
Multioutput regression | >1 | Continuous |
|
Multioutput- multiclass classification | >1 | >2 |
|
Warning
All classifiers in scikit-learn do multiclass classification out-of-the-box. You don’t need to use the sklearn.multiclass
module unless you want to experiment with different multiclass strategies.
The sklearn.multiclass
module implements meta-estimators to solve multiclass
and multilabel
classification problems by decomposing such problems into binary classification problems. multioutput
regression is also supported.
-
Multiclass classification: classification task with more than two classes. Each sample can only be labelled as one class.
For example, classification using features extracted from a set of images of fruit, where each image may either be of an orange, an apple, or a pear. Each image is one sample and is labelled as one of the 3 possible classes. Multiclass classification makes the assumption that each sample is assigned to one and only one label - one sample cannot, for example, be both a pear and an apple.
Valid multiclass representations for
type_of_target
(y
) are:-
1d or column vector containing more than two discrete values. An example of a vector
>>>y
for 3 samples:>>> import numpy as np >>> y = np.array(['apple', 'pear', 'apple']) >>> print(y) ['apple' 'pear' 'apple']
-
sparse binary matrix of shape
>>>(n_samples, n_classes)
with a single element per row, where each column represents one class. An example of a sparse binary matrixy
for 3 samples, where the columns, in order, are orange, apple and pear:>>> from scipy import sparse >>> row_ind = np.array([0, 1, 2]) >>> col_ind = np.array([1, 2, 1]) >>> y_sparse = sparse.csr_matrix((np.ones(3), (row_ind, col_ind))) >>> print(y_sparse) (0, 1) 1.0 (1, 2) 1.0 (2, 1) 1.0
-
-
Multilabel classification: classification task labelling each sample with
x
labels fromn_classes
possible classes, wherex
can be 0 ton_classes
inclusive. This can be thought of as predicting properties of a sample that are not mutually exclusive. Formally, a binary output is assigned to each class, for every sample. Positive classes are indicated with 1 and negative classes with 0 or -1. It is thus comparable to runningn_classes
binary classification tasks, for example withsklearn.multioutput.MultiOutputClassifier
. This approach treats each label independently whereas multilabel classifiers may treat the multiple classes simultaneously, accounting for correlated behavior among them.For example, prediction of the topics relevant to a text document or video. The document or video may be about one of ‘religion’, ‘politics’, ‘finance’ or ‘education’, several of the topic classes or all of the topic classes.
A valid representation of multilabel
>>>y
is an either dense or sparse binary matrix of shape(n_samples, n_classes)
. Each column represents a class. The1
’s in each row denote the positive classes a sample has been labelled with. An example of a dense matrixy
for 3 samples:>>> y = np.array([[1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 0, 0]]) >>> print(y) [[1 0 0 1] [0 0 1 1] [0 0 0 0]]
An example of the same
>>>y
in sparse matrix form:>>> y_sparse = sparse.csr_matrix(y) >>> print(y_sparse) (0, 0) 1 (0, 3) 1 (1, 2) 1 (1, 3) 1
-
Multioutput regression: predicts multiple numerical properties for each sample. Each property is a numerical variable and the number of properties to be predicted for each sample is greater than or equal to 2. Some estimators that support multioutput regression are faster than just running
n_output
estimators.For example, prediction of both wind speed and wind direction, in degrees, using data obtained at a certain location. Each sample would be data obtained at one location and both wind speed and direction would be output for each sample.
A valid representation of multioutput
>>>y
is a dense matrix of shape(n_samples, n_classes)
of floats. A column wise concatenation of continuous variables. An example ofy
for 3 samples:>>> y = np.array([[31.4, 94], [40.5, 109], [25.0, 30]]) >>> print(y) [[ 31.4 94. ] [ 40.5 109. ] [ 25. 30. ]]
-
Multioutput-multiclass classification (also known as multitask classification): classification task which labels each sample with a set of non-binary properties. Both the number of properties and the number of classes per property is greater than 2. A single estimator thus handles several joint classification tasks. This is both a generalization of the multilabel classification task, which only considers binary attributes, as well as a generalization of the multiclass classification task, where only one property is considered.
For example, classification of the properties “type of fruit” and “colour” for a set of images of fruit. The property “type of fruit” has the possible classes: “apple”, “pear” and “orange”. The property “colour” has the possible classes: “green”, “red”, “yellow” and “orange”. Each sample is an image of a fruit, a label is output for both properties and each label is one of the possible classes of the corresponding property.
A valid representation of multioutput
>>>y
is a dense matrix of shape(n_samples, n_classes)
of class labels. A column wise concatenation of 1d multiclass variables. An example ofy
for 3 samples:>>> y = np.array([['apple', 'green'], ['orange', 'orange'], ['pear', 'green']]) >>> print(y) [['apple' 'green'] ['orange' 'orange'] ['pear' 'green']]
Note that all classifiers handling multioutput-multiclass (also known as multitask classification) tasks, support the multilabel classification task as a special case. Multitask classification is similar to the multioutput classification task with different model formulations. For more information, see the relevant estimator documentation.
All scikit-learn classifiers are capable of multiclass classification, but the meta-estimators offered by sklearn.multiclass
permit changing the way they handle more than two classes because this may have an effect on classifier performance (either in terms of generalization error or required computational resources).
Below is a summary of the classifiers supported by scikit-learn grouped by strategy; you don’t need the meta-estimators in this class if you’re using one of these, unless you want custom multiclass behavior:
-
Inherently multiclass:
-
sklearn.svm.LinearSVC
(setting multi_class=”crammer_singer”) -
sklearn.linear_model.LogisticRegression
(setting multi_class=”multinomial”) -
sklearn.linear_model.LogisticRegressionCV
(setting multi_class=”multinomial”)
-
Multiclass as One-Vs-One:
-
sklearn.gaussian_process.GaussianProcessClassifier
(setting multi_class = “one_vs_one”)
-
Multiclass as One-Vs-The-Rest:
-
sklearn.gaussian_process.GaussianProcessClassifier
(setting multi_class = “one_vs_rest”) -
sklearn.svm.LinearSVC
(setting multi_class=”ovr”) -
sklearn.linear_model.LogisticRegression
(setting multi_class=”ovr”) -
sklearn.linear_model.LogisticRegressionCV
(setting multi_class=”ovr”)
-
Support multilabel:
-
Support multiclass-multioutput:
Warning
At present, no metric in sklearn.metrics
supports the multioutput-multiclass classification task.
Others metrics
also work in the multiclass case:
| Compute the balanced accuracy |
| Cohen’s kappa: a statistic that measures inter-annotator agreement. |
| Compute confusion matrix to evaluate the accuracy of a classification. |
| Average hinge loss (non-regularized) |
| Compute the Matthews correlation coefficient (MCC) |
| Compute Area Under the Receiver Operating Characteristic Curve (ROC AUC) from prediction scores. |