[MRG] Normalize linear_model decision_function scores. #19142
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reference Issues/PRs
Fixes #19139.
What does this implement/fix? Explain your changes.
According to the docs, the decision_function scores in LinearClassifierMixin are supposed to be the distances of samples to corresponding hyperplanes. (I assume) the hyperplanes are defined by the coefficients and intercepts arrays. The scores are computed as the scalar product of each sample with each of the coefficients, plus the intercepts. The problem is that without normalizing these scores by the norm of their corresponding coefficients, the scores won't actually be the signed distances to the corresponding hyperplanes. This is because the signed distance of a point p to a hyperplane defined by c'x + b = 0 is (c'p + b)/|c|, not the (c'p + b) currently computed.
I fix this problem by normalizing the scores by the norm of their corresponding coefficients.
Any other comments?
I ran pytest on linear_model and a few of the tests are failing, some because computed accuracies are being compared to hard-coded values. This may be expected if the hard-coded values reflect desired outputs using the previous, potentially incorrect, method of computing the scores.
Also, I haven't done any checking for division by zero which would occur if any of the coefficients are all zeros, because I wasn't sure what sklearn best practices are for doing so, and it will be easy enough for whoever does know to add this. Some tests are failing because NaNs are appearing, presumably due to such division by zero.