11
$\begingroup$

I was analyzing the classifier created using a decision tree. There is a tuning parameter called max_depth in scikit's decision tree. Is this equivalent of pruning a decision tree? If not, how could I prune a decision tree using scikit?

dt_ap = tree.DecisionTreeClassifier(random_state=1, max_depth=13)
boosted_dt = AdaBoostClassifier(dt_ap, random_state=1)
boosted_dt.fit(X_train, Y_train)
$\endgroup$
2
  • $\begingroup$ I have succeeded in implementing Cost complexity pruning on Sklearn's model, and here is the link: github.com/appleyuchi/Decision_Tree_Prune you may like it. $\endgroup$
    – appleyuchi
    Dec 10, 2018 at 4:14
  • $\begingroup$ While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review $\endgroup$
    – oW_
    Dec 10, 2018 at 19:47

2 Answers 2

9
$\begingroup$

Is this equivalent of pruning a decision tree?

Though they have similar goals (i.e. placing some restrictions to the model so that it doesn't grow very complex and overfit), max_depth isn't equivalent to pruning. The way pruning usually works is that go back through the tree and replace branches that do not help with leaf nodes.

If not, how could I prune a decision tree using scikit?

You can't through scikit-learn (without altering the source code).
Quote taken from the Decision Tree documentation: Mechanisms such as pruning (not currently supported)

If you want to post-prune a tree you have to do it on your own:
You can read this excellent post detailing how to do so.

$\endgroup$
1
$\begingroup$

Now with the new version 0.22.1, you can! It does pruning based on minimal cost-complexity pruning: the subtree with the largest cost complexity that is smaller than ccp_alpha will be chosen.

https://scikit-learn.org/stable/auto_examples/tree/plot_cost_complexity_pruning.html

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.