Skip to content

[MRG] FIX Missing test for check_class_weight_balanced_linear_classifier (#…#13313

Merged
glemaitre merged 6 commits intoscikit-learn:masterfrom
psorianom:test_check_class_weight_balanced_linear_classifier
Mar 1, 2019
Merged

[MRG] FIX Missing test for check_class_weight_balanced_linear_classifier (#…#13313
glemaitre merged 6 commits intoscikit-learn:masterfrom
psorianom:test_check_class_weight_balanced_linear_classifier

Conversation

@psorianom
Copy link
Copy Markdown
Contributor

…9079)

Reference Issues/PRs

Fixes #9079

What does this implement/fix? Explain your changes.

check_class_weight_balanced_linear_classifier in sklearn/utils/estimator_checks verifies that a Classifier correctly computes the class weights when class_weight="balanced".

This check is run on test_common.py. Nevertheless, it is missing a test in sklearn/utils/tests/test_estimator_checks.py.

This PR adds a test to verify that the check actually verifies that a classifier is not correctly computing the class weights when class_weight="balanced".

The solution involved generating a dummy class that calculates the balanced weights and then arbitrarily modifies them to simulate the miscalculation of coef_. The assertion in check_class_weight_balanced_linear_classifier is then supposed to fail.

Any other comments?

Copy link
Copy Markdown
Member

@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

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

Could you add also an entry in what's new.
In this case, sklearn contribution could be interested to know about an additional common tests.



def test_check_class_weight_balanced_linear_classifier():
class BadBalancedWeightsClassifier(BaseBadClassifier):
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.

You can move this class outside next to the other classes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @glemaitre, thank you for reviewing this.
I changed the location of the class as you suggested. Can you point me towards where can I find "what's new" to add an entry ?

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.

@glemaitre
Copy link
Copy Markdown
Member

The error is unrelated (http error).

`predict`, `predict_proba`, `transform`, and `decision_function` does not
change. :issue:`12328` by :user:`Nicolas Hug <NicolasHug>`

- Add ``test_check_class_weight_balanced_linear_classifier`` to
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 would simplify a bit the message:

Add an additional common test for estimators to check that ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What do you think abou this version:

  • Add an additional estimator_check test to verify the proper functioning of the check that corroborates that estimators generate correct balanced class weights (when class_weight=balanced). :issue:9079 by :user:Pavel Soriano <psorianom>.

As I am adding a test for a check, it gets a little bit confusing :/

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.

oh upsy. Sorry about that. You actually don't need an entry then. Let me change this and merge then

@glemaitre
Copy link
Copy Markdown
Member

Otherwise it looks good

glemaitre and others added 2 commits February 28, 2019 16:45
Co-Authored-By: psorianom <pavelsoriano@fastmail.fr>
@glemaitre
Copy link
Copy Markdown
Member

Actually, we need a second review there. But everything LGTM.

# Intentionally modify the balanced class_weight
# to simulate a bug and raise an exception
if self.class_weight == "balanced":
class_weight += 1.
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 don't get what this is doing. Surely this doesn't result in affecting the output of predict. In fact the reason why the test is passing may be because predict isn't affected by changing class weight at all...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, you are right, this dummy class does not affect the output of predict. Instead, it affects class_weight.

I did it that way because, as I understand, the check check_class_weight_balanced_linear_classifier this test is testing does not verify the output of predict. It simply verifies coef_ after the estimator has been fitted.

assert_allclose(coef_balanced, coef_manual)

@jnothman
Copy link
Copy Markdown
Member

jnothman commented Mar 1, 2019 via email

@glemaitre glemaitre merged commit 8f6b5b0 into scikit-learn:master Mar 1, 2019
@glemaitre
Copy link
Copy Markdown
Member

@psorianom Thanks!!!

xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
koenvandevelde pushed a commit to koenvandevelde/scikit-learn that referenced this pull request Jul 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

check_class_weight_balanced_classifiers is never run?!

3 participants