-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[MRG + 1] Add class_weight to PA Classifier, remove from PA Regressor #4767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MRG + 1] Add class_weight to PA Classifier, remove from PA Regressor #4767
Conversation
@@ -125,6 +126,77 @@ def test_classifier_undefined_methods(): | |||
assert_raises(AttributeError, lambda x: getattr(clf, x), meth) | |||
|
|||
|
|||
def test_class_weights(): | |||
# Test class weights. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this taken from the SGDClassifier tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of it, with some mods here and there. Some others might have been adapted from d-tree's tests if I recall correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to reuse the SGD tests for PA (since they share the implementation). There'd be more work there, so I think this shouldn't be a show stopper for this PR. @amueller what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eg, here: #4838 (comment)
LGTM. Removing from regressor seems fine for me as silently ignoring was a bug and breaking there seems ok. |
Note that I need to rebase this on top of the newly merged #4347 |
44c11f5
to
0166186
Compare
yeah still lgtm. |
@amueller ... While I'm at it, should i also add a |
raise ValueError("class_weight 'balanced' is not supported for " | ||
"partial_fit. In order to use 'balanced' " | ||
"weights, use " | ||
"compute_class_weight('balanced', classes, y). " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this error message indicate that compute_class_weight
can be found in sklearn.utils
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it would be more clear, I'll update this. I'd simply copied from SGD's partial_fit
message. I can also update the other instances where this message appears around the code base in a separate PR.
I skimmed through it and couldn't find this, could you point it out? They do talk about initializing the linear model's weight vector (the coefficients) to 0. Is there something I missed in the cost-sensitive learning section? I thought sample weights (and class weights) are a generally applicable concept, which is why people don't really mention them. But I'd leave sample weights for a different PR. |
+1 on both accounts |
Ah yes, I think I skipped through it too fast :/ |
0166186
to
718825d
Compare
OK, error message updated and commits squashed. |
@vene that message look better to you? |
"partial_fit. In order to use 'balanced' " | ||
"weights, from the sklearn.utils module use " | ||
"compute_class_weight('balanced', classes, y). " | ||
"In place of y you can us a large enough sample " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can us -> you can use
I'd say "subset" instead of "sample", because it's less ambiguous. (consider n_samples
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Ha, yeah ok. Cut and paste job, shall fix. And as I mentioned, this error exists several other places, will (once we get this settled) persist around the code base.
- Fair call, shall also change that.
I really like the new suggestion of using a subset of the labels to estimate class priors. |
rebase on top of scikit-learn#4347 improve error message update error msg
718825d
to
ee78879
Compare
@vene , I think I have all your comments incorporated. |
ping @vene this look good to you now? |
assert_array_equal(clf.predict([[0.2, -1.0]]), np.array([1])) | ||
|
||
# we give a small weights to class 1 | ||
clf = PassiveAggressiveClassifier(C=0.1, n_iter=100, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not entirely convinced it's better, but I can see some reasons for just doing clf.set_params(class_weight={1: 0.001})
here. It makes it explicit that the rest of the parameters shouldn't be changed, in case somebody modifies the test in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree it might be slightly clearer @vene but I see this paradigm only very rarely in other tests in git grep... You think it's necessary for merge?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not important, it just seems slightly better to me from a maintenance point of view.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong opinion on this. Either way would be fine.
I reviewed the code. The changes are good. They are some pending comments, but it's minor details, and merging right now seems the best way to provide value to users. Given that there is already a 👍 I am merging. |
[MRG + 1] Add class_weight to PA Classifier, remove from PA Regressor
Thanks for the reviews all! |
Thanks for the reviews all!
Thanks for the work! And sorry it took so long to merge: the bandwiwth of
core devs is unfortunately very limited.
|
Was browsing Landscape.io and noticed this strange one.
class_weight
is a zombie param for thePassiveAggressiveRegressor
, not present for thePassiveAggressiveClassifier
O_oRemoved from regressor, didn't think deprecation is necessary since it didn't go anywhere and makes no sense either, and implemented it for the classifier with some tests.