Skip to content

Helpers for managing different set of classes #8100

Open

Description

@jnothman

It seems like half the bugs we've solved in the past couple of months surround problems of having different sets of classes, in the context of:

  • cross-validation splitting that yields different subsets of classes in different training or testing subsets (and hence issues in alignment of class-wise outputs from predict_proba, decision_function or metrics, or in normalising macro-averaged scores)
  • partial_fit where classes are specified upfront, but then repeated calls need matching to those classes
  • warm_start where classes_ from the first fit must be identical to the set of classes in y in each call to fit()

These are all subtly different problems, but at the moment it seems like we're handling them on an ad-hoc (and too often a post-hoc) basis.

It would be amazing if someone could review these issues and identify where either API changes (classes as a constructor parameter to classifiers has been suggested) or helper utilities might help avoid these issues in the future.

Activity

  1. dalmia commented on Jan 12, 2017

    @dalmia
    Contributor

    I'll take this up.

  2. jnothman commented on Jan 12, 2017

    @jnothman
    MemberAuthor

    I think it will be a challenge, @dalmia, but go ahead.

  3. dalmia commented on Jan 12, 2017

    @dalmia
    Contributor

    Thanks for the heads up @jnothman. :)

  4. amueller commented on Mar 3, 2017

    @amueller
    Member

    @dalmia are you still working on this?

  5. dalmia commented on Mar 8, 2017

    @dalmia
    Contributor

    @amueller No Sir, please retain the 'Need Contributor' label.

  6. amueller commented on Jun 28, 2017

    @amueller
    Member

    Ok so in #6231 we talked about adding classes as a construction parameter, which I think might be the best solution.

    If we do that, what's the shape of coef_ in linear models? I guess it needs to reflect all classes to solve the warm-start issue.

  7. amueller commented on Jul 7, 2017

    @amueller
    Member

    Should we sort the classes that the user provides? That might be surprising to the user, but not doing it might lead to subtle bugs as we rely on that in some places, I think?

    It seems a bit strange to me that get_params and set_params return / set classes as searching over it is nonsensical, but I guess it's not the worst....

  8. jnothman commented on Jul 8, 2017

    @jnothman
    MemberAuthor
  9. amueller commented on Aug 4, 2017

    @amueller
    Member
  10. amueller commented on Aug 11, 2017

    @amueller
    Member

    Should we have attributes reflect the additional classes in all cases? I think so, because otherwise it will be very confusing. @aarshayj points out that that requires changing coef_ and intercept_ in the linear models, if we want them to correspond to the entries in decision_function.

  11. thismlguy commented on Aug 11, 2017

    @thismlguy
    Contributor

    yea i think we'll have to do this. for instance, if the user sets classes=[1,2,3] while y has only [1,2], then an SVM will return just 1 set of coefficients and 1d decision function because the library we use makes use of data only in y. but in our API, we need to supplement both coef and decision function to make them 3d.

    the returned values are for the positive class (2). so we can fill in 0s for class (3) and fill in class (1) as negative of the values for class (2).

  12. jnothman commented on Aug 13, 2017

    @jnothman
    MemberAuthor
  13. amueller commented on Aug 30, 2017

    @amueller
    Member

    So I'm not entirely certain this is the right solution any more, because of issues we face in the implementation.

    There are two or three main points:

    • are we requiring classes in our API contract for third-party estimators?
    • If we don't, then issues like in cross_val_predict will remain for third-party estimators, and we end up where we started.
    • how much of the internal structure of the estimators should reflect the new classes? coef_ et al is very vague. For GradientBoostingClassifier, what should estimators_ be? Should we actually create trees for classes that don't exist? What should coef_ and intercept_ be? Ideally we want zero probabilities, which means -np.inf for decision function and intercept, which is maybe not great.

    If we used a wrapper/meta-estimator approach, these issues would not exist as we could wrap third-party estimators and not expose any internal data structures. I'm not sure if a wrapper would be the right solution to ensure consistent scoring, but it's probably possible.

    Ping @jnothman @GaelVaroquaux @ogrisel @agramfort.

  14. amueller commented on Aug 30, 2017

    @amueller
    Member

    Hm for the third-party estimators, I guess we don't have to require classes, and just raise an error if we want to combine multiple estimators that have different classes_ like in cross_val_predict. Basically someone only needs to implement classes if they want these edge cases to work. Still seems like a bit of a burden on the estimator author that we could avoid with a wrapper approach.

  15. jnothman commented on Aug 31, 2017

    @jnothman
    MemberAuthor

    are we requiring classes in our API contract for third-party estimators?

    In short, yes. Eventually. We need to maintain the classes_ mismatch handling in cross_val_predict for now in any case.

    We don't end up where we started in that we have a way for metaestimators to pass classes to supporting classifiers (and scorers), and that we remove the problem in partial_fit for us and for third parties who adopt the classes parameter. But yes, when the user has not specified classes, weird errors may be raised in partial_fit still (unless protected by common tests).

    A wrapper approach for meta-estimators interpreting the output of decision_function? Okay, but it won't work for interpreting coef_, will it? It won't work for passing classes to scorers, but I suppose we can make that the user's problem as we currently do.

    I do see your problems about making the models represent all the classes in a consistent way. I'm not yet sure what the solution is.

  16. GaelVaroquaux commented on Aug 31, 2017

    @GaelVaroquaux
    Member
  17. added this to the 0.21 milestone on Jun 26, 2018
  18. modified the milestones: 0.21, 0.22 on Apr 10, 2019
  19. modified the milestones: 0.22, 0.23 on Oct 17, 2019
  20. modified the milestones: 0.23, 0.24 on Apr 20, 2020
  21. removed this from the 0.24 milestone on Oct 15, 2020
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions