Repository navigation
Description
Description
I've been working on #7301, and I found some seemingly duplicate logic in the decision tree code. At the least, it's something that requires more documentation; at the most, it's a bug.
So I was poking around, trying to figure out why I couldn't get equal trees for uniform sample weights when i set min_weight_fraction_leaf and min_samples_leaf to the same float value. I saw that the two built trees actually had radically different values of min_samples_split, despite me not setting this value at all. In the code (https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/tree.py#L229), it shows that min_samples_split is set to the max(min_samples_split, 2 * min_samples_leaf). This isn't documented at all, and should be if we decide to keep this statement.
Now this logic makes sense to me, as you can't make a split if there's no way to split it in such a manner that each leaf has the minimum number of samples. However, isn't this sort of logic encapsulated when we're building the tree? Namely, I'm thinking of the is_leaf conditional, which determines whether to split on a node or not (https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/_tree.pyx#L219):
is_leaf = ((depth >= max_depth) or
(n_node_samples < min_samples_split) or
(n_node_samples < 2 * min_samples_leaf) or
(weighted_n_node_samples < min_weight_leaf))
I think the statement (n_node_samples < 2 * min_samples_leaf) has the same effect as setting min_samples_split explicitly to the max of itself or 2*min_samples_leaf. Indeed, removing the max call does not cause any tests to fail.
Should we be removing the explicit call to max? It seems like a piece of duplicate logic, but maybe I'm missing something. ping @glouppe @raghavrv @jmschrei / anyone else.
Activity
nelson-liu commentedon Sep 4, 2016
Additionally, there's a separate issue of how
is_leafhandlesmin_weight_leafandmin_samples_leafdifferently (sincemin_samples_leafis multiplied by 2), which means that trees grown on equal weights with equivalent values ofmin_weight_leafandmin_samples_leafwill not be grown in the same manner. Any suggestions for fixing this? I'm not sure removing the2*min_samples_leafand the(min_samples_split, 2 * min_samples_leaf)is technically sound, but doing so doesn't cause any tests to fail (at least on my local machine, unsure about CI yet) and givesmin_weight_leafandmin_samples_leafequal interpretations of the float parameter in the case when the samples weights are uniform.jnothman commentedon Sep 4, 2016
Input from @glouppe would be most welcome.
jnothman commentedon Sep 4, 2016
Doesn't setting
is_leafto True whereweighted_n_node_samples < min_weight_leafmean that the required minimum is not upheld?jnothman commentedon Sep 4, 2016
I suspect that check is redundant and never used, as long as the splitters correctly ensure that no split results in
min_weight_leafbeing violated.nelson-liu commentedon Sep 4, 2016
Indeed, the main issue is that given two trees with only
min_weight_fraction_leafandmin_samples_split, the two trees grow differently because there are times wheren_node_samples < 2*min_samples_leafbutweighted_n_node_samples is not < min_weight_leaf. This is in contrary to the idea that they should give equivalent results in certain cases.jnothman commentedon Sep 4, 2016
I would hope the condition there regarding min_weight_leaf is never
executed. And I would think that the min_samples_leaf criterion there is
mostly a shortcut: the splitter would give up if asked to split that node,
wouldn't it? So unless I'm wrong in my presumptions, if there's actually a
case where these differ, then there might be a bug elsewhere....??
On 5 September 2016 at 00:06, Nelson Liu [email protected] wrote:
nelson-liu commentedon Sep 4, 2016
well this is the code i'm running:
In est_weight, there are definitely cases where
n_node_samples < 2 * min_samples_leaf(min_samples_leafis set to 2 by default) is false andweighted_n_node_samples < min_weight_leafistrue. This basically happens ifmin_weight_leafis greater than 4).In est_sample,
min_weight_fraction_leaf = 0.0(by default) and thus the condition withmin_weight_leafis never executed (sincemin_samples_leafis always greater than 0).Regardless, it shows that these two if statements are not executing the same thing at all, which leads to (incorrect) growth of different trees.
nelson-liu commentedon Sep 4, 2016
opened a PR at #7340 so you can see the changes i am proposing.
jnothman commentedon Sep 4, 2016
But aren't these cases eliminated by the Splitter checks on
min_weight_leaf? is themin_weight_leafcriterion actually violated in the final tree?nelson-liu commentedon Sep 4, 2016
No the
min_weight_leafcriterion is not violated in the final tree. Im not sure if the cases are eliminated by the splitter checks, but supposedly the ones that are incorrectly marked as a leaf node (fulfill one of the conditions) do not get split on further.jnothman commentedon Sep 4, 2016
Yes, but if one is marked as a leaf node that has weighted_n_node_samples <
min_weight_leaf then it will violate the min_weight_leaf condition... but
if the splitter then rejects it it won't be retained in the final tree. Of
course this means that the min_weight_leaf case tries more splits than
the min_samples_leaf, which would create differences if there are RNGs
involved.
On 5 September 2016 at 09:27, Nelson Liu [email protected] wrote:
jnothman commentedon Sep 4, 2016
I will admit I'm not familiar enough with the code to know when splits are
validated relative to this code.
On 5 September 2016 at 09:30, Joel Nothman [email protected] wrote:
jnothman commentedon Sep 5, 2016
If things are working as I suspect, then this condition regarding
min_samples_leafis only an optimisation, allowing the splitter not to descend that path. However, by doing so, therandom_stateis not altered where it is in themin_weight_leafpath (since the splitter needs to descend farther), so we cannot expect the resultant trees to be identical.nelson-liu commentedon Sep 5, 2016
Yeah, that makes sense. I feel like it is logical for us to expect them to be identical, though (in the sense that I think this would be the correct behavior) ? Considering I'd think that building two separate trees, one with a set
min_weight_fraction_leafand another with that same value pegged tomin_samples_leafon uniformly weighted data should have the same interpretation and output tree?jnothman commentedon Sep 5, 2016
Their effect as regularisers is identical, but
min_samples_splitcan be implemented more efficiently, and results in a different tree due to randomisation. I don't think that's a problem, but you're welcome to propose a note in the documentation. And yes, to get rid of that incorrect condition.nelson-liu commentedon Sep 5, 2016
Fair enough, I'll remove the test that sees whether the two parameters have the same effect in #7301, since the trees are different due to randomization. Thanks for the clarity @jnothman
jnothman commentedon Sep 5, 2016
The clarity came from collaboration. Thank you.q
amueller commentedon Sep 6, 2016
the random state is only used for tie breaking. Are the results also different for data with no ties?
I didn't go through it in detail, but it sounds to me like the
min_weight_leafstops to early. Can you show the visualizations of the trees (if they are small enough)?nelson-liu commentedon Sep 6, 2016
Here are visualizations of the following trees:
the output:
The trees:

sample:
weight:

amueller commentedon Sep 6, 2016
is that tie breaking? that's not obvious to me
nelson-liu commentedon Sep 6, 2016
no, I don't think there are any ties...
amueller commentedon Sep 14, 2016
this question is still open, right?
glouppe commentedon Sep 29, 2016
Closed in #7441