Decision Trees and Random Forests · Learning Materials
English translation
Topics covered
Choose model complexity by checking whether a tree that fits the training data well also performs well on new data.
Tree Questions and Complexity
A Decision Tree splits data using feature values. Greater depth can fit detailed differences in training data but increase errors on new data.
Comparing Models under the Same Conditions
Random Forest combines results from multiple trees. Compare it with a single tree using the same split, preprocessing, and metrics to interpret differences in complexity. Examine both accuracy and errors by class.
Worked example
Split the Iris data with test_size=.3, random_state=7, stratify=y. Use depths 1,3,None.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X,y=load_iris(return_X_y=True)
Xtr,Xte,ytr,yte=train_test_split(X,y,test_size=.3,random_state=7,stratify=y)
model=DecisionTreeClassifier(max_depth=3,random_state=7).fit(Xtr,ytr)
print(model.score(Xte,yte))
0.9777777777777777
Reference example
View more examples