Practical 5: Decision Trees & Random Forest

Objective

Build tree-based classification models and understand ensemble methods.

Duration

3-4 hours

Prerequisites


What You’ll Learn


📋 Tasks

1. Build Decision Tree

from sklearn.tree import DecisionTreeClassifier

dt = DecisionTreeClassifier(max_depth=5)
dt.fit(X_train, y_train)

2. Build Random Forest

from sklearn.ensemble import RandomForestClassifier

rf = RandomForestClassifier(n_estimators=100, max_depth=10)
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)

3. Feature Importance

importances = rf.feature_importances_
for name, importance in zip(feature_names, importances):
    print(f"{name}: {importance:.4f}")

📊 Learning Outcomes


Next: Practical 6 → ← Back to Practicals