-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
Extending Criterion #10251
Description
Unless I'm missing something, it's not completely trivial how one can use a custom sklearn.tree._criterion.Criterion for a decision tree. See my use case here.
Things I have tried include:
-
Import the
ClassificationCriterionin Python and subclass it. It seems thatnode_impurityandchildren_impuritydo not get called, the impurity is always 0 (perhaps because they arecdefand notcpdef?). I'm also unsure what the parameters to__new__/__cinit__should be (e.g.1andnp.array([2], dtype='intp')for a binary classification problem?), or how to pass them properly: I have to create theCriterionobject from outside the tree to circumvent the check on thecriterionargument. -
Extend
ClassificationCriterionin a Cython file. This seems to work, but (a) it requires exportingClassificationCriterionfrom_criterion.pxdand (b) it would be nice if it would be documented more extensively what should be done innode_impurityandchildren_impurity. I will post my code below once it seems to work correctly.
May I propose one of the following to make this easier?
- Document what should be done to extend the class in Cython or Python - if Python should be allowed: I am aware of the performance issue with that, but in some cases it may be OK to do this in Python - I don't know.
- Make it possible to pass a function or other object not extending
Criterionto the tree, similar to how it is very easy to implement a custom scorer for validation functions. That would require changing the checks here.