Decision Tree

Python · Machine Learning (Scikit-Learn)

Theory

A decision tree predicts by asking a sequence of yes/no questions about the features, like a flowchart: 'is credit <= 723.5?', then 'is income <= 5.85?', until it reaches a leaf that gives the prediction. During fit it tries every feature and threshold and picks the split that makes the groups purest, measured by Gini impurity (or entropy) for classification and squared error for regression. It repeats this on each branch.

Trees handle numbers and encoded categories, need no scaling and can be read as rules with export_text. Their weakness is overfitting: left unchecked, a tree keeps splitting until it memorises the training data. Limit it with max_depth, min_samples_leaf or min_samples_split, and check train against test scores. feature_importances_ shows which features drove the splits.

Why we use it

Trees capture non-linear patterns and interactions automatically and produce rules that business users can follow, such as a clear credit approval policy. They are also the building block of random forests and gradient boosting.

When to use it

Use a shallow tree when you need explainable rules or a quick non-linear baseline. For the best accuracy, prefer a random forest or gradient boosting, which average many trees. Always limit depth to avoid overfitting.

How it works

  1. All training rows at the root
  2. Try every feature and threshold
  3. Pick the split that most reduces Gini impurity
  4. Reached max_depth or node already pure? Yes: Make a leaf: majority class. No: Split this node again.
  5. Repeat for each branch
  6. Rules: follow questions down to a leaf

Syntax

from sklearn.tree import DecisionTreeClassifier, export_text
tree = DecisionTreeClassifier(max_depth=3, min_samples_leaf=5, random_state=0)
tree.fit(X_train, y_train)
print(export_text(tree, feature_names=list(X.columns)))

Example

tree = DecisionTreeClassifier(max_depth=2, random_state=0).fit(X_train, y_train)
print(export_text(tree, feature_names=list(X.columns)))

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