Skip to content

Custom Criterion for Tree Splitting #17436

Done

Description

@ido-tera-group

Describe the workflow you want to enable

I want to be able to define a custom criterion for tree splitting when building decision trees / tree ensembles.
More specifically, it would be great to be able to base this criterion on features besides X & y (i.e. "Z"), and for that I will need the indexes of the samples being considered.

Describe your proposed solution

clf = DecisionTreeClassifier(..., criterion=my_func)
clf.fit(X, y)

def my_func(indexes):
return [purity score]

Describe alternatives you've considered, if relevant

I looked at the source .pyx code, but it's not clear to me how to start modifying it...
If there was a nice explanation somewhere, it would be a good start.

Additional context

I want to define a criterion that takes into consideration a confounding variable.

Activity

  1. amueller commented on Jun 3, 2020

    @amueller
    Member

    Hm I was sure there was an issue already open with this but I can't find it now. Indeed it would be nice to have an example of this in the user-guide or developer guide, it's a common request.

  2. NicolasHug commented on Jun 3, 2020

    @NicolasHug
    Member

    There was #7777 but without any actionable item

    About documenting this publicly: I'm a bit hesitant here. All this logic is private and we probably want to keep it this way for our own sanity?

    @ido-tera-group I'd recommend checking out how MSE is implemented (cdef class MSE(RegressionCriterion)) since it's probably the simplest criterion, and work your way up from there. The code can be a bit cryptic at times (often because of multi-output support) but it's reasonably well documented IMHO.

  3. ido-tera-group commented on Jun 3, 2020

    @ido-tera-group
    Author

    Thanks for the tips! :) Will look into it.

  4. glemaitre commented on Jun 4, 2020

    @glemaitre
    Member

    I also thought that we made the header available such that it is easy to inherate and create a criterion. However, we did not document it and do not officially support it to still be able to make breaking changes at the low-level criterion.

  5. zealseeker commented on Nov 11, 2020

    @zealseeker

    I am making a custom criterion. Here is my way:
    Create a new class mimicking an existing criterion class, such as Entropy (https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/_criterion.pyx#L505-L585). So we just need to define two functions.

    Then you need to instantiate the criterion. (See how the classier instantiate it internally when you give a string rather than a criterion object, https://github.com/scikit-learn/scikit-learn/blob/0fb307bf3/sklearn/tree/_classes.py#L331-L337)

    This is similar to the suggestion from @NicolasHug I think.

    But you have to compile by yourself if you change pyx. I don't know whether there is a better way (e.g. define the class in a python script).

  6. lorentzenchr commented on Nov 30, 2026

    @lorentzenchr
    Member

    We seem to prefer to keep all our Cython private. Therefore we also don’t want to document "hacking" our tree code.
    If a specific criterion is missing, we can discuss it in a new issue.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions