Three.js Decision Tree Classifier (CART Visualization)

Grow a CART decision tree in Three.js on rings, blobs or checkerboard data, watching the axis-aligned decision regions refine as depth increases next to a live diagram of the learned splits, leaf classes and training accuracy.

Key topics: decision tree visualization, cart algorithm, gini impurity, machine learning three.js, classification, overfitting

What this code does

- CART: at each node every threshold on every axis is tried, keeping the split with the lowest weighted Gini impurity.
- Gini Impurity: 1 − Σ pₖ², which is 0 for a pure node and maximal when the classes are evenly mixed.
- Axis-Aligned Regions: every leaf is a rectangle, so curved boundaries such as rings need many splits (a staircase).
- Overfitting: deep trees with small leaves reach 100% training accuracy by memorising noise; min samples per leaf regularises.
- Tree Diagram: grey nodes show the split rule; coloured leaves show the predicted class and sample count.
- Controls: dataset, max depth, min samples per leaf, depth animation and resample.

Continue Learning

JavaScript Implementation

ES6+
// CART split search: try every threshold on every axis, keep the lowest weighted Gini impurity
const gini = (counts, n) => 1 - counts.reduce((s, k) => s + (k / n) ** 2, 0)

function bestSplit (points) {
  let best = null
  for (const axis of ['x', 'y']) {
    const sorted = [...points].sort((a, b) => a[axis] - b[axis])
    const left = [0, 0, 0]
    const right = countClasses(sorted)
    sorted.forEach((p, i) => {
      left[p.c]++; right[p.c]--
      const nl = i + 1, nr = sorted.length - nl
      const score = (nl * gini(left, nl) + nr * gini(right, nr)) / sorted.length
      if (nr && (!best || score < best.score)) best = { axis, threshold: p[axis], score }
    })
  }
  return best // recurse on both halves until max depth or a pure node
}