Repository navigation
Conversation
|
|
||
| # initialize a flag which checks whether fit or partial fit was called | ||
| # once or not | ||
| self.fit_called = False |
There was a problem hiding this comment.
using this to check first call to fit in place of _check_partial_fit_first_call. thoughts?
There was a problem hiding this comment.
classes_ set during initialization to set it once during the first call or when refit used and not again.
There was a problem hiding this comment.
I'm not sure why you would need this. Also, init shouldn't set any attributes that are not __init__ parameters.
| initialization. It will be ignored in subsequent calls. | ||
|
|
||
| Note: Now this argument is redundant. We should remove this. But | ||
| this will affect backward compatibility |
There was a problem hiding this comment.
is removing classes argument from partial_fit feasible? it will cause the previous code using this argument to give an error. thoughts?
There was a problem hiding this comment.
If we go the route of the PR and add classes to the constructor everywhere, we should deprecate it in partial_fit I think. We certainly wouldn't remove it, because that would break code, as you say. Check out the documentation on deprecation in the developer docs.
There was a problem hiding this comment.
got it.. my bad, should have checked it in the first place..
| Returns self. | ||
| """ | ||
| X, y = check_X_y(X, y) | ||
| check_y_classes(y, self.classes_) |
There was a problem hiding this comment.
added this new function in utils as it'll be useful for all estimators.
| i = classes.searchsorted(y_i) | ||
| for y_i in np.unique(y): | ||
| # classes need not be sorted because user input | ||
| i = np.where(classes == y_i)[0][0] |
There was a problem hiding this comment.
the classes need not be sorted anymore because they are user input. so using where in place of searchsorted
There was a problem hiding this comment.
huh... we could sort them. not having them sorted might lead to subtle bugs in some places...
There was a problem hiding this comment.
i'll sort them but then i'll also make a note in the documentation of this parameter that this will be sorted internally and they should use the classes_ argument to get the final order. works?
| y2 = np.array([1, 1, 2, 2, 3, 3]) | ||
|
|
||
| # Test not needed if we remove the classes parameter in partial fit | ||
| # def test_gnb(): |
There was a problem hiding this comment.
this test is no more required because partial_fit will not have a classes argument. if this works, i'll remove this in next commit. just commented for now.
There was a problem hiding this comment.
this test should stay there until the end of the deprecation cycle.
There was a problem hiding this comment.
added back with 1 more test in it
| assert_raises(ValueError, clf.partial_fit, np.hstack((X, X)), y) | ||
|
|
||
|
|
||
| def test_gnb_extra_classes(): |
There was a problem hiding this comment.
new test defined to check if adding new labels at end or started just adds zero-value columns and doesn't change the predictions made earlier.
|
i've added some comments in the files changed section. please review those as well. |
|
Thanks for posting this. I'll try to check it out tomorrow. |
|
you need to fix the doctests. |
amueller
left a comment
There was a problem hiding this comment.
some comments, rest in meeting ;)
| """ | ||
|
|
||
| def __init__(self, priors=None): | ||
| def __init__(self, classes=None, priors=None): |
There was a problem hiding this comment.
it should go last in case people passed priors as positional argument.
| self.priors = priors | ||
|
|
||
| # initialize additional parameter: | ||
| self.classes_ = None |
There was a problem hiding this comment.
It's illegal to set trailing underscore attributes in __init__ (and should result in some test failure)
|
|
||
| # initialize a flag which checks whether fit or partial fit was called | ||
| # once or not | ||
| self.fit_called = False |
There was a problem hiding this comment.
I'm not sure why you would need this. Also, init shouldn't set any attributes that are not __init__ parameters.
| """ | ||
| # check if classes were defined, set the classes_ attribute every time | ||
| if self.classes is not None: | ||
| self.classes_ = np.asarray(self.classes) |
There was a problem hiding this comment.
we should check that they are unique, right?
| initialization. It will be ignored in subsequent calls. | ||
|
|
||
| Note: Now this argument is redundant. We should remove this. But | ||
| this will affect backward compatibility |
There was a problem hiding this comment.
If we go the route of the PR and add classes to the constructor everywhere, we should deprecate it in partial_fit I think. We certainly wouldn't remove it, because that would break code, as you say. Check out the documentation on deprecation in the developer docs.
| i = classes.searchsorted(y_i) | ||
| for y_i in np.unique(y): | ||
| # classes need not be sorted because user input | ||
| i = np.where(classes == y_i)[0][0] |
There was a problem hiding this comment.
huh... we could sort them. not having them sorted might lead to subtle bugs in some places...
| y2 = np.array([1, 1, 2, 2, 3, 3]) | ||
|
|
||
| # Test not needed if we remove the classes parameter in partial fit | ||
| # def test_gnb(): |
There was a problem hiding this comment.
this test should stay there until the end of the deprecation cycle.
| """Test whether adding extra classes doesn't change the prediction of the | ||
| existing classes just adds 0 value columns for new classes""" | ||
| clf1 = GaussianNB(classes=[1, 2]).fit(X, y) | ||
| pred1 = clf1.predict_proba(X) |
There was a problem hiding this comment.
check the shapes first on all of these
| assert_array_almost_equal(pred1[:, 0], pred3[:, 1]) | ||
| assert_array_almost_equal(pred1[:, 1], pred2[:, 1]) | ||
| assert_array_almost_equal(pred1[:, 1], pred3[:, 2]) | ||
| assert_array_almost_equal(pred2[:, 2], zero_array) |
| assert_array_almost_equal(pred2[:, 3], zero_array) | ||
| assert_array_almost_equal(pred3[:, 0], zero_array) | ||
| assert_array_almost_equal(pred3[:, 3], zero_array) | ||
|
|
There was a problem hiding this comment.
add test that passing y that's not contained in classes raises error
| raise ValueError("Negative values in data passed to %s" % whom) | ||
|
|
||
|
|
||
| def check_y_classes(y, classes): |
|
|
||
| def check_y_classes(y, classes): | ||
| """ | ||
| Chech if y has a subset of classes as set in the classes argument. |
That is always an option - though not a function, a scoring callable. And I'm only talking as a workaround until we have the |
we could ask the user to do it at their end then. maybe make the error more explicit with link to the documentation on how to resolve it. |
|
That's what I meant |
|
I'm not sure how we can use make_scorer to solve the error in the code |
|
I think we're heading the right direction regardless, in that we're annotating the scorer with how the underlying metric should take classes. I think we should take a step back and list the cases that are working poorly ATM and try to think about solving it as a whole:
Are there cases I have missed? We need to make sure that our solution fixes all of these, perhaps with the exception of partial_fit, and is the simplest solution to do so |
I guess we'll be deprecating the classes parameter of partial_fit and force the user to pass same input as that passed while initialization (if passed). what do u mean by it being a maintenance pitfall?
yea its less likely but a user might have bad train-test split strategy and if there are many classes, it might be an issue. we can still cater to it with classes parameter and smarter scorers.
i think we can solve both by adding an extra step. before doing cv, we can check if the estimator has classes parameter set, if not, we'll can just copy the estimator into a new one, keeping all parameters same just passing an additional classes argument taking unique from y (not sure if such an estimator clone is currently possible in sklearn). but again we need the classes parameter added to estimators for all these. i'm not sure how we can do it by just fixing the scorers. one way I can think of, without having the classes parameter added to all estimators, is if we can somehow create a helper to explicitly set the classes_ attribute of an estimator, which is illegal right now afaik. |
|
@jnothman I think you have all cases. We might want to also think about bagging, which do work, but would be easier with this implementation. @aarshayj whenever we do cross-validation we clone the estimator for each separate fold, so in theory we could inject |
|
Btw, also see #1304 for an earlier attempt that we then abandoned but now following through on (maybe). |
|
I think we should not only consider our cross-validation, but also users building their own cross-validation or other ensembling. It would be good to be able to ensure consistent shape of |
|
I think I actually like @aarshayj's idea of setting |
|
Ok, so in this branch: cross_val_score(GaussianNB(), iris.data, iris.target, cv=KFold(2), scoring="f1_macro")
Here there are two classes in the averaging because cross_val_score(GaussianNB(classes=[0, 1, 2]), iris.data, iris.target, cv=KFold(2), scoring="f1_macro")
The second result is probably the intended one. Without having That give the correct result. It doesn't work in this branch, though (in this branch you should use |
|
I'm a bit confused because master yields cross_val_score(GaussianNB(), iris.data, iris.target, cv=KFold(2), scoring="f1_macro")
|
This is because f1_score considers labels as the combination of unique values of y_true and y_pred. So in first fold, classes [1,2] went into training and a few 2s were predicted so f1_score had 3 classes. But in second case, [0,1] went into training but all 1s were predicted. so f1_score divided by 2 as it didn't know that 0 existed as a class. This PR is more consistent. it passes labels from estimator and the average is taken only for the classes present in train. So the result:
|
|
I propose to add a I think computing a metric for a subset of the labels is a relatively rare special case, and we should be strict in validating the input to the metric, unless the user explicitly asks for a subset. |
|
So my plan:
I think that will cover all use-cases and will make it hard for users to get wrong results. Also it will be relatively automatic. |
I mean that we've had problems with buggy implementations. In part because we've not merged some common tests for |
|
@jnothman any comments on my plan? |
|
Yes, a comment: please make us release 0.19 so that I can feel comfortable thinking about these things! I'll comment more specifically when I can soon focus on this... |
|
Some days I wonder who I should blame for this idea of keeping the learning algorithm, its model and the data semantics on the same object... Similarly, I know Gaël wanted to not decorate metrics with the properties that transform them into scorers, but I'm a little worried that annotations like At a glance your plan seems reasonable. The need for Your plan doesn't mention Your plan also doesn't mention how we do this in terms of estimator checks in a way that doesn't break contrib code! |
|
one way to do it without a new argument like Also, I'm not sure how many people actually use labels in scorer to get scores for only a subset of classes. If I can assume, the people who do really know what they're doing, this might work. Of course, this would mean we fix both classifiers and scorers in the same version. |
|
I think we should just deprecate
I'm not sure I understand your point. The idea was to have @aarshayj the problem I saw with your approach was that we might pass |
I think we can overcome this. What I meant was that we should have this check before we clone the estimator and pass classes in
i think the idea is also based on an assumption that people who are using a subset of labels really know what they are doing and we might get away without a hard confirmation from them. sounds fair? |
|
no, better with allow_label_subsets. we might, for instance, have multiple
scorers, some only for subsets. or we might want a model fitted to all the
classes but scored on a subset.
…On 15 Aug 2017 6:59 am, "Aarshay Jain" ***@***.***> wrote:
@aarshayj <https://github.com/aarshayj> the problem I saw with your
approach was that we might pass classes automagically in cross-validation
when we know the true classes, so it's a fragile way to determine it was
explicitly set by the user.
I think we can overcome this. What I meant was that we should have this
check before we clone the estimator and pass classes in cross_val_score.
When a call to cross_val_score is made, we have the estimator, scorer and
y. so we can check the following:
- if classes is set in estimator, we're good irrespective of labels.
- if not, if labels is a subset of whats present in y, then we flag an
error (after deprecation) or warning (now) saying that a subset of classes
was passed as labels but classes was not set at init.
- if not, labels has same classes and y and we're good to set them as
classes in cloned estimator.
thoughts?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#9290 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEz6xr4fv02iosqlvcFR6GLaxQulRTgks5sYLUfgaJpZM4OP3co>
.
|
|
I think the above strategy allows both of these scenarios. The classes being fit on are dependent on the labels only though the fact that they have to be a superset. We can fit on as many classes and score on a subset. And we can also have as many scorers as we want, the classes argument will be a union of all the labels and classes in y, if not passed during init. |
|
@aarshayj I don't think I understand your proposal, let's discuss in person. |
Fixing issue #8100
This PR involves adding a classes argument to the initialization of the gaussian naive bayes estimator. This would make the classes argument of partial_fit redundant as we'll no longer need to specify it every time.