Skip to content

Problems accepting pandas.SparseSeries as target #7352

Closed

Description

@jnothman

At #3864 (comment), @nielsenmarkus11 raised an issue where roc_auc_score returned strange results where its y_true was a pandas.SparseSeries.

The following is a sufficient demonstrator of some weird behaviour:

import pandas as pd
import numpy as np
from sklearn.metrics import roc_auc_score, roc_curve

r =  np.random.RandomState(0).rand(5)
print(roc_curve(pd.SparseSeries([1,0,0,0,1]), r))
print(roc_auc_score(pd.SparseSeries([1,0,0,0,1]), r))

for this value of r, roc_auc_score claims that y_true is constant. For other values of r the returned score is > 1.

A few points:

  • Users are unlikely to be making substantial memory savings when evaluating roc_auc_score with a sparse y_true. After all, the scores are dense so will occupy at least as much memory as one is attempting to save with a sparse structure.
  • Our metrics and estimators were not intended for and have not been tested with SparseSeries. We should, initially, raise an error when they are passed. (Contributor welcome.)
  • The current problem seems to come down to the fact that np.array(some_sparse_series) only returns the explicit data, i.e. no zeroes.
  • There is also quirky behaviour involving sparse series of non-floats: pd.SparseSeries([True, False, False, False, True])[[1,2,0,3,4]] returns a SparseSeries with a float64 dtype. (And taking np.array of the result returns an array of 2 floats. Hence some weirdness in roc_auc_score.)

The latter two points may constitute bugs or features for Pandas. I will post issues there.

Again a contributor is welcome to implement rejecting y as SparseSeries.

Activity

  1. nielsenmarkus11 commented on Sep 7, 2016

    @nielsenmarkus11
    Contributor

    Thanks. I've corrected my code to add pandas.SparseSeries.to_dense() in order to correct the problem.

  2. amueller commented on Sep 7, 2016

    @amueller
    Member

    hm after the array conversion, the length shouldn't match any more, right? Shouldn't that throw us off?

  3. nielsenmarkus11 commented on Sep 7, 2016

    @nielsenmarkus11
    Contributor

    @amueller, these both returned the same results:

    import pandas as pd
    import numpy as np
    from sklearn.metrics import roc_auc_score, roc_curve
    
    r =  np.random.RandomState(0).rand(5)
    
    #SparseSeries.to_dense()
    print(roc_curve(pd.SparseSeries([1,0,0,0,1]).to_dense(), r))
    print(roc_auc_score(pd.SparseSeries([1,0,0,0,1]).to_dense(), r))
    
    #Series
    print(roc_curve(pd.Series([1,0,0,0,1]), r))
    print(roc_auc_score(pd.Series([1,0,0,0,1]), r))
  4. nielsenmarkus11 commented on Sep 7, 2016

    @nielsenmarkus11
    Contributor

    @jnothman, I can work on a solution for this one. Although, I'm a newbie to GitHub. How can I become a contributor?

  5. maniteja123 commented on Sep 8, 2016

    @maniteja123
    Contributor

    Hi, this is the guide to developers for contributing and setting up the environment. Also there is gitter where you can hop in and discuss. Hope it helps.

  6. nielsenmarkus11 commented on Sep 8, 2016

    @nielsenmarkus11
    Contributor

    Thanks @maniteja123!

  7. modified the milestone: 0.19 on Sep 29, 2016
  8. InonS commented on May 28, 2017

    @InonS

    Hey, @nielsenmarkus11 , thanks for picking this up!
    Any news? Maybe the milestone should be updated?

  9. lesteve commented on Jun 1, 2017

    @lesteve
    Member

    I am a bit confused about this one. It seems like the root of the problem is that:

    import numpy as np
    import pandas as pd
    
    ss = pd.SparseSeries([0, 1, 0, 2, 3])
    print(np.asarray(ss))

    Output:

    [1 2 3]
    

    Expected behaviour:

    [0 1 0 2 3]
    

    I am wondering whether pandas is to blame on this one. @jorisvandenbossche, if you can give your opinion on this one that would be great.

  10. jorisvandenbossche commented on Jun 1, 2017

    @jorisvandenbossche
    Member

    Yes, that is indeed unexpected, as @jnothman also notes in the top-post. He opened an issue about it, see the dicussion here: pandas-dev/pandas#14167
    Summary: it was originally deliberately implemeted like that, but in the meantime we agree we should change it to return the dense values. The question is more when we would like to change that (which is not answered in the issue)

  11. lesteve commented on Jun 1, 2017

    @lesteve
    Member

    OK thanks I did miss that in the top-post indeed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions