You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 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.
Reacted by Fernando Marcos Wittmann, Alberto Santini, hori-ryota, suhelhammoud, Jeremy Feng and haggaishachar-ma
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.
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.
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.
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.
Activity
amueller commentedon Jun 3, 2020
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.
NicolasHug commentedon Jun 3, 2020
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.ido-tera-group commentedon Jun 3, 2020
Thanks for the tips! :) Will look into it.
glemaitre commentedon Jun 4, 2020
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.
zealseeker commentedon Nov 11, 2020
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).
lorentzenchr commentedon Nov 30, 2026
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.