You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
multiclass jaccard_similarity_score should not be equal to accuracy_score #7332
The documentation for sklearn.metrics.jaccard_similarity_score currently (version 0.17.1) states that:
In binary and multiclass classification, this function is equivalent to the accuracy_score. It differs in the multilabel classification problem.
However, I do not think that this is the right thing to do for multiclass-problems. As far as I can tell, within the machine learning community a more common usage of the Jaccard index for multi-class is to
use the mean Jaccard-Index calculated for each class indivually. i.e., first calculate the jaccard index for class 0, class 1 and class 2, and then average them. This is what is very commonly done in the image segmentation community (where this is referred to as the "mean Intersection over Union" score (see e.g.[1]), but as far as I can tell by skimming it, this is also what the original publication of the jaccard index did in multiclass scenarios [2]. Note that this is NOT the same as the accuracy_score. Consider this example:
y_true = [0, 1, 2]
y_pred = [0, 0, 0]
The accuracy is clearly 1/3, and this is also what the jaccard_score in sklearn currently returns. The class-specific jaccard_scores would be:
J0 = 1 /3
J1 = 0 / 1
J2 = 0 / 1
Thus IMO the jaccard_score should be (J0 + J1 + J2) / 3 = 1/9 in this case
@untom The Pascal VOC is multi-class multi-label, right? Pixels are not evaluated individually, but the whole image is. And then people usually look at per-class measures.
Currently jaccard_similarity_score just counts the samples in intersection of pred and true, which is not correct for the definition of Jaccard similarity coefficient. We need to calculate union.
I agree that this seems to be strange even for the binary case. I would have thought Jaccard is an alternative to precision or recall or F1 (= Dice coefficient) in evaluating performance, in the binary case, on a single positive class, i.e. "true positives / (true positives + false positives + false negatives)". In particular, the binary implementation in our case does not seem to equate to the multilabel implementation run over a single class.
Regarding @untom's initial contention that multiclass implementation is incorrect, I agree that the multiclass implementation is useless. I don't think that the macro averaging he suggests is the only way to go about it, and as with P/R/F, micro-averaging excluding a majority negative class is still meaningful; weighted macro-average may also be feasible.
So yes, multiple strange things in our jaccard implementation IMO, and at a glance I don't see how the reference given in #1795 tells us about the multiclass case.
@jacobdang polite ping. Are you still working? It is very much fine if you are still working but if you are not, then this is looks like a really good issue I would like to work on.
Unfortunately the code there is quite complicated, and perhaps could be simplified if something like #10628 existed, but I haven't had time to make that work let alone bring it up to mergeable standard.
This problem in scikit-learn has just recently caused some big headache for me in my research.
I work with segmentation and was surprised to see how scikit-learn interprets the jaccard index metric.
@TSchattschneider I feel you. I remember how frustrated I was. I had the same problem. This bug should be flagged in the documentation, unless is fixed.
The feature was obviously proposed by someone interested in the multilabel
case and should have been reviewed with more diligence.
Sorry the fix has not been prioritised. #10083 is ready for review, but
core developer review time is often hard to get.
Activity
amueller commentedon Sep 14, 2016
@untom The Pascal VOC is multi-class multi-label, right? Pixels are not evaluated individually, but the whole image is. And then people usually look at per-class measures.
But you're right, it looks like the original definition is different from ours, see https://encrypted.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&cad=rja&uact=8&ved=0ahUKEwjT1-XGvY_PAhXGeD4KHSFLBUAQFggwMAI&url=http%3A%2F%2Fwww.informatica.si%2Findex.php%2Finformatica%2Farticle%2Fdownload%2F753%2F608&usg=AFQjCNFd6KF03h8j6Yfk_6hQbx6oeBZP8g&sig2=1OOs_6BFsyaPcxGvPbEtLQ
Where is it in the original paper?
hccheng commentedon Oct 27, 2016
According to the evaluation code (http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCdevkit_18-May-2011.tar), the multi-class VOC score is the average of per-class Jaccard scores.
The relevant part is from line 74 to line 90 in VOCevalseg.m
shiba24 commentedon Feb 21, 2017
@untom hi, i think you are right.
Currently jaccard_similarity_score just counts the samples in intersection of pred and true, which is not correct for the definition of Jaccard similarity coefficient. We need to calculate union.
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/metrics/classification.py#L174
I am not sure what it looks like for multi label.
jnothman commentedon Feb 21, 2017
I agree that this seems to be strange even for the binary case. I would have thought Jaccard is an alternative to precision or recall or F1 (= Dice coefficient) in evaluating performance, in the binary case, on a single positive class, i.e. "true positives / (true positives + false positives + false negatives)". In particular, the binary implementation in our case does not seem to equate to the multilabel implementation run over a single class.
Regarding @untom's initial contention that multiclass implementation is incorrect, I agree that the multiclass implementation is useless. I don't think that the macro averaging he suggests is the only way to go about it, and as with P/R/F, micro-averaging excluding a majority negative class is still meaningful; weighted macro-average may also be feasible.
So yes, multiple strange things in our jaccard implementation IMO, and at a glance I don't see how the reference given in #1795 tells us about the multiclass case.
Labelling this a bug.
jnothman commentedon Oct 21, 2017
Yes, probably. PR welcome
gxyd commentedon Oct 26, 2017
@jacobdang polite ping. Are you still working? It is very much fine if you are still working but if you are not, then this is looks like a really good issue I would like to work on.
multiclass jaccard similarity not equal to accurary_score
dimimal commentedon Mar 6, 2018
Any progress about this issue yet?
jnothman commentedon Mar 6, 2018
Waiting for a second review at #10083
jnothman commentedon Mar 6, 2018
Unfortunately the code there is quite complicated, and perhaps could be simplified if something like #10628 existed, but I haven't had time to make that work let alone bring it up to mergeable standard.
agamemnonc commentedon Jul 20, 2018
So, is the Jaccard similarity score a valid metric to use for multilabel classification problems?
jnothman commentedon Jul 22, 2018
dimimal commentedon Jul 26, 2018
@agamemnonc Only if you want the average score across the samples. It is totally different than intersection over union.
jnothman commentedon Jul 26, 2018
TSchattschneider commentedon Feb 4, 2019
This problem in scikit-learn has just recently caused some big headache for me in my research.
I work with segmentation and was surprised to see how scikit-learn interprets the jaccard index metric.
dimimal commentedon Feb 4, 2019
@TSchattschneider I feel you. I remember how frustrated I was. I had the same problem. This bug should be flagged in the documentation, unless is fixed.
jnothman commentedon Feb 4, 2019
Fix _classification.py documentation