EZ LearnEZ Learn
Courses
EZ Board
Dashboard
All articles
ml-for-cyber-securitymlcsdecision-treescikit-learn

Decision Trees: Rules the Model Writes Itself

A decision tree learns, entirely on its own from data, which question to ask first. Left unconstrained, it will also learn to memorise the training set instead of the pattern inside it.

Ashish Revar25 October 20265 min read

A decision tree asks a sequence of yes/no questions about the features, each one splitting the data to make the two resulting groups as "pure" as possible, mostly one label or the other. For malware detection, a tree might learn, entirely on its own from data, that "does this file request the CreateRemoteThread API" is a far better first question than "is the file larger than 1 MB."

  1. At the current node, consider every feature and every possible threshold.
  2. For each candidate split, measure how much it reduces impurity (Gini impurity or entropy) in the two resulting groups.
  3. Choose the split that reduces impurity the most.
  4. Repeat inside each child node, until a stopping rule is reached (maximum depth, minimum samples per leaf, or no further useful split exists).
  5. A leaf's prediction is simply the majority label of the training examples that landed there.

decision_tree_phishing.py

from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split

# X: engineered URL features (length, digit ratio, has-IP-address,
#    subdomain count, HTTPS flag, ...); y: 1 = phishing, 0 = legitimate
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=7, stratify=y
)

tree = DecisionTreeClassifier(max_depth=5, min_samples_leaf=10, random_state=7)
tree.fit(X_train, y_train)
print(f"Test accuracy: {tree.score(X_test, y_test):.3f}")

Caution: A Tree That Memorises Is Not a Tree That Generalises

An unconstrained tree, grown without max_depth or min_samples_leaf, will keep splitting until every leaf is perfectly pure, including leaves with a single training example. That tree has not learned a pattern; it has memorised the training set, and its performance on new data is usually far worse than its training accuracy suggests. This is overfitting, and it is the single most common mistake in applied ML.

Try it yourself: Spot the Overfit

Two trees are trained on the same phishing-URL data. Tree A uses max_depth=5, min_samples_leaf=10 and scores 0.89 on training data and 0.86 on test data. Tree B uses no depth limit at all and scores 0.999 on training data and 0.71 on test data. Which tree would you actually deploy, and what single number in this comparison tells you why?

Related to this topic

Continue learning

Reference material

eBook

ML for Cyber Security eBook

v1.0

Open resource

Cheatsheet

ML for Cyber Security Cheatsheet

v1.0

Open resource

MCQ Bank

ML for Cyber Security MCQ Bank

v1.0

Open resource

Question Bank

ML for Cyber Security Question Bank

v1.0

Open resource

Discussion

Loading comments…
More articlesTest your knowledge
EZ MentorAI · Free

Decision Trees: Rules the Model Writes Itself

Hi! I'm EZ Mentor

Your AI tutor for
Decision Trees: Rules the Model Writes Itself

Sign in to ask, it's free.

EZ Mentor is AI-powered · Answers may not be perfect · 10 free questions / day

EZ LearnEZ Learn

Structured learning across Reverse Engineering, Cloud Security, and Research Methodology, plus games and practice tools in EZ Arena. Free, and open to anyone.

EZ Learn

  • Courses
  • EZ Articles
  • EZ Watch
  • EZ Talks
  • EZ Bytes
  • Cyber Hygiene

EZ Library

  • All Resources
  • eBooks & PDFs
  • Question Banks
  • MCQ Banks
  • Cheatsheets

EZ Exams

  • All Exams
  • REMA
  • Cloud Security
  • Research Methodology
  • Python for Cyber Security
  • Cyber Law & IPR
  • Community
  • EZ Board

EZ Arena

  • EZ Games
  • Live Quiz
  • ML Challenges
  • दिशा - Hygiene
  • सजग - Awareness
  • अभ्यास - Practice

Platform

  • About
  • Contact
  • Terms of Use
  • Disclaimer
  • Verify Certificate
  • Events

© 2026 EZ Learn by EpochZero. Educational content for learning purposes.

Platform Lead: Ashish Revar