Skip to content

MRG add common test to check if estimators overwrite their init params - #1518

Merged
amueller merged 0 commit into
scikit-learn:masterfrom
amueller:test_fit_changes_params
Jan 15, 2013
Merged

amueller merged 0 commit into
scikit-learn:masterfrom
amueller:test_fit_changes_params

Conversation

@amueller

@amueller amueller commented Jan 5, 2013

Copy link
Copy Markdown
Member

Addressing #1108 (checking that random_state is not altered) and even a bit more.

Currently the test is pretty strict: it checks that the init parameter is not changed AT ALL during fit.
For default values that are set to None and instantiated later, this is a bit stupid.

But on the other hand, sometimes None means a parameter will be set from the data - so the none should stay there after fit.

What do you think would be a good policy?

@amueller

amueller commented Jan 5, 2013

Copy link
Copy Markdown
Member Author

Current output

DecisionTreeClassifier changes min_samples_split from 1 to 2 during fit.
DecisionTreeClassifier changes random_state from None to <mtrand.RandomState object at 0x7f0a60ce11b0> during fit.
EllipticEnvelop changes random_state from None to <mtrand.RandomState object at 0x7f0a60ce11b0> during fit.
EllipticEnvelope changes random_state from None to <mtrand.RandomState object at 0x7f0a60ce11b0> during fit.
ExtraTreeClassifier changes min_samples_split from 1 to 2 during fit.
ExtraTreeClassifier changes random_state from None to <mtrand.RandomState object at 0x7f0a60ce11b0> during fit.
ExtraTreesClassifier changes random_state from None to <mtrand.RandomState object at 0x7f0a60ce11b0> during fit.
GradientBoostingClassifier changes loss from deviance to mdeviance during fit.
GradientBoostingClassifier changes init from None to PriorProbabilityEstimator() during fit.
GradientBoostingClassifier changes random_state from None to <mtrand.RandomState object at 0x7f0a60ce11b0> during fit.
GradientBoostingClassifier changes max_features from None to 2 during fit.
RandomForestClassifier changes random_state from None to <mtrand.RandomState object at 0x7f0a60ce11b0> during fit.

@glouppe

glouppe commented Jan 5, 2013

Copy link
Copy Markdown
Contributor

Looks like the tree-growers are again the bad guys ;)

@amueller

amueller commented Jan 5, 2013

Copy link
Copy Markdown
Member Author

I'm on it already ;)

@amueller

amueller commented Jan 5, 2013

Copy link
Copy Markdown
Member Author

I would rather see it as the tree-growers having added the most functionality recently, so that is not so well-tested yet ;)

@amueller

amueller commented Jan 5, 2013

Copy link
Copy Markdown
Member Author

cc @pprett I modified the random state place a bit again ;)

@amueller

amueller commented Jan 5, 2013

Copy link
Copy Markdown
Member Author

I fixed the random_state everywhere. Now it looks like this:

DecisionTreeClassifier changes min_samples_split from 1 to 2 during fit.
ExtraTreeClassifier changes min_samples_split from 1 to 2 during fit.
GradientBoostingClassifier changes loss from deviance to mdeviance during fit.
GradientBoostingClassifier changes init from None to PriorProbabilityEstimator() during fit.
GradientBoostingClassifier changes max_features from None to 2 during fit.

I will now do regression, transformers and clustering.
I'm not sure if I should modify the test or we should introduce underscored attributes.

In GradientBoostingClassifier, the max_features actually is a slight bug, right? @pprett ?

@ogrisel

ogrisel commented Jan 5, 2013

Copy link
Copy Markdown
Member

I would rather be strict and use _ suffixed names for every attribute updated from the data during the call to fit.

@ogrisel

ogrisel commented Jan 5, 2013

Copy link
Copy Markdown
Member

BTW thanks @amueller for automating this kind of checks.

@amueller

amueller commented Jan 5, 2013

Copy link
Copy Markdown
Member Author

@ogrisel After running the tests I tend to agree. There are only very little estimators that don't do that already, so it would only be minor changes.

@amueller

amueller commented Jan 5, 2013

Copy link
Copy Markdown
Member Author

Ok, looks better now:

DecisionTreeClassifier changes min_samples_split from 1 to 2 during fit.
ExtraTreeClassifier changes min_samples_split from 1 to 2 during fit.
GradientBoostingClassifier changes loss from deviance to mdeviance during fit.
GradientBoostingClassifier changes init from None to PriorProbabilityEstimator() during fit.
GradientBoostingClassifier changes max_features from None to 2 during fit.
DecisionTreeClassifier changes min_samples_split from 1 to 2 during fit.
ExtraTreeClassifier changes min_samples_split from 1 to 2 during fit.
GradientBoostingClassifier changes loss from deviance to mdeviance during fit.
GradientBoostingClassifier changes init from None to PriorProbabilityEstimator() during fit.
GradientBoostingClassifier changes max_features from None to 2 during fit.
DecisionTreeClassifier changes min_samples_split from 1 to 2 during fit.
ExtraTreeClassifier changes min_samples_split from 1 to 2 during fit.
GradientBoostingClassifier changes loss from deviance to mdeviance during fit.
GradientBoostingClassifier changes init from None to PriorProbabilityEstimator() during fit.
GradientBoostingClassifier changes max_features from None to 2 during fit.
.Ward changes n_components from None to 1 during fit.
WardAgglomeration changes n_components from None to 1 during fit.

It seems GradientBoostingClassifier is both a Classifier and a Regressor (and a transformer)? Have to check that.

I now simply forgot the random_state that was retrieved via check_random_state. I didn't think it made sense to save it. We could think about saving the initial state that was chosen for reproducing a given result (not really necessary imho), but I don't think that would be possible from saving the used state.

@amueller

amueller commented Jan 5, 2013

Copy link
Copy Markdown
Member Author

Is there a reason not to have min_samples_split be 2 by default?

@amueller

amueller commented Jan 6, 2013

Copy link
Copy Markdown
Member Author

If no-one opposed I'll cherry pick the random-state stuff into master and leave the test in the PR for the moment so we can discuss.
Paul reported a problem on the ML that I now traced back to the RF changing its seed :-/
So if you call fit on a RF with fixed seed twice, it will give you different results. you have to reset the seed in between.

@glouppe

glouppe commented Jan 6, 2013

Copy link
Copy Markdown
Contributor

Is there a reason not to have min_samples_split be 2 by default?

Hmm not really. Anyway, all the xxx changes min_samples_split from 1 to 2 during fit can be easily fixed by not overwriting the value of self.min_samples_split. I can do that in #1488 while I am at it. (Or should I do that elsewhere?)

@amueller

amueller commented Jan 6, 2013

Copy link
Copy Markdown
Member Author

Yeah, you can do that, too. I was wondering why there is the possibility to set 1 at all. That doesn't make sense, right? Is it a marker for "set to default"?

Then ok, just don't overwrite it. I just thought it would be slightly more intuitive to have as a default value the value that is actually used. If you read the docstring, you might wonder why someone would want to set that to one.

Either way, I'd say just push it to master since it is such a minor change.

@amueller

amueller commented Jan 6, 2013

Copy link
Copy Markdown
Member Author

Ok, I got it. It is done in such a way that min_samples_split is not stricter than min_samples_leaf.
I am not sure I understand it, though. Shouldn't that be a min, not a max? The way it is done now is that it loosens the requirements by the user. If it was a min, it would just speed up computation by stopping early if min_samples_leaf could not be satisfied.

@amueller

amueller commented Jan 6, 2013

Copy link
Copy Markdown
Member Author

Also it is not documented ;)

@glouppe

glouppe commented Jan 6, 2013

Copy link
Copy Markdown
Contributor

@amueller No, I think you are wrong. max is the correct way to do it. Recall that the higher min_samples_split, the stricter it is, since it stops earlier the construction of the tree. Hence I don't see how it could loosen the user requirements.

Also it is not documented ;)

I don't see that necessary since both min_samples_split and min_samples_leaf are already documented.

Anyway, I pushed a fix in #1488 so as not to overwrite self.min_samples_split. You are also right that it should be set to 2 by default. I'll fix that as well.

I'll fix that as well.

Done :)

@amueller

amueller commented Jan 6, 2013

Copy link
Copy Markdown
Member Author

Oh, sorry, I had a sign error as you noticed. As it doesn't change the behavior, it doesn't need to be documented. Sorry about that. Thanks for the fix!

@GaelVaroquaux

Copy link
Copy Markdown
Member

Currently the test is pretty strict: it checks that the init parameter is not
changed AT ALL during fit.
For default values that are set to None and instantiated later, this is a bit
stupid.

But on the other hand, sometimes None means a parameter will be set from the
data - so the none should stay there after fit.

What do you think would be a good policy?

I think that your choice is a good one. It is good to be able to rely on
a library not doing things behind ones back.

@GaelVaroquaux

Copy link
Copy Markdown
Member

I would rather be strict and use _ suffixed names for every attribute updated
from the data during the call to fit.

+1

@amueller

amueller commented Jan 7, 2013

Copy link
Copy Markdown
Member Author

Thanks for your feedback @GaelVaroquaux.

Btw, when @glouppe's fix via #1488 is merged, the only ones left are

GradientBoostingClassifier changes loss from deviance to mdeviance during fit.
GradientBoostingClassifier changes init from None to PriorProbabilityEstimator() during fit.
GradientBoostingClassifier changes max_features from None to 2 during fit.

Also setting the 0.13 milestone on this as I feel these are bugs (the doc state two subsequent calls of fit are independent - which was not the case with the random states and with max_features on GradientBoostingClassifier).

@amueller

Copy link
Copy Markdown
Member Author

I fixed the remaining issues in GradientBoosting. I think it's good to merge now.

@amueller

Copy link
Copy Markdown
Member Author

travis failure is mldata related

@amueller

Copy link
Copy Markdown
Member Author

@pprett if you're around I'd like to get your ok for my changes to GBRT

@pprett

pprett commented Jan 14, 2013

Copy link
Copy Markdown
Member

regarding GBRT: code looks good - all tests pass - all benchmarks look good

+1

thanks, andy

2013/1/14 Andreas Mueller [email protected]

@pprett https://github.com/pprett if you're around I'd like to get your
ok for my changes to GBRT

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/1518#issuecomment-12216284.

Peter Prettenhofer

@amueller

Copy link
Copy Markdown
Member Author

Thanks for checking @pprett :)

@zaxtax

zaxtax commented Jan 14, 2013

Copy link
Copy Markdown
Contributor

LGTM +1

@amueller

Copy link
Copy Markdown
Member Author

Ok, I'm merging this...

@amueller
amueller merged commit b2d3435 into scikit-learn:master Jan 15, 2013
@amueller
amueller deleted the test_fit_changes_params branch January 15, 2013 21:35
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.

6 participants