[MRG] Permutation importance support sample weight #16906
[MRG] Permutation importance support sample weight #16906rth merged 8 commits intoscikit-learn:masterfrom
Conversation
thomasjpfan
left a comment
There was a problem hiding this comment.
Thank you for the PR @RoeiKa !
thomasjpfan
left a comment
There was a problem hiding this comment.
Please add an entry to the change log at doc/whats_new/v0.24.rst with tag ||. Like the other entries there, please reference this pull request with :pr: and credit yourself (and other contributors if applicable) with :user:.
|
An entry was added to doc/whats_new/v0.24.rst |
| else: | ||
| X_permuted[:, col_idx] = X_permuted[shuffling_idx, col_idx] | ||
| feature_score = scorer(estimator, X_permuted, y) | ||
| feature_score = scorer(estimator, X_permuted, y, sample_weight) |
There was a problem hiding this comment.
We can be a little more careful here. If a user passes a scorer that is a callable without sample_weight, this would break their code. What about:
| feature_score = scorer(estimator, X_permuted, y, sample_weight) | |
| if sample_weight is not None | |
| feature_score = scorer(estimator, X_permuted, y, | |
| sample_weight=sample_weight) | |
| else: | |
| feature_score = scorer(estimator, X_permuted, y) |
And a small test to make sure passing a callable scorer with the signature:
def my_scorer(estimator, X, y):
return 1
does not error.
There was a problem hiding this comment.
That's a good point. The only issue is that both permutation_importance and _calculate_permutation_scores use this call, so the suggested correction would have to be implemented in both functions.
An alternative solution is to create a new protected function that do this logic, for example:
def _scorer_weights_robust(scorer, estimator, X, y, sample_weight):
if sample_weight is not None:
return scorer(estimator, X, y, sample_weight)
return scorer(estimator, X, y)
then we can use it like this:
feature_score = _scorer_weights_robust(scorer, estimator, X_permuted, y, sample_weight)
What do you think?
There was a problem hiding this comment.
Okay lets create this function to be called in both spots.
thomasjpfan
left a comment
There was a problem hiding this comment.
LGTM
We will need to wait for another review before we can merge this.
|
LGTM, thanks @RoeiKa ! |
Reference Issues/PRs
None
What does this implement/fix? Explain your changes.
This PR adds sample weight support for the permutation_importance function.
Any other comments?