Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Decision Trees and R | Master Data Science with Python lesso | BraveProgrammer
  1. Courses
  2. /
  3. Master Data Science with Python

Lesson 49 of 60

·python

Decision Trees and Random Forests

30 minutes

Course progress82%

Decision Trees & Random Forests

Tree‑based models are intuitive, handle mixed data types, and require little preprocessing.

Decision Tree basics

  • Splits data based on feature thresholds.
  • Uses impurity measures: Gini, entropy (information gain).

Training a Decision Tree with scikit‑learn

from sklearn.tree import DecisionTreeClassifier
 sklearn  tree

clf = DecisionTreeClassifier(max_depth=, random_state=)
clf.fit(X_train, y_train)


plt.figure(figsize=(,))
tree.plot_tree(clf, filled=, feature_names=X.columns, class_names=[,])
plt.show()
from
import
5
42
# Visualize the tree
12
8
True
'No'
'Yes'

Random Forests (ensemble of trees)

from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=200, max_features='sqrt', random_state=42)
rf.fit(X_train, y_train)

# Feature importance
importances = pd.Series(rf.feature_importances_, index=X.columns)
importances.sort_values(ascending=False).plot(kind='bar')
plt.title('Feature Importances from Random Forest')
plt.show()

Hyperparameters to tune

  • n_estimators (number of trees)
  • max_depth
  • min_samples_split / min_samples_leaf
  • max_features

Advantages & disadvantages

Decision TreeRandom Forest
InterpretabilityHigh (visual)Medium (feature importance)
OverfittingProneLess prone (averaging)
Training speedFastSlower (many trees)

Out‑of‑Bag (OOB) error estimate

rf.oob_score_   # gives OOB accuracy for classification

Info

Random Forests handle missing values internally (by using surrogate splits) but it's better to clean data first.
Make it stickPractice what you've learned1 quiz · 4 practical tests · Optional activities from Master Data Science with PythonBrowse activities
Previous lessonSupervised Learning – Logistic RegressionContinue to lesson 50Model Evaluation Metrics for ClassificationBack to Master Data Science with Python curriculum