Random Forest

Python · Machine Learning (Scikit-Learn)

Theory

A random forest is an ensemble of many decision trees whose votes are combined. Each tree is trained on a bootstrap sample (rows drawn with replacement) and, at every split, considers only a random subset of features. The trees therefore make different mistakes, and the forest's majority vote (or average, for regression) is much more stable and accurate than any single tree.

The main settings are n_estimators (number of trees; more is steadier but slower), max_depth and min_samples_leaf (tree size) and max_features (features tried per split). Like trees, forests need no scaling. feature_importances_ gives the average impurity reduction each feature produced, a quick view of what drives predictions, though it can favour features with many distinct values. oob_score=True scores each tree on the rows it did not see, a free validation estimate.

Why we use it

Forests are among the most reliable out-of-the-box models for tabular data: accurate, resistant to overfitting compared with a single tree, robust to outliers and unscaled features, and they rank feature importance for free.

When to use it

Use a random forest as a strong default for classification or regression on tables, especially with non-linear effects. Choose a single tree when rules must be readable, linear models when coefficients must be explained, and gradient boosting when squeezing out the last bit of accuracy.

How it works

  1. Training data
  2. Draw a bootstrap sample of rows for each tree
  3. Grow each tree, trying a random subset of features at each split
  4. Repeat for n_estimators trees
  5. Classification? Yes: Majority vote of the trees. No: Average of the trees.
  6. Final prediction + feature importances

Syntax

from sklearn.ensemble import RandomForestClassifier
forest = RandomForestClassifier(n_estimators=200, max_depth=None,
                                min_samples_leaf=1, random_state=0)
forest.fit(X_train, y_train)
forest.feature_importances_

Example

forest = RandomForestClassifier(n_estimators=200, random_state=0).fit(X_train, y_train)
print(forest.score(X_test, y_test))
print(forest.feature_importances_)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic