Skip to content

ENH/DEPR add new response_method and deprecate needs_* in make_scorer#26840

Merged
thomasjpfan merged 38 commits into
scikit-learn:mainfrom
glemaitre:make_scorer_list_response
Oct 17, 2023
Merged

ENH/DEPR add new response_method and deprecate needs_* in make_scorer#26840
thomasjpfan merged 38 commits into
scikit-learn:mainfrom
glemaitre:make_scorer_list_response

Conversation

@glemaitre

@glemaitre glemaitre commented Jul 15, 2023

Copy link
Copy Markdown
Member

closes #26817

While working in #26120, I find myself doing dirty tricks to overwrite the response method used in _ThresholdScorer while we should be able to pass a list of priorities and delegate to _get_response_values.

I introduce response_method in make_scorer that remove the need to needs_proba and needs_threshold. I assume that the API is simplified due to the work done with _get_response_values. Now, we don't need to have different type of scorers (i.e. _PredictScorer, _ProbaScorer, and ThresholdScorer). All are handled in a single class because all the complexity is delegated to the _get_response_values to which we provide the response method to try.

I need to alternate the provided scorer in the _MultiMetricScorer to be sure to hit the cache. It means that we need to decide on a single method to use for a single estimator. At the end, it simplifies also when the cache policy is beneficial.

@github-actions

github-actions Bot commented Jul 15, 2023

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: b80e944. Link to the linter CI: here

@glemaitre

Copy link
Copy Markdown
Member Author

ping @betatim on this one ;)

Comment thread sklearn/utils/_response.py Outdated
Comment thread sklearn/utils/tests/test_response.py Outdated
Comment thread sklearn/metrics/_scorer.py Outdated
Comment thread sklearn/metrics/_scorer.py Outdated
@betatim

betatim commented Jul 17, 2023

Copy link
Copy Markdown
Member

While working in #26120, I find myself doing dirty tricks to overwrite the response method used in _ThresholdScorer while we should be able to pass a list of priorities and delegate to _get_response_values.

I kinda see why it is needed, but I also kinda don't like having an extra argument to make_scorer that is only used sometimes. Do you need to be able to configure the list or would it be enough to change _ThresholdScorer to use a list of options (the "list of str") instead of just "predict"?

If we settle on adding an argument to make_scorer I think we need some input validation to warn/error when users use it but don't set use_threshold=True (or think about whether it makes sense to all this even if use_threshold=False).

The bug fix part for ClassifierChain I am happy with, just a few style nits.

@thomasjpfan thomasjpfan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does resolving #26817 require a new respond_method? If they are unrelated, can this be split into two PRs?

  • Adding multi-label-indicator support
  • Add respond_method to adjust the order for needs_threshold=True

Comment thread sklearn/utils/_response.py Outdated
Comment thread sklearn/metrics/_scorer.py Outdated
@glemaitre

Copy link
Copy Markdown
Member Author

I open #27002 to solve the issue of multilabel-indcator on a separate PR. I am putting this PR on draft until merging the other PR.

The other PR will target 1.3.1.

@glemaitre

Copy link
Copy Markdown
Member Author

Going back to this PR now that we solve the multi-label bug and I need this PR for #26120.

@glemaitre

Copy link
Copy Markdown
Member Author

Do you need to be able to configure the list or would it be enough to change _ThresholdScorer to use a list of options (the "list of str") instead of just "predict"?

This is exactly this. I would like to favor predict_proba or decision_function depending on the use case.

If we settle on adding an argument to make_scorer I think we need some input validation to warn/error when users use it but don't set use_threshold=True (or think about whether it makes sense to all this even if use_threshold=False).

We can error when passing something to this new argument while not setting use_threshold=True.

@glemaitre glemaitre marked this pull request as ready for review September 28, 2023 10:08
@glemaitre

Copy link
Copy Markdown
Member Author

Maybe @betatim @thomasjpfan you want to have a new look at this one.
@adrinjalali @ogrisel for some extra reviews (I would be fine with only 2 reviewers ;))

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm finding the combination of needs_proba, needs_threshold and response_method confusing. Seems like with the introduction of response_method we can deprecated one of needs_proba/needs_threshold

Specifically, seems like we can deprecate _ProbaScorer?

I also kinda feel like the changes here aren't tested, but codecov obviously doesn't agree 😁

(didn't mean to "approve" 😝 )

Comment thread sklearn/metrics/_scorer.py Outdated
Comment thread sklearn/metrics/_scorer.py Outdated
Comment thread sklearn/metrics/_scorer.py Outdated
Comment thread sklearn/metrics/_scorer.py Outdated
@glemaitre

Copy link
Copy Markdown
Member Author

I'm finding the combination of needs_proba, needs_threshold and response_method confusing. Seems like with the introduction of response_method we can deprecated one of needs_proba/needs_threshold

I think that we can deprecate both of them indeed: if response_method="predict_proba", then you it would be equivalent to needs_proba=True. If "decision_function" in the response_method

Comment thread sklearn/metrics/_scorer.py
Comment thread sklearn/metrics/_scorer.py
Comment thread sklearn/metrics/_scorer.py Outdated
Comment thread sklearn/metrics/_scorer.py Outdated
# Score functions that need decision values
top_k_accuracy_scorer = make_scorer(
top_k_accuracy_score, greater_is_better=True, needs_threshold=True
top_k_accuracy_score,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Whatever changes below is only because of the new API.

@glemaitre glemaitre left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added a couple of comments to help at reviewing this PR.
Usually, the complex part is about the caching mechanism and _MultimetricScorer.

@adrinjalali adrinjalali self-requested a review October 5, 2023 11:16

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice!

Comment thread doc/modules/model_evaluation.rst Outdated
Comment thread doc/whats_new/v1.4.rst Outdated
Comment thread doc/whats_new/v1.4.rst Outdated
Comment thread sklearn/metrics/_scorer.py Outdated
Comment thread sklearn/metrics/_scorer.py Outdated
Comment thread sklearn/metrics/_scorer.py Outdated
glemaitre and others added 4 commits October 5, 2023 13:54
Co-authored-by: Adrin Jalali <adrin.jalali@gmail.com>
Co-authored-by: Adrin Jalali <adrin.jalali@gmail.com>
@adrinjalali

Copy link
Copy Markdown
Member

Deprecation warnings making tests fail, otherwise LGTM.

@thomasjpfan thomasjpfan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR! Overall LGTM

@thomasjpfan thomasjpfan enabled auto-merge (squash) October 16, 2023 22:51
@thomasjpfan thomasjpfan merged commit e3d67d5 into scikit-learn:main Oct 17, 2023
glemaitre added a commit to glemaitre/scikit-learn that referenced this pull request Oct 31, 2023
…scikit-learn#26840)

Co-authored-by: Adrin Jalali <adrin.jalali@gmail.com>
Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
REDVM pushed a commit to REDVM/scikit-learn that referenced this pull request Nov 16, 2023
…scikit-learn#26840)

Co-authored-by: Adrin Jalali <adrin.jalali@gmail.com>
Co-authored-by: Thomas J. Fan <thomasjpfan@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scorer not working on ClassifierChain

4 participants