Ravindra BagaleCourses & study guides मराठी Track your progress

35. Decision Trees and Random Forests in Python

35.4 Copy-Run a Small Random Forest

Why. Same data, stronger committee — RandomForest with a small n_estimators for a laptop-friendly demo.

What. RandomForestClassifier(n_estimators=50) builds 50 trees and majority-votes for the class.

How.

Fit a small forest

  1. Use the same X_train, y_train.
  2. Fit RandomForestClassifier.
  3. Compare predict on X_test with the single tree (spot-check only).
from sklearn.ensemble import RandomForestClassifier

forest = RandomForestClassifier(n_estimators=50, random_state=0)
forest.fit(X_train, y_train)
print("tree preds: ", tree.predict(X_test))
print("forest preds:", forest.predict(X_test))
print("feature_importances_:", forest.feature_importances_.round(3))

What you should see. Predictions from both models; importances that sum to about 1.0 — a rough “which column the forest used more,” not a causal proof.

Fictional data

Same teaching churn table — not a production CRM extract.