Skip to content

metrics.log_loss fails when any classes are missing in y_true #4033

Closed

Description

@ebenolson

When calling log_loss with a label array (i.e. not an indicator matrix) for y_true, it uses a LabelBinarizer to construct the indicator matrix.

If not all classes in y_pred are present in y_true, this has the wrong shape, and it raises
ValueError: y_true and y_pred have different number of classes

Perhaps I don't understand the intended use of this mode, but it seems like it would work better to infer the indicator matrix from the shape of y_pred, as this causes brittleness where things appear to work until a batch of samples happens to not include every class.

Activity

  1. amueller commented on Dec 30, 2014

    @amueller
    Member

    I agree, this seems brittle. A problem is that it is not clear which class is missing. If your classes are ["elephant", "tiger", "rhino"] but your y_true only contains ["elephant", "tiger"] it can not work.

    The solution we use in other places is a classes parameter for the loss where you can provide all known classes.

  2. added
    Easy
    on Dec 30, 2014
  3. amueller commented on Dec 30, 2014

    @amueller
    Member

    This is a general problem for "ranking"-style metrics on multi-class tasks. I'm not sure what the current status is, but I'm sure @arjoly or @mblondel know.

  4. removed
    Easy
    on Dec 30, 2014
  5. birdkat commented on Mar 28, 2016

    @birdkat

    Does it be solved? I also meet the problem~~~

  6. xiapoy commented on Mar 28, 2016

    @xiapoy

    Same here

  7. zygmuntz commented on Mar 28, 2016

    @zygmuntz

    Bump. That's a stinky bug.

  8. rortms commented on Apr 20, 2016

    @rortms

    I'm also running up against this.

  9. jnothman commented on Apr 25, 2016

    @jnothman
    Member

    @amueller, I don't think we have any other multiclass ranking metrics.
    log_loss's second argument should be called y_score, not y_pred, to be
    clear and consistent, too. There is no classes parameter in any metrics
    atm, though the labels parameter to the precision/recall/fscore family
    serves a similar purpose if, for instance, you're doing macro-averages, and
    the current CV sample lacks a particular class (which is a pretty bad
    situation to use macro averaging in, but whatevs). However, classes_ is a
    standard attribute elsewhere and corresponds to what we're talking about.

    A PR to support a classes/labels parameter is very welcome. It may
    involve changing LabelBinarizer. Hopefully by the time it is merged we'll
    have decided the appropriate parameter name!

    On 25 April 2016 at 10:27, soliz [email protected] wrote:

    Just got that error :) Fun
    ValueError: y_true and y_pred have different number of classes 5, 2

    —
    You are receiving this because you are subscribed to this thread.
    Reply to this email directly or view it on GitHub
    #4033 (comment)

  10. jnothman commented on Dec 9, 2016

    @jnothman
    Member
  11. taposh commented on Feb 19, 2017

    @taposh

    I am using 0.18.1 and have an erorr,

    ValueError: y_true contains only one label (2). Please provide the true labels explicitly through the labels argument.

    #Visualize Log Loss when True value = 1
    #y-axis is log loss, x-axis is probabilty that label = 1
    #As you can see Log Loss increases rapidly as we approach 0
    #But increases slowly as our predicted probability gets closer to 1
    import matplotlib.pyplot as plt
    import numpy as np
    from sklearn.metrics import log_loss
    
    x = [i*.0001 for i in range(1,10000)]
    y = [log_loss(y_true=[1],y_pred=[[i*.0001,1-(i*.0001)]],eps=1e-15,labels=None) for i in range(1,10000,1)]
    
    plt.plot(x, y)
    plt.axis([-.05, 1.1, -.8, 10])
    plt.title("Log Loss when true label = 1")
    plt.xlabel("predicted probability")
    plt.ylabel("log loss")
    
    plt.show()
    
  12. jnothman commented on Feb 21, 2017

    @jnothman
    Member

    Then do what it says...? Provide the labels explicitly so that it knows which aligns to which column.

  13. snassimr commented on Mar 8, 2017

    @snassimr

    I am still getting :

    ValueError: The number of classes in labels is different from that in y_pred. Classes found in labels: [0 1 2]

    If predict_proba is working appropriately ?

  14. jnothman commented on Mar 9, 2017

    @jnothman
    Member

    Please give a fully reproducible code snippet. Thanks.

  15. snassimr commented on Mar 9, 2017

    @snassimr

    It was my mistake . I contrived to cut the probability vector during passin it to log_loss.
    Sorry and very thank for your work.

  16. kirk86 commented on Apr 27, 2017

    @kirk86

    log_loss also doesn't support multiclass targets, especially when you have one-hot labels

  17. jkginfinite commented on Aug 1, 2018

    @jkginfinite

    The reason for the error has to do with the math. If there there are no members of a certain class of present, then it's probability is zero. A perfect example would be a situation where both sets are all zero. Log loss depends upon the logarithm of the probability, and if the probability is zero, then the logarithm returns an error, because log(0) is undefined.

    The way to deal with this problem is by using try except statements:

    def log_loss1(a,b): try: k = log_loss(a,b) except: k = np.nan return k

  18. ebenolson commented on Aug 1, 2018

    @ebenolson
    Author

    No, the logs are calculated on the predicted probabilities, the bug here was when a certain class was missing in the true labels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions