.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/tree/plot_cost_complexity_pruning.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_tree_plot_cost_complexity_pruning.py: ======================================================== Post pruning decision trees with cost complexity pruning ======================================================== .. currentmodule:: sklearn.tree The :class:`DecisionTreeClassifier` provides parameters such as ``min_samples_leaf`` and ``max_depth`` to prevent a tree from overfiting. Cost complexity pruning provides another option to control the size of a tree. In :class:`DecisionTreeClassifier`, this pruning technique is parameterized by the cost complexity parameter, ``ccp_alpha``. Greater values of ``ccp_alpha`` increase the number of nodes pruned. Here we only show the effect of ``ccp_alpha`` on regularizing the trees and how to choose a ``ccp_alpha`` based on validation scores. See also :ref:`minimal_cost_complexity_pruning` for details on pruning. .. GENERATED FROM PYTHON SOURCE LINES 19-29 .. code-block:: Python # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause import matplotlib.pyplot as plt from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeClassifier .. GENERATED FROM PYTHON SOURCE LINES 30-40 Total impurity of leaves vs effective alphas of pruned tree --------------------------------------------------------------- Minimal cost complexity pruning recursively finds the node with the "weakest link". The weakest link is characterized by an effective alpha, where the nodes with the smallest effective alpha are pruned first. To get an idea of what values of ``ccp_alpha`` could be appropriate, scikit-learn provides :func:`DecisionTreeClassifier.cost_complexity_pruning_path` that returns the effective alphas and the corresponding total leaf impurities at each step of the pruning process. As alpha increases, more of the tree is pruned, which increases the total impurity of its leaves. .. GENERATED FROM PYTHON SOURCE LINES 40-47 .. code-block:: Python X, y = load_breast_cancer(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) clf = DecisionTreeClassifier(random_state=0) path = clf.cost_complexity_pruning_path(X_train, y_train) ccp_alphas, impurities = path.ccp_alphas, path.impurities .. GENERATED FROM PYTHON SOURCE LINES 48-50 In the following plot, the maximum effective alpha value is removed, because it is the trivial tree with only one node. .. GENERATED FROM PYTHON SOURCE LINES 50-56 .. code-block:: Python fig, ax = plt.subplots() ax.plot(ccp_alphas[:-1], impurities[:-1], marker="o", drawstyle="steps-post") ax.set_xlabel("effective alpha") ax.set_ylabel("total impurity of leaves") ax.set_title("Total Impurity vs effective alpha for training set") .. image-sg:: /auto_examples/tree/images/sphx_glr_plot_cost_complexity_pruning_001.png :alt: Total Impurity vs effective alpha for training set :srcset: /auto_examples/tree/images/sphx_glr_plot_cost_complexity_pruning_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Total Impurity vs effective alpha for training set') .. GENERATED FROM PYTHON SOURCE LINES 57-60 Next, we train a decision tree using the effective alphas. The last value in ``ccp_alphas`` is the alpha value that prunes the whole tree, leaving the tree, ``clfs[-1]``, with one node. .. GENERATED FROM PYTHON SOURCE LINES 60-71 .. code-block:: Python clfs = [] for ccp_alpha in ccp_alphas: clf = DecisionTreeClassifier(random_state=0, ccp_alpha=ccp_alpha) clf.fit(X_train, y_train) clfs.append(clf) print( "Number of nodes in the last tree is: {} with ccp_alpha: {}".format( clfs[-1].tree_.node_count, ccp_alphas[-1] ) ) .. rst-class:: sphx-glr-script-out .. code-block:: none Number of nodes in the last tree is: 1 with ccp_alpha: 0.3272984419327777 .. GENERATED FROM PYTHON SOURCE LINES 72-76 For the remainder of this example, we remove the last element in ``clfs`` and ``ccp_alphas``, because it is the trivial tree with only one node. Here we show that the number of nodes and tree depth decreases as alpha increases. .. GENERATED FROM PYTHON SOURCE LINES 76-92 .. code-block:: Python clfs = clfs[:-1] ccp_alphas = ccp_alphas[:-1] node_counts = [clf.tree_.node_count for clf in clfs] depth = [clf.tree_.max_depth for clf in clfs] fig, ax = plt.subplots(2, 1) ax[0].plot(ccp_alphas, node_counts, marker="o", drawstyle="steps-post") ax[0].set_xlabel("alpha") ax[0].set_ylabel("number of nodes") ax[0].set_title("Number of nodes vs alpha") ax[1].plot(ccp_alphas, depth, marker="o", drawstyle="steps-post") ax[1].set_xlabel("alpha") ax[1].set_ylabel("depth of tree") ax[1].set_title("Depth vs alpha") fig.tight_layout() .. image-sg:: /auto_examples/tree/images/sphx_glr_plot_cost_complexity_pruning_002.png :alt: Number of nodes vs alpha, Depth vs alpha :srcset: /auto_examples/tree/images/sphx_glr_plot_cost_complexity_pruning_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 93-100 Accuracy vs alpha for training and testing sets ---------------------------------------------------- When ``ccp_alpha`` is set to zero and keeping the other default parameters of :class:`DecisionTreeClassifier`, the tree overfits, leading to a 100% training accuracy and 88% testing accuracy. As alpha increases, more of the tree is pruned, thus creating a decision tree that generalizes better. In this example, setting ``ccp_alpha=0.015`` maximizes the testing accuracy. .. GENERATED FROM PYTHON SOURCE LINES 100-111 .. code-block:: Python train_scores = [clf.score(X_train, y_train) for clf in clfs] test_scores = [clf.score(X_test, y_test) for clf in clfs] fig, ax = plt.subplots() ax.set_xlabel("alpha") ax.set_ylabel("accuracy") ax.set_title("Accuracy vs alpha for training and testing sets") ax.plot(ccp_alphas, train_scores, marker="o", label="train", drawstyle="steps-post") ax.plot(ccp_alphas, test_scores, marker="o", label="test", drawstyle="steps-post") ax.legend() plt.show() .. image-sg:: /auto_examples/tree/images/sphx_glr_plot_cost_complexity_pruning_003.png :alt: Accuracy vs alpha for training and testing sets :srcset: /auto_examples/tree/images/sphx_glr_plot_cost_complexity_pruning_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.392 seconds) .. _sphx_glr_download_auto_examples_tree_plot_cost_complexity_pruning.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/tree/plot_cost_complexity_pruning.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_cost_complexity_pruning.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_cost_complexity_pruning.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_cost_complexity_pruning.zip ` .. include:: plot_cost_complexity_pruning.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_