decisiontreeclassifier classes scikit-learn only implements pre-pruningnot post-pruning let' look at the effect of pre-pruning in more detail on the breast cancer dataset as alwayswe import the dataset and split it into training and test part then we build model using the default setting of fully developing the tree (growing the tree until all leaves are purewe fix the random_state in the treewhich is used for tiebreaking internallyin[ ]from sklearn tree import decisiontreeclassifier cancer load_breast_cancer(x_trainx_testy_trainy_test train_test_splitcancer datacancer targetstratify=cancer targetrandom_state= tree decisiontreeclassifier(random_state= tree fit(x_trainy_trainprint("accuracy on training set{ }format(tree score(x_trainy_train))print("accuracy on test set{ }format(tree score(x_testy_test))out[ ]accuracy on training set accuracy on test set as expectedthe accuracy on the training set is %--because the leaves are purethe tree was grown deep enough that it could perfectly memorize all the labels on the training data the test set accuracy is slightly worse than for the linear models we looked at previouslywhich had around accuracy if we don' restrict the depth of decision treethe tree can become arbitrarily deep and complex unpruned trees are therefore prone to overfitting and not generalizing well to new data now let' apply pre-pruning to the treewhich will stop developing the tree before we perfectly fit to the training data one option is to stop building the tree after certain depth has been reached here we set max_depth= meaning only four consecutive questions can be asked (cf figures - and - limiting the depth of the tree decreases overfitting this leads to lower accuracy on the training setbut an improvement on the test setin[ ]tree decisiontreeclassifier(max_depth= random_state= tree fit(x_trainy_trainprint("accuracy on training set{ }format(tree score(x_trainy_train))print("accuracy on test set{ }format(tree score(x_testy_test))supervised machine learning algorithms