Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
ae594b0
ENH + TST generalized and test unique_labels
arjoly Jan 21, 2013
ae2cd16
ENH extend zero_one_loss to multilabel output
arjoly Jan 21, 2013
c4ab9ae
ENH add hamming_loss metrics
arjoly Jan 21, 2013
4b273f1
TST add representation and shuffling invariance
arjoly Jan 22, 2013
0e98b2f
COSMIT + TST more test for Hamming loss
arjoly Jan 22, 2013
9bbc62d
ENH add multi-label support for accuracy_score
arjoly Jan 22, 2013
f44935f
TST hamming loss labels inference
arjoly Jan 22, 2013
f64b1c8
COSMIT pep8
arjoly Jan 22, 2013
6d23fa1
DOC now there is multilabels metrics
arjoly Jan 22, 2013
2c88b71
Update what's new
arjoly Jan 22, 2013
130fb34
DOC improve parameter description
arjoly Jan 22, 2013
d548827
FIX missing import in doctest
arjoly Jan 22, 2013
c76a70a
TST more tests for unique_labels
arjoly Jan 22, 2013
95bba46
DOC rename label binary matrix to label indicator matrix
arjoly Jan 22, 2013
a2b63bf
What's new : add credit
arjoly Jan 23, 2013
0d6e908
COSMIT less decorative comment
arjoly Jan 23, 2013
7d8eb48
ENH unique_classes is now a function of the multiclass module
arjoly Jan 23, 2013
6e09ff1
DOC add example and a small narrative doc
arjoly Jan 23, 2013
13a8573
ENH (corner case) + DOC + TST for is_multilabel and is_label_indicato…
arjoly Jan 23, 2013
65cd20c
ENH create a sklearn.utils.multiclass module
arjoly Jan 23, 2013
12ed2af
DOC add some see also + hamming loss vs zero-one loss + note in multi…
arjoly Jan 24, 2013
6e43ab5
TST representation invariance redundant labels
arjoly Jan 24, 2013
c730112
TST reduce test time
arjoly Jan 24, 2013
0538ff0
TST add invariance with empty label
arjoly Jan 25, 2013
7d5b1a4
TST symetry + refactor symetry test
arjoly Jan 25, 2013
d6f4c6d
TST add test for sample order invariance
arjoly Jan 25, 2013
66a8d04
DOC improve doc on multilabel case
arjoly Jan 25, 2013
0479836
DOC hamming loss is upperbounded by the zero-one loss
arjoly Jan 25, 2013
e56d730
DOC beautifull references + some see also
arjoly Jan 25, 2013
bfafe33
COSMIT simpler hamming loss in lael indicator matrix
arjoly Jan 25, 2013
48b98ae
COSMIT remove unused import
arjoly Feb 4, 2013
7b279c0
TST make some works only once
arjoly Feb 4, 2013
158544e
TST informative assertion msg in invariance test
arjoly Feb 4, 2013
20dc1bc
In multiclass case, the hamming_loss compute the hamming distance btw…
arjoly Feb 4, 2013
47525be
ENH handle mix input multilabel representation
arjoly Feb 4, 2013
6cfa0e6
TST more robust invariance tests to floating point epsilon
arjoly Feb 4, 2013
a5710ca
DOC correct typo and improve example
arjoly Feb 5, 2013
5636273
TST simplify shuffling
arjoly Feb 5, 2013
135f2f5
DOC TST better inline comment
arjoly Feb 5, 2013
4e3fae7
ENH consistency (classes args instead of labels)
arjoly Feb 5, 2013
462ef50
ENH revert renaming accident
arjoly Feb 5, 2013
cdf9c3f
DOC + FIX specify supported format and fix a bug
arjoly Feb 5, 2013
7f6a266
DOC FIX remaining attribute name change
arjoly Feb 13, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/developers/utilities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ Testing Functions
- :func:`testing.all_estimators` : returns a list of all estimators in
sklearn to test for consistent behavior and interfaces.

Multiclass and multilabel utility function
==========================================

- :func:`multiclass.is_multilabel`: Helper function to check if the task
is a multi-label classification one.

- :func:`multiclass.is_label_indicator_matrix`: Helper function to check if
a classification output is in label indicator matrix format.

- :func:`multiclass.unique_labels`: Helper function to extract an ordered
array of unique labels from a list of labels.


Helper Functions
================
Expand Down
2 changes: 1 addition & 1 deletion doc/modules/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ Classification metrics
metrics.confusion_matrix
metrics.f1_score
metrics.fbeta_score
metrics.hamming_loss
metrics.hinge_loss
metrics.matthews_corrcoef
metrics.precision_recall_curve
Expand Down Expand Up @@ -819,7 +820,6 @@ Pairwise metrics
multiclass.fit_ecoc
multiclass.predict_ecoc


.. _naive_bayes_ref:

:mod:`sklearn.naive_bayes`: Naive Bayes
Expand Down
93 changes: 87 additions & 6 deletions doc/modules/model_evaluation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,24 @@ Others also work in the multiclass case:
.. autosummary::
:template: function.rst

accuracy_score
classification_report
confusion_matrix
f1_score
fbeta_score
precision_recall_fscore_support
precision_score
recall_score

And some also work in the multilabel case:

.. autosummary::
:template: function.rst

accuracy_score
hamming_loss
zero_one_loss


Some metrics might require probability estimates of the positive class,
confidence values or binary decisions value.

Expand All @@ -64,7 +72,8 @@ Accuracy score
---------------
The :func:`accuracy_score` function computes the
`accuracy <http://en.wikipedia.org/wiki/Accuracy_and_precision>`_, the fraction
of correct predictions.
of correct predictions. In multilabel classification, the accuracy is also
called subset accuracy (which is different of label-based accuracy).

If :math:`\hat{y}_i` is the predicted value of
the :math:`i`-th sample and :math:`y_i` is the corresponding true value,
Expand All @@ -73,17 +82,28 @@ defined as

.. math::

\texttt{accuracy}(y, \hat{y}) = \frac{1}{n_\text{samples}} \sum_{i=0}^{n_\text{samples}-1} 1(\hat{y} = y)
\texttt{accuracy}(y, \hat{y}) = \frac{1}{n_\text{samples}} \sum_{i=0}^{n_\text{samples}-1} 1(\hat{y}_i = y_i)

where :math:`1(x)` is the `indicator function
<http://en.wikipedia.org/wiki/Indicator_function>`_.

>>> import numpy as np
>>> from sklearn.metrics import accuracy_score
>>> y_pred = [0, 2, 1, 3]
>>> y_true = [0, 1, 2, 3]
>>> accuracy_score(y_true, y_pred)
0.5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add some text in between saying "and in the multi-label case with indicator matrices" and "with lists". As someone not very familiar with the interface might be a bit taken aback.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


In the multilabel case with binary indicator format:

>>> accuracy_score(np.array([[0.0, 1.0], [1.0, 1.0]]), np.zeros((2, 2)))
0.0

and with a list of labels format:

>>> accuracy_score([(1, 2), (3,)], [(1, 2), tuple()])
0.5

.. topic:: Example:

* See :ref:`example_plot_permutation_test_for_classification.py`
Expand Down Expand Up @@ -207,6 +227,54 @@ and infered labels:
for an example of classification report usage in parameter estimation using
grid search with a nested cross-validation.

Hamming loss
------------
The :func:`hamming_loss` computes the average Hamming loss or `Hamming
distance <http://en.wikipedia.org/wiki/Hamming_distance>`_ between two sets
of samples.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe say that this only applies to multi-label or multi-output prediction (does the current implementation work for multi-output?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't handle multi-output of multi-class / multi-labels target, except if the multi-output problem is in the binary indicator format.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, then maybe also say this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, the problem is a multi-label one.


If :math:`\hat{y}_j` is the predicted value for the :math:`j`-th labels of
a given sample, :math:`y_j` is the corresponding true value and
:math:`n_\text{labels}` is the number of class or labels, then the
Hamming loss :math:`L_{Hamming}` between two samples is defined as:

.. math::

L_{Hamming}(y, \hat{y}) = \frac{1}{n_\text{labels}} \sum_{j=0}^{n_\text{labels} - 1} 1(\hat{y}_j \not= y_j)

where :math:`1(x)` is the `indicator function
<http://en.wikipedia.org/wiki/Indicator_function>`_.

>>> from sklearn.metrics import hamming_loss
>>> y_pred = [1, 2, 3, 4]
>>> y_true = [2, 2, 3, 4]
>>> hamming_loss(y_true, y_pred)
0.25

In the multilabel case with binary indicator format:

>>> hamming_loss(np.array([[0.0, 1.0], [1.0, 1.0]]), np.zeros((2, 2)))
0.75

and with a list of labels format:

>>> hamming_loss([(1, 2), (3,)], [(1, 2), tuple()]) # doctest: +ELLIPSIS
0.166...

.. note::

In multiclass classification, the Hamming loss correspond to the Hamming
distance between ``y_true`` and ``y_pred`` which is equivalent to the
:ref:`zero_one_loss` function.

In multilabel classification, the Hamming loss is different from the
zero-one loss. The zero-one loss penalizes any predications that don't
predict correctly the subset of labels. The Hamming loss penalizes only
the fraction of labels incorrectly predicted.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remark here: I really don't understand what is meant by "the subset of labels".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, I get it now. I'm going to rephrase this and other docstrings.


The Hamming loss is upperbounded by the zero one loss. With the
normalization over the samples, the Hamming loss is always between 0 and 1.


.. _precision_recall_f_measure_metrics:

Expand Down Expand Up @@ -331,9 +399,9 @@ Here some small examples in binary classification::
array([ 0.35, 0.4 , 0.8 ])


Multiclass and multilabels classification
Multiclass and multilabel classification
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
In multiclass and multilabels classification task, the notions of precision,
In multiclass and multilabel classification task, the notions of precision,
recall and F-measures can be applied to each label independently.

Moreover, these notions can be further extended. The functions
Expand Down Expand Up @@ -599,13 +667,16 @@ classification loss (:math:`L_{0-1}`) over :math:`n_{\text{samples}}`. By
defaults, the function normalizes over the sample. To get the sum of the
:math:`L_{0-1}`, set ``normalize`` to ``False``.

In multilabel classification, the :func:`zero_one_loss` function corresponds
to the subset zero one loss: the subset of labels must be correctly predict.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get this sentence.


If :math:`\hat{y}_i` is the predicted value of
the :math:`i`-th sample and :math:`y_i` is the corresponding true value,
then the 0-1 loss :math:`L_{0-1}` is defined as:

.. math::

L_{0-1}(y_i, \hat{y}_i) = 1(\hat{y} \not= y)
L_{0-1}(y_i, \hat{y}_i) = 1(\hat{y}_i \not= y_i)

where :math:`1(x)` is the `indicator function
<http://en.wikipedia.org/wiki/Indicator_function>`_.
Expand All @@ -618,6 +689,16 @@ where :math:`1(x)` is the `indicator function
>>> zero_one_loss(y_true, y_pred, normalize=False)
1

In the multilabel case with binary indicator format:

>>> zero_one_loss(np.array([[0.0, 1.0], [1.0, 1.0]]), np.zeros((2, 2)))
1.0

and with a list of labels format:

>>> zero_one_loss([(1, 2), (3,)], [(1, 2), tuple()])
0.5

.. topic:: Example:

* See :ref:`example_plot_rfe_with_cross_validation.py`
Expand Down
12 changes: 5 additions & 7 deletions doc/modules/multiclass.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ improves.
:class:`sklearn.linear_model.SGDClassifier`,
:class:`sklearn.linear_model.RidgeClassifier`.

.. note::

At the moment there are no evaluation metrics implemented for multilabel
learnings.
The :mod:`sklearn.utils.multiclass` module contains usefull functions
when working with multiclass and multilabel problem.


One-Vs-The-Rest
Expand Down Expand Up @@ -176,11 +174,11 @@ Example::
.. topic:: References:

.. [1] "Solving multiclass learning problems via error-correcting ouput codes",
Dietterich T., Bakiri G.,
Journal of Artificial Intelligence Research 2,
Dietterich T., Bakiri G.,
Journal of Artificial Intelligence Research 2,
1995.

.. [2] "The error coding method and PICTs",
.. [2] "The error coding method and PICTs",
James G., Hastie T.,
Journal of Computational and Graphical statistics 7,
1998.
Expand Down
8 changes: 6 additions & 2 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
Changelog
---------

- Hyperlinks to documentation in example code on the website by
`Martin Luessi`_.

- :class:`grid_search.GridSearchCV` and
:func:`cross_validation.cross_val_score` now support the use of advanced
scoring function such as area under the ROC curve and f-beta scores.
Expand All @@ -31,8 +34,9 @@ Changelog
sparse matrix, meaning stored models trained using these estimators
can be made much more compact.

- Hyperlinks to documentation in example code on the website by
`Martin Luessi`_.
- :func:`metrics.accuracy_score`, :func:`metrics.zero_one_loss` support
multi-label classification and a new metric :func:`metrics.hamming_loss`
is added with multi-label support by `Arnaud Joly`_.


.. _changes_0_13_1:
Expand Down
2 changes: 2 additions & 0 deletions sklearn/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
confusion_matrix,
explained_variance_score,
f1_score,
hamming_loss,
fbeta_score,
hinge_loss,
matthews_corrcoef,
Expand Down Expand Up @@ -61,6 +62,7 @@
'explained_variance_score',
'f1_score',
'fbeta_score',
'hamming_loss',
'hinge_loss',
'homogeneity_completeness_v_measure',
'homogeneity_score',
Expand Down
Loading