Decision boundaries explained

Classification is the most common task in machine learning: given examples labelled spam or not spam, tumour or healthy tissue, cat or dog, learn a rule that labels new cases. With two measurements per example, every case is a point in the plane and the rule colours the whole plane, and the line where its colours change, the decision boundary, shows at a glance how a method thinks. This page explains how six classic classifiers draw their boundaries, why they differ, and how to tell a rule that has learned from one that has memorised.

Six panels of the same two moons of blue and red points, each coloured by the class a different classifier predicts, with a dark boundary line: smooth for logistic regression and naive Bayes, ragged for k-nearest neighbours, stepped for the decision tree, winding for the support vector machine and the neural network
Six panels of the same two moons of blue and red points, each coloured by the class a different classifier predicts, with a dark boundary line: smooth for logistic regression and naive Bayes, ragged for k-nearest neighbours, stepped for the decision tree, winding for the support vector machine and the neural network

Classifiers, regions and boundaries

A classifier is a function from inputs to classes. Most also give a score or a probability for each class, and predict the class with the highest one. If the input is two numbers (x, y), the classifier assigns a class to every point of the plane, splitting it into decision regions; the decision boundary is the set of points where the top two classes score equally. Its shape is the classifier's inductive bias made visible: the assumptions it brings to the data. A linear method can only draw straight boundaries, a tree only staircases of horizontal and vertical lines, a nearest-neighbour rule follows the data point by point. Real problems have many more inputs, and the boundary becomes a surface in many dimensions, but the same ideas hold.

Logistic regression: a straight line, or a curve with features

Logistic regression computes a weighted sum z = w₀ + w₁x + w₂y and turns it into a probability with the logistic function σ(z) = 1 / (1 + e⁻ᶻ); for several classes, the softmax function does the same with one sum per class. The boundary, where the probabilities are equal, is where the sums are equal: a straight line. The weights are learned by minimising the cross-entropy, the average of −log p of the true class, by gradient descent; the app uses Adam, one pass over all the training points per epoch, and a small L2 penalty. A straight line cannot separate circles or XOR, but adding new inputs such as x², xy and y² lets the same method draw a circle, an ellipse or a hyperbola: the model is still linear in its features, but the features are not linear in x and y. Higher polynomial degrees bend the boundary more, and can overfit.

k-nearest neighbours: ask the neighbours

The k-nearest neighbours rule, analysed by Thomas Cover and Peter Hart in 1967, does no training at all: it remembers every point, and to classify a new one it finds the k training points closest to it and takes a majority vote. With k = 1 each training point owns the region of places nearer to it than to any other, its Voronoi cell, so the boundary runs along the cell edges and wraps around every stray point: perfect on the training data and often poor on new data. As k grows the votes average over more points and the boundary smooths out, until with k as large as the data it predicts the majority class everywhere. Cover and Hart showed that with enough data the error of the 1-NN rule is at most twice the best possible. Its weaknesses are speed, since every prediction compares with every training point, and many dimensions, where all points become nearly equally far apart.

Decision trees: questions with yes-or-no answers

A decision tree classifies by a sequence of questions, each comparing one input with a threshold: is x below 0.7? If yes, is y above −1.2? Each leaf predicts the majority class of the training points that reach it. In the plane every question is a vertical or horizontal cut, so the regions are rectangles. The CART algorithm of Leo Breiman and colleagues (1984) grows the tree greedily: at each node it tries every threshold on every input and keeps the cut that lowers the Gini impurity, 1 − Σ pᵢ², the most. Greed has a price: on XOR no single first cut improves the impurity, so the first choice is almost arbitrary. Grown deep enough a tree can isolate every training point, which is pure overfitting, so depth is limited or the tree is pruned; random forests and gradient boosting combine many trees to keep their strengths and average out their noise.

Naive Bayes: one bell curve per class

Naive Bayes is a generative classifier: instead of learning the boundary directly, it models how each class produces its points, and then uses Bayes' theorem, P(class | x) ∝ P(x | class) · P(class), to find the most probable class for a point. The naive part is the assumption that the inputs are independent within a class, so P(x, y | class) = P(x | class) · P(y | class). Gaussian naive Bayes models each of these as a normal distribution with the class's mean and variance along that axis, drawn in the app as ellipses at one and two standard deviations, always aligned with the axes. Where two classes have different spreads the boundary is a curve (a conic section); with equal spreads it is a straight line. The method needs only means, variances and counts, so it learns from little data and very fast, which made it the classic spam filter, but it cannot follow shapes such as moons or spirals.

Support vector machines: the widest margin

Many straight lines may separate two classes; a support vector machine, developed by Vladimir Vapnik and colleagues, picks the one with the widest margin, the empty band between the classes. The boundary then depends only on the points on the edge of the band, the support vectors. With overlapping classes a soft margin allows points inside the band or on the wrong side, at a cost set by C: large C fits the training points tightly, small C accepts errors for a wider, smoother margin. The kernel trick makes the method nonlinear: the optimisation needs only the similarities K(x, x′) between points, and replacing the dot product with the RBF kernel exp(−γ|x − x′|²) works as if the points had been mapped into an infinite-dimensional space, where a flat boundary becomes a curved one in the plane. The app solves the dual problem with sequential minimal optimisation (John Platt, 1998), which repeatedly picks the pair of points that most violates the optimality conditions and solves for those two exactly, the method of LIBSVM.

Neural networks: learning the features

A neural network chains layers of simple units, each taking a weighted sum of its inputs and passing it through a nonlinear function such as tanh. The last layer is a logistic regression, but on features the hidden layers have learned, so the network finds for itself the transformations that the polynomial features of logistic regression had to be given. With enough hidden units it can approximate any boundary (the universal approximation theorem), and it is trained like logistic regression, by gradient descent on the cross-entropy with the gradients from backpropagation. A single neuron, the perceptron, can only draw a straight line; one hidden layer already solves XOR, and two can wind around a spiral. The app's network has two hidden layers of tanh units; the Neural Network Sandbox lets you build and train networks of any shape, and the Perceptron Visualizer shows the single neuron learning step by step.

Training, testing and overfitting

Accuracy on the training points measures how well a classifier has fitted them, not how well it will do on new data. So part of the data, the test set, is held back and used only to evaluate it; with little data, cross-validation rotates which part is held back. A classifier with high training accuracy and much lower test accuracy has overfitted: it has learned the noise of its training sample, as k-NN with k = 1, a very deep tree, a high polynomial degree, or an SVM with very large γ or C will. One that does poorly on both has underfitted, being too rigid for the shape of the data. This is the bias–variance trade-off: rigid models are biased, flexible ones vary with every sample. Every classifier has settings that move it along this scale, and the best choice is the one with the best test accuracy, not the best training accuracy.

Comparing classifiers

The picture of several classifiers on the same small datasets, made famous by the classifier comparison in the scikit-learn documentation, teaches a humbling lesson: each method shines on some shapes and fails on others. Linear methods are unbeatable when the classes really are separated by a line and useless on circles; trees handle XOR but draw jagged boundaries on diagonal ones; flexible methods follow anything, including the noise. The no free lunch theorems of David Wolpert make this precise: averaged over all possible problems, no learning method is better than any other. What makes one better in practice is how well its assumptions match the data at hand, which is why practitioners try several, tune each on validation data, and look at the results.

Поширені запитання

Is the decision boundary always a line?

Only for linear classifiers such as plain logistic regression, a linear SVM or a perceptron. Polynomial features, kernels, trees, nearest neighbours and neural networks all draw curved or piecewise boundaries, and the boundary can split a class into several separate islands.

What is the difference between a generative and a discriminative classifier?

A generative classifier, such as naive Bayes, models how each class produces its data and uses Bayes' theorem to classify; it could also generate new examples. A discriminative classifier, such as logistic regression, an SVM or a neural network, models only the boundary or the probability of the class given the input. Discriminative models are often more accurate with plenty of data; generative ones learn faster from little.

Why do test points matter more than training points?

Because the purpose of a classifier is to label new data. Any method flexible enough can be perfect on its training points by memorising them, so training accuracy can be misleading. The test points, never used in training, estimate how it will do on data it has not seen.

How is this related to the perceptron and neural networks?

The perceptron is the simplest classifier with a straight boundary, learned point by point; logistic regression is its smooth, probabilistic cousin. Stacking such units in layers gives a neural network, whose boundary can take any shape. The Perceptron Visualizer and the Neural Network Sandbox explore those two in depth; this app compares them with other families of classifiers.

Готові спробувати?

Відкрийте PictorX у браузері — без встановлення і без кредитної картки.

Open the decision boundaries app