Skip to content

DecisionTreeRegressor creates splits for an exactly constant target #35434

Open

Description

@lcrmorin

Warning

This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.

DecisionTreeRegressor creates multiple leaves for a target whose values are all identical. Reproduced with scikit-learn 1.9.1 on Linux/Kaggle and macOS.

I found this as part of some work on using Trees for binning. I had some custom metric further down my code that goes infinite when the difference between consecutive bins would go to 0. I wrongly assumed it would not go to 0 since Trees would merge consecutive "bins" with same value.

Note that it seems to depends on the target value. For exemple it doesn't happen with 0.0 in the code below. I figured that when trying to figure the impact of standardisation on the binning approach. The use of decision tree on a degenerate constant target would return different numbers of bins based on whether the target would be standardised or not.

Steps/Code to Reproduce

import numpy as np
import sklearn
from sklearn.tree import DecisionTreeRegressor

X = np.arange(16, dtype=np.float64).reshape(-1, 1)
y = np.full(16, 10.1, dtype=np.float64)

model = DecisionTreeRegressor(
    max_leaf_nodes=8,
    random_state=101,
).fit(X, y)

print(sklearn.__version__)
print(model.get_n_leaves())
print(model.tree_.impurity[0])
print(model.tree_.threshold[model.tree_.children_left != -1])

Expected Results

No split for a constant target.

Actual Results

4
2.842170943040401e-14
[0.5 2.5 3.5]

Impurity is not 0.

Versions

1.9.1

Interest in fixing the bug

Possibly interested in fixing the bug. It might be tricky low level floating point cython stuff. But it might also be solved by some numpy is_close somewhere.

Edit: the impurity being non zero for a pure bin might be the issue. There is indeed a tolerance threshold, but it is set to machine error. The impurity can accumulate to a bigger error due to a sum of machine error.

Activity

  1. added
    Needs Triage
    on Nov 17, 2026
  2. cakedev0 commented on Nov 18, 2026

    @cakedev0
    Member

    Thanks for the report!

    As you say it might be tricky to fix (and too expensive in terms of code complexity and or performance compared to the benefits). I'm not sure it's worth the effort. But I'm pretty sure some big changes I planned for trees (for performance reasons) might fix it naturally!

    In the meantime, you can try with scikit-learn-intelex. They don't have decision trees but they have random forests (and you can pass n_estimators=1), and then rf.estimators_[0] gives you a scikit-learn decision tree. And the way they've implemented RFs should avoid this bug. Let me know if it works for you.

    For now, I'll label this low priority as I don't think it has a big impact. But I agree it's not super pretty (and not convenient for your use case).

  3. added
    Low Priority
    Needs Decision
    and removed
    Needs Triage
    on Nov 18, 2026
  4. github-actions commented on Nov 18, 2026

    @github-actions

    Thanks for the work you've done so far. The goal of this comment is to set expectations.

    Deciding on new features or substantial changes is a lengthy process. It frequently happens that no maintainer is available to take on this task right now.

    Please do not create a Pull Request before a decision has been made regarding the proposed work. Making this decision can often take a significant amount of time and effort.

  5. lcrmorin commented on Nov 18, 2026

    @lcrmorin
    Author

    Yes ML performance impact should be limited, as the tree still output the same value. I had a tought about it today... I think there are some impurity implementations that are both faster and more robust. I'll wait for the tree update.

  6. cakedev0 commented on Nov 18, 2026

    @cakedev0
    Member

    I think there are some impurity implementations that are both faster and more robust.

    Well, I'm curious about that! And it doesn't collide with what I plan to do which focuses on sorting. So feel free to share a small snippet or a PR on your fork to show your idea.

  7. cakedev0 commented on Nov 19, 2026

    @cakedev0
    Member

    Another very simple workaround: use min_impurity_decrease=1e-10.

    (Edit: Claude tells me to use min_impurity_decrease=1e-12 * np.mean(y**2) to be safer)

  8. lcrmorin commented on Nov 19, 2026

    @lcrmorin
    Author

    I feel like this parameter is a ML regularisation parameter, not necessarily to palliate some instable impurity calculations. Still you are right. I had in mind welford algorithm used for on-line correlation. It is indeed stabler, however it is not faster on a given array. Might be worth to check other available algorithms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugLow PriorityNeeds Decision

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions