8000 DOC improve doc for multiclass and types_of_target (#15333) · ogrisel/scikit-learn@1237e40 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1237e40

Browse files
lucyleeowogrisel
authored andcommitted
DOC improve doc for multiclass and types_of_target (scikit-learn#15333)
1 parent e547c90 commit 1237e40

File tree

1 file changed

+155
-42
lines changed

1 file changed

+155
-42
lines changed

doc/modules/multiclass.rst

Lines changed: 155 additions & 42 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,165 @@ Multiclass and multilabel algorithms
1414

1515
The :mod:`sklearn.multiclass` module implements *meta-estimators* to solve
1616
``multiclass`` and ``multilabel`` classification problems
17-
by decomposing such problems into binary classification problems. Multitarget
17+
by decomposing such problems into binary classification problems. ``multioutput``
1818
regression is also supported.
1919

20-
- **Multiclass classification** means a classification task with more than
21-
two classes; e.g., classify a set of images of fruits which may be oranges,
22-
apples, or pears. Multiclass classification makes the assumption that each
23-
sample is assigned to one and only one label: a fruit can be either an
24-
apple or a pear but not both at the same time.
25-
26-
- **Multilabel classification** assigns to each sample a set of target
27-
labels. This can be thought as predicting properties of a data-point
28-
that are not mutually exclusive, such as topics that are relevant for a
29-
document. A text might be about any of religion, politics, finance or
30-
education at the same time or none of these.
31-
32-
- **Multioutput regression** assigns each sample a set of target
33-
values. This can be thought of as predicting several properties
34-
for each data-point, such as wind direction and magnitude at a
35-
certain location.
36-
37-
- **Multioutput-multiclass classification** and **multi-task classification**
38-
means that a single estimator has to handle several joint classification
39-
tasks. This is both a generalization of the multi-label classification
40-
task, which only considers binary classification, as well as a
41-
generalization of the multi-class classification task. *The output format
42-
is a 2d numpy array or sparse matrix.*
43-
44-
The set of labels can be different for each output variable.
45-
For instance, a sample could be assigned "pear" for an output variable that
46-
takes possible values in a finite set of species such as "pear", "apple";
47-
and "blue" or "green" for a second output variable that takes possible values
48-
in a finite set of colors such as "green", "red", "blue", "yellow"...
49-
50-
This means that any classifiers handling multi-output
51-
multiclass or multi-task classification tasks,
52-
support the multi-label classification task as a special case.
53-
Multi-task classification is similar to the multi-output
54-
classification task with different model formulations. For
55-
more information, see the relevant estimator documentation.
20+
- **Multiclass classification**: classification task with more than two classes.
21+
Each sample can only be labelled as one class.
22+
23+
For example, classification using features extracted from a set of images of
24+
fruit, where each image may either be of an orange, an apple, or a pear.
25+
Each image is one sample and is labelled as one of the 3 possible classes.
26+
Multiclass classification makes the assumption that each sample is assigned
27+
to one and only one label - one sample cannot, for example, be both a pear
28+
and an apple.
29+
30+
Valid :term:`multiclass` representations for
31+
:func:`~utils.multiclass.type_of_target` (`y`) are:
32+
33+
- 1d or column vector containing more than two discrete values. An
34+
example of a vector ``y`` for 3 samples:
35+
36+
>>> import numpy as np
37+
>>> y = np.array(['apple', 'pear', 'apple'])
38+
>>> print(y)
39+
['apple' 'pear' 'apple']
40+
41+
- sparse :term:`binary` matrix of shape ``(n_samples, n_classes)`` with a
42+
single element per row, where each column represents one class. An
43+
example of a sparse :term:`binary` matrix ``y`` for 3 samples, where
44+
the columns, in order, are orange, apple and pear:
45+
46+
>>> from scipy import sparse
47+
>>> row_ind = np.array([0, 1, 2])
48+
>>> col_ind = np.array([1, 2, 1])
49+
>>> y_sparse = sparse.csr_matrix((np.ones(3), (row_ind, col_ind)))
50+
>>> print(y_sparse)
51+
(0, 1) 1.0
52+
(1, 2) 1.0
53+
(2, 1) 1.0
54+
55+
56+
- **Multilabel classification**: classification task labelling each sample with
57+
``x`` labels from ``n_classes`` possible classes, where ``x`` can be 0 to
58+
``n_classes`` inclusive. This can be thought of as predicting properties of a
59+
sample that are not mutually exclusive. Formally, a binary output is assigned
60+
to each class, for every sample. Positive classes are indicated with 1 and
61+
negative classes with 0 or -1. It is thus comparable to running ``n_classes``
62+
binary classification tasks, for example with
63+
:class:`sklearn.multioutput.MultiOutputClassifier`. This approach treats
64+
each label independently whereas multilabel classifiers *may* treat the
65+
multiple classes simultaneously, accounting for correlated behaviour amoung
66+
them.
67+
68+
For example, prediction of the topics relevant to a text document or video.
69+
The document or video may be about one of 'religion', 'politics', 'finance'
70+
or 'education', several of the topic classes or all of the topic classes.
71+
72+
Valid representation of :term:`multilabel` `y` is either dense (or sparse)
73+
:term:`binary` matrix of shape ``(n_samples, n_classes)``. Each column
74+
represents a class. The ``1``'s in each row denote the positive classes a
75+
sample has been labelled with. An example of a dense matrix ``y`` for 3
76+
samples:
77+
78+
>>> y = np.array([[1, 0, 0, 1], [0, 0, 1, 1], [0, 0, 0, 0]])
79+
>>> print(y)
80+
[[1 0 0 1]
81+
[0 0 1 1]
82+
[0 0 0 0]]
83+
84+
An example of the same ``y`` in sparse matrix form:
85+
86+
>>> y_sparse = sparse.csr_matrix(y)
87+
>>> print(y_sparse)
88+
(0, 0) 1
89+
(0, 3) 1
90+
(1, 2) 1
91+
(1, 3) 1
92+
93+
94+
- **Multioutput regression**: predicts multiple numerical properties for each
95+
sample. Each property is a numerical variable and the number of properties
96+
to be predicted for each sample is greater than or equal to 2. Some estimators
97+
that support multioutput regression are faster than just running ``n_output``
98+
estimators.
99+
100+
For example, prediction of both wind speed and wind direction, in degrees,
101+
using data obtained at a certain location. Each sample would be data
102+
obtained at one location and both wind speed and directtion would be
103+
output for each sample.
104+
105+
Valid representation of :term:`multilabel` `y` is dense matrix of shape
106+
``(n_samples, n_classes)`` of floats. A column wise concatenation of
107+
:term:`continuous` variables. An example of ``y`` for 3 samples:
108+
109+
>>> y = np.array([[31.4, 94], [40.5, 109], [25.0, 30]])
110+
>>> print(y)
111+
[[ 31.4 94. ]
112+
[ 40.5 109. ]
113+
[ 25. 30. ]]
114+
115+
116+
- **Multioutput-multiclass classification**
117+
(also known as **multitask classification**):
118+
classification task which labels each sample with a set of **non-binary**
119+
properties. Both the number of properties and the number of
120+
classes per property is greater than 2. A single estimator thus
121+
handles several joint classification tasks. This is both a generalization of
122+
the multi\ *label* classification task, which only considers binary
123+
attributes, as well as a generalization of the multi\ *class* classification
124+
task, where only one property is considered.
125+
126+
For example, classification of the properties "type of fruit" and "colour"
127+
for a set of images of fruit. The property "type of fruit" has the possible
128+
classes: "apple", "pear" and "orange". The property "colour" has the
129+
possible classes: "green", "red", "yellow" and "orange". Each sample is an
130+
image of a fruit, a label is output for both properties and each label is
131+
one of the possible classes of the corresponding property.
132+
133+
Valid representation of :term:`multilabel` `y` is dense matrix of shape
134+
``(n_samples, n_classes)`` of floats. A column wise concatenation of 1d
135+
:term:`multiclass` variables. An example of ``y`` for 3 samples:
136+
137+
>>> y = np.array([['apple', 'green'], ['orange', 'orange'], ['pear', 'green']])
138+
>>> print(y)
139+
[['apple' 'green']
140+
['orange' 'orange']
141+
['pear' 'green']]
142+
143+
Note that any classifiers handling multioutput-multiclass (also known as
144+
multitask classification) tasks, support the multilabel classification task
145+
as a special case. Multitask classification is similar to the multioutput
146+
classification task with different model formulations. For more information,
147+
see the relevant estimator documentation.
148+
56149

57150
All scikit-learn classifiers are capable of multiclass classification,
58151
but the meta-estimators offered by :mod:`sklearn.multiclass`
59152
permit changing the way they handle more than two classes
60153
because this may have an effect on classifier performance
61154
(either in terms of generalization error or required computational resources).
62155

156+
**Summary**
157+
158+
+-----------------+-------------+-------------+------------------------------------------+
159+
| | Number of | Target | Valid |
160+
| | targets | cardinality | :func:`~utils.multiclass.type_of_target` |
161+
+=================+=============+=============+==========================================+
162+
| Multiclass | 1 | >2 | - 'multiclass' |
163+
| classification | | | |
164+
+-----------------+-------------+-------------+------------------------------------------+
165+
| Multilabel | >1 | 2 (0 or 1) | - 'multilabel-indicator' |
166+
| classification | | | |
167+
+-----------------+-------------+-------------+------------------------------------------+
168+
| Multioutput | >1 | Continuous | - 'continuous-multioutput' |
169+
| regression | | | |
170+
+-----------------+-------------+-------------+------------------------------------------+
171+
| Multioutput- | >1 | >2 | - 'multiclass-multioutput' |
172+
| multiclass | | | |
173+
| classification | | | |
174+
+-----------------+-------------+-------------+------------------------------------------+
175+
63176
Below is a summary of the classifiers supported by scikit-learn
64177
grouped by strategy; you don't need the meta-estimators in this class
65178
if you're using one of these, unless you want custom multiclass behavior:
@@ -94,7 +207,7 @@ if you're using one of these, unless you want custom multiclass behavior:
94207
- :class:`sklearn.gaussian_process.GaussianProcessClassifier` (setting multi_class = "one_vs_one")
95208

96209

97-
- **Multiclass as One-Vs-All:**
210+
- **Multiclass as One-Vs-The-Rest:**
98211

99212
- :class:`sklearn.ensemble.GradientBoostingClassifier`
100213
- :class:`sklearn.gaussian_process.GaussianProcessClassifier` (setting multi_class = "one_vs_rest")
@@ -167,7 +280,7 @@ This strategy, also known as **one-vs-all**, is implemented in
167280
per class. For each classifier, the class is fitted against all the other
168281
classes. In addition to its computational efficiency (only `n_classes`
169282
classifiers are needed), one advantage of this approach is its
170-
interpretability. Since each class is represented by one and only one classifier,
283+
interpretability. Since each class is represented by one and only one classifier,
171284
it is possible to gain knowledge about the class by inspecting its
172285
corresponding classifier. This is the most commonly used strategy and is a fair
173286
default choice.
@@ -431,7 +544,7 @@ averaged together.
431544
Regressor Chain
432545
================
433546

434-
Regressor chains (see :class:`RegressorChain`) is analogous to
435-
ClassifierChain as a way of combining a number of regressions
436-
into a single multi-target model that is capable of exploiting
547+
Regressor chains (see :class:`RegressorChain`) is analogous to
548+
ClassifierChain as a way of combining a number of regressions
549+
into a single multi-target model that is capable of exploiting
437550
correlations among targets.

0 commit comments

Comments
 (0)
0