Skip to content

Cloning decision tree estimators breaks criterion objects #6420

Closed

Description

@panisson

I'm trying to implement different criterions for decision trees.
I've found that decision trees could accept a Criterion object as a criterion parameter:
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/tree.py#L335
And the easiest way to implement other criterions would be to implement subclasses of tree._criterion.Criterion class.

The normal way to pass a criterion to a decision tree is by using its string name, and it works fine:

from sklearn import tree, model_selection, metrics, datasets
import numpy as np

X, y = datasets.make_classification(n_samples=1000, random_state=42)
cv = model_selection.KFold(n_folds=10, shuffle=True, random_state=43)

dtc = tree.DecisionTreeClassifier(criterion='gini', random_state=42)
print np.mean(model_selection.cross_val_score(dtc, X, y, cv=cv))

mean score is 0.866.

However, if I use a Criterion object, it does not work anymore:

gini = tree._criterion.Gini(n_outputs=1, n_classes=np.array([2]))
dtc = tree.DecisionTreeClassifier(criterion=gini, random_state=42)
print np.mean(model_selection.cross_val_score(dtc, X, y, cv=cv))

mean score now is 0.476.
It seems that the cloning of the decision tree is breaking the criterion object in some way, because this code is also not working:

from sklearn.base import clone

gini = tree._criterion.Gini(n_outputs=1, n_classes=np.array([2]))

dtc = tree.DecisionTreeClassifier(criterion=gini, random_state=42)
scores = []
for train_idx, test_idx in cv.split(X, y):
    estimator = clone(dtc)
    estimator.fit(X[train_idx], y[train_idx])
    scores.append(metrics.accuracy_score(y[test_idx], estimator.predict(X[test_idx])))
print np.mean(scores)

but if I reset the criterion object of the estimator by, e.g.,

estimator.criterion = dtc.criterion

then the score values are back to normal.

I could not find where the cloning is breaking the criterion object, any help would be welcome.

Thanks all for your effort on this project, sklearn is really great!

regards
André

Activity

  1. amueller commented on Oct 7, 2016

    @amueller
    Member
  2. jnothman commented on Oct 8, 2016

    @jnothman
    Member
    >>> gini = tree._criterion.Gini(n_outputs=1, n_classes=np.array([2]))
    >>> dtc = tree.DecisionTreeClassifier(criterion=gini, random_state=42)
    >>> gini.__reduce__()
    (sklearn.tree._criterion.ClassificationCriterion, (1, array([2])), {})
    >>> base.clone(dtc).criterion.__reduce__()
    (sklearn.tree._criterion.ClassificationCriterion, (1, array([5764616311966949241])), {})

    clone relies on copy.deepcopy:

    >>> copy.deepcopy(gini).__reduce__()
    (sklearn.tree._criterion.ClassificationCriterion, (1, array([4314681712])), {})
  3. amueller commented on Oct 8, 2016

    @amueller
    Member

    So deepcopy of criteria is broken?

  4. jnothman commented on Oct 8, 2016

    @jnothman
    Member

    Yes, though I've not worked out why, despite spending some time tracing deepcopy.

  5. added
    Moderate
    on Oct 13, 2016
  6. jnothman commented on Oct 13, 2016

    @jnothman
    Member

    Need Debugger label?

  7. amueller commented on Oct 13, 2016

    @amueller
    Member

    is need debugger the same as need contributor or as need reviewer or something else? lol

  8. olologin commented on Oct 16, 2016

    @olologin
    Contributor

    Looks like I found the problem, np.array object returned from repr doesn't own underlying memory, so unerlying memory gets freed after ClassificationCriterion is destroyed.

    copy.deepcopy(gini).reduce()
    here we get reduce result from copy, and then copy object is removed, after this reduce result is filled with garbage.

    Also, there are some other funny problems, like:

    gini = tree._criterion.Gini(n_outputs=1, n_classes=np.array([2]))
    gini_copy = copy.deepcopy(gini)
    gini_copy.__class__
    Out[1]: sklearn.tree._criterion.ClassificationCriterion
    

    I'll try to fix this and make PR.

  9. olologin commented on Oct 16, 2016

    @olologin
    Contributor

    @panisson, Could you check your original problem on this branch #7680 ?

  10. panisson commented on Oct 18, 2016

    @panisson
    Author

    This branch solves my original problem. Thanks!

  11. added 2 commits that reference this issue on Oct 19, 2016
    da5a85b
    98eaad3
  12. added a commit that references this issue on Oct 19, 2016
    74e4c42
  13. added a commit that references this issue on Oct 25, 2016
    0d69158
  14. added a commit that references this issue on Nov 9, 2016
    2bb913d
  15. added a commit that references this issue on Jun 14, 2017
    2f7f34c
  16. added 3 commits that reference this issue on Jul 27, 2017
    f558cc9
    95ce561
    fbf642d
  17. added a commit that references this issue on Aug 19, 2017
    1fcfc36
  18. added a commit that references this issue on Nov 15, 2017
    2255f4f
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

    BugModerate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions