Friday, November 27, 2026

Desision Tree

Inside Classification: How Machines Learn to Sort the World

Inside Classification: How Machines Learn to Sort the World

Every day, machines sort things for us. Your email filter decides what counts as spam, a bank decides whether a loan is risky, and a hospital system flags scans that need a doctor's attention. Behind many of these decisions is a family of techniques called classification. This post walks through the core ideas, from intuition to arithmetic.

What Classification Really Means

Classification is a form of supervised learning. We give a model examples where the correct answer is already known, and the model learns a pattern it can apply to examples it has never seen.

The process has two phases:

  • Training: the model sees labelled records, each described by attributes (such as colour, size, or income) and tagged with a class (such as "fraud" or "not fraud"). It learns how the attributes relate to the class.
  • Prediction: the model receives a new record with no label and assigns it to one of the known classes.

Classification is not the same as regression. Regression predicts a continuous number, such as a house price. Classification predicts a category. Every technique below is a different way of building the mapping from attributes to classes.

Decision Trees: Learning by Asking Questions

Imagine a bank officer deciding whether a refund claim is fraudulent. She might ask: Was the refund requested within 30 days? Does the customer have a high income? Has this account filed claims before? Each answer narrows the decision until she reaches a verdict. That is how a decision tree works.

Anatomy of a tree

  • The root node holds the first question.
  • Internal nodes each test one attribute.
  • Branches represent the possible answers to a test.
  • Leaf nodes hold the final class label.

To classify a record, start at the root, follow the branch matching the record's value, and continue until you reach a leaf.

Building a tree: Hunt's idea

  1. Start with all training records at the root.
  2. If every record in a node has the same class, make that node a leaf.
  3. Otherwise, pick the attribute test that separates the classes best.
  4. Split the records according to the test's outcomes.
  5. Repeat on each child node.

The procedure is greedy: at each step it picks the best split for that node alone, without looking ahead. Greedy choices are fast and usually work well, though they do not guarantee a globally optimal tree.

Expressing the Question: Attribute Test Conditions

The form of a test depends on the type of attribute.

Nominal attributes, such as colour, have no natural order. Use one branch per value (multiway), or group the values into two sets (binary), such as {red} versus {blue, green}.

Ordinal attributes, such as clothing size (S, M, L), have an order. Groups must respect that order. Grouping {S, L} against {M} throws away information the ordering provides.

Continuous attributes, such as age, are usually split with a threshold: "Age ≤ 35" versus "Age > 35".

Binary splits are simpler to search and are used by CART. Multiway splits are common for nominal data in ID3.

Choosing the Best Split

When a node holds a mix of classes, we measure impurity: how mixed the classes are. A pure node contains one class, and a good split produces children that are purer than their parent.

Three impurity measures

For a node where p(i) is the fraction of records in class i:

  • Entropy = −Σ p(i) · log₂ p(i). Zero for a pure node, largest when classes are evenly mixed.
  • Gini index = 1 − Σ p(i)². Also zero when pure and largest when mixed. Used by CART.
  • Classification error = 1 − max p(i). The fraction misclassified if the node predicts its majority class. Simple, but less sensitive to changes in class mix, so rarely used to grow trees.

Measuring a split's quality

Gain = I(parent) − Σ (Nj / N) · I(childj)

Each child's impurity is weighted by the fraction of records it receives, so a split that sends most records into one pure child is rewarded.

  • Information gain uses entropy (ID3).
  • Gini gain uses the Gini index (CART).
  • Gain ratio divides information gain by a split-information term that penalises attributes with many distinct values. An ID column would split data perfectly but generalise terribly. C4.5 uses this measure to avoid that trap.

A worked example

A parent node has 10 records: 5 of class A and 5 of class B. One candidate split sends 4 A and 1 B left, and 1 A and 4 B right.

  • Parent Gini = 1 − (0.5² + 0.5²) = 0.50
  • Each child Gini = 1 − (0.8² + 0.2²) = 0.32
  • Weighted child Gini = 0.5 × 0.32 + 0.5 × 0.32 = 0.32
  • Gini gain = 0.50 − 0.32 = 0.18

With entropy, the parent is 1.0 and each child is about 0.722, so the information gain is about 0.278. With classification error, the error gain is 0.30.

All three measures agree this split is informative. A split that sends 5 A to one side and 5 B to the other would produce pure children and maximum gain.

The Algorithm in Full

TreeGrow(records, attributes):
    if all records share one class      → return leaf(that class)
    if attributes is empty              → return leaf(majority class)
    best = attribute test with highest gain
    partition records by best's outcomes
    for each partition:
        child = TreeGrow(partition, attributes minus best)
    return node(best, children)

Growing a tree until every leaf is pure can memorise the training data, noise included. This is overfitting. The usual remedy is pruning: trimming branches that add little predictive value, or stopping growth early.

Rule-Based Classifiers: Knowledge as IF–THEN Statements

A decision tree can be rewritten as rules, one per path from root to leaf:

  • IF Blood = Warm AND Gives birth = Yes THEN Mammal
  • IF Blood = Cold THEN Non-mammal
  • IF Gives birth = No AND Can fly = Yes THEN Bird

Two measures describe a rule:

  • Coverage: the fraction of records whose attributes match the rule's condition.
  • Accuracy: among covered records, the fraction that actually have the rule's class.

Rules extracted from a tree never overlap. Rules learned directly, for example by RIPPER, may overlap or leave gaps, so they are ordered or resolved by voting.

Nearest Neighbour: Judging by Similarity

The k-nearest neighbour classifier stores all training records. To classify a new record, it finds the k closest training records and takes a majority vote among their labels.

This makes it a lazy learner: training just stores the data, and all the work happens at prediction time.

  • Distance: Euclidean distance, d = √Σ(xᵢ − yᵢ)², is the usual default.
  • Choice of k: a small k follows the data closely and is sensitive to noise. A large k smooths the boundary but can blur classes together. An odd k avoids ties in two-class problems.
  • Normalisation: income in lakhs will swamp age in years unless both are rescaled.
  • Weighted voting: closer neighbours can count for more.

Bayesian Classifiers: Reasoning with Probability

The Bayesian approach asks how probable each class is, given the attributes observed. It uses Bayes' theorem:

P(C | X) = P(X | C) · P(C) / P(X)

P(C) is the prior, how common the class is. P(X | C) is the likelihood, how likely these attribute values are under class C. P(X) is the same for every class, so we compare classes using the numerator alone.

Naive Bayes

Estimating P(X | C) directly is hard with many attributes. Naive Bayes assumes attributes are conditionally independent given the class, so the likelihood factorises:

P(X | C) = Π P(xᵢ | C)

The prediction chooses the class that maximises P(C) · Π P(xᵢ | C). The assumption is often unrealistic, yet naive Bayes performs well in practice, especially for text such as spam filtering.

A worked example

A play-tennis dataset has 14 records: 9 Yes and 5 No. Classify a new day: Outlook = Sunny, Temperature = Cool, Humidity = High, Windy = True.

Attribute valueP(value | Yes)P(value | No)
Sunny2/93/5
Cool3/91/5
High humidity3/94/5
Windy3/93/5
  • Yes: 9/14 × (2/9 × 3/9 × 3/9 × 3/9) ≈ 0.0053
  • No: 5/14 × (3/5 × 1/5 × 4/5 × 3/5) ≈ 0.0206

The score for No is about four times larger, so the prediction is No. P(X) never had to be computed, since it cancels out.

The zero-probability problem

If a combination never appears in the training data, its probability is zero, and that zero makes the whole product zero. Laplace smoothing adds a small count to every possibility, so no probability is exactly zero.

Comparing the Approaches

MethodStrengthsWeaknesses
Decision treesInterpretable; handle mixed attribute typesCan overfit; sensitive to small data changes
Rule-basedTransparent; easy to editConflicting rules need careful handling
Nearest neighbourNo training; adapts to complex boundariesSlow on large data; depends on scaling and k
Naive BayesFast; needs little data; good for textIndependence assumption often violated

No single method wins everywhere. The right choice depends on data size, whether explanations are needed, and how the attributes behave.

Closing Thoughts

Classification rests on one goal: learn from labelled examples so that unseen examples can be sorted correctly. The methods differ in how they represent that knowledge, as questions, rules, neighbours, or probabilities. Understanding the mechanics of each makes it far easier to choose the right tool and explain its decisions to others.

No comments:

Post a Comment

Desision Tree

Inside Classification: How Machines Learn to Sort the World Inside Classification: How Machines Learn to Sort the World Every d...