Trees and forests

DecisionTreeClassifier and RandomForestClassifier.

A decision tree splits on one feature at a time. max_depth keeps it short. A random forest averages many trees. Trees do not need scaling. They can overfit if you let them grow without a depth cap.

Goal

Fit a shallow tree and a small forest, print accuracy, and list feature importances.

Decision tree

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

X, y = make_classification(n_samples=200, n_features=4, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=0
)
tree = DecisionTreeClassifier(max_depth=3, random_state=0)
tree.fit(X_train, y_train)
print("accuracy:", round(accuracy_score(y_test, tree.predict(X_test)), 3))
print("importances", tree.feature_importances_.round(3))

Random forest

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

X, y = make_classification(n_samples=200, n_features=4, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=0
)
rf = RandomForestClassifier(n_estimators=40, max_depth=5, random_state=0, n_jobs=1)
rf.fit(X_train, y_train)
print("accuracy:", round(accuracy_score(y_test, rf.predict(X_test)), 3))
print("importances", rf.feature_importances_.round(3))

n_jobs=1 is required here — extra processes are not useful in this tab. n_estimators=40 is enough for the demo.

Importances as a bar chart

from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier

X, y = make_classification(n_samples=200, n_features=4, random_state=0)
rf = RandomForestClassifier(n_estimators=40, random_state=0, n_jobs=1)
rf.fit(X, y)
names = [f"f{i}" for i in range(X.shape[1])]
plt.bar(names, rf.feature_importances_)
plt.ylabel("importance")
plt.title("Random forest")
plt.show()
Pitfall

A deep tree (max_depth=None) can memorize train. Compare train vs test accuracy; a wide gap means overfit.