Visualized ML
Topics

A · One neuron

Feature Engineering

A linear model can only draw straight lines, but in the right features a curved problem becomes a straight one.

Try it: A6 · Feature Engineering

Intuition

A linear model, whether linear regression, logistic regression or a perceptron, can only draw straight boundaries. Hand it points of one class inside a disc and the other class all around, and it fails, because no straight line encloses a disc.

But "straight" depends on the coordinates. Describe each point not by \((x_1, x_2)\) but by its squared distance from the centre, \(x_1^2 + x_2^2\), and the disc becomes "the points where this number is small". One threshold on one number: a straight cut. The model didn't get smarter; its inputs did. Choosing and computing those inputs is feature engineering.

Lift every point to a third coordinate, \(x_1^2 + x_2^2\). The inner class stays low, the outer class rises, and a flat plane now separates them. Seen from above, that flat cut is a circle.

The math

A feature map \(\phi\) turns an input into a new vector, and the linear model works on that instead:

\[ \hat{y} = \sigma\big(\mathbf{w}^\top \phi(\mathbf{x}) + b\big) \]

The model is still linear in its weights, so everything stays the same: the same loss, the same gradient, the same convexity, the same gradient descent. Only the boundary \(\mathbf{w}^\top\phi(\mathbf{x}) + b = 0\) is now a curve in the original space.

Common feature maps

  • Polynomials. \(\phi(x_1, x_2) = (x_1, x_2, x_1^2, x_1 x_2, x_2^2)\). Degree 2 gives every conic section: circles, ellipses, parabolas, hyperbolas. Degree \(k\) on \(d\) inputs gives \(\binom{d + k}{k} - 1\) features, which grows quickly: degree 3 on 100 inputs is already 176,850.
  • Crosses. A product of two features, \(x_i x_j\), lets the effect of one depend on the other. With \(x_1 x_2\) added, XOR becomes linearly separable.
  • Transformations. \(\log x\) for quantities that span orders of magnitude (incomes, populations), \(\sin\) and \(\cos\) of the angle for cyclic ones (hour of day, so 23:00 is next to 00:00).
  • Encodings. One-hot vectors for categories, buckets for numbers whose effect isn't monotonic.
  • Scaling. Standardising each feature to mean 0 and standard deviation 1 doesn't change what a linear model can represent, but makes gradient descent far faster and makes regularization treat features fairly.

Worked example: XOR. With features \((x_1, x_2, x_1 x_2)\), take weights \((1, 1, -2)\) and bias \(-0.5\). Then \((0, 0) \to -0.5\), \((0, 1) \to 0.5\), \((1, 0) \to 0.5\), \((1, 1) \to 1 + 1 - 2 - 0.5 = -0.5\). Positive exactly for the two XOR-true corners: one product feature made the impossible problem linear.

The kernel trick

Many features are expensive to compute and store. For some models only dot products between feature vectors are ever needed, and some dot products have a shortcut. For degree-2 polynomial features, \(\phi(\mathbf{x})^\top\phi(\mathbf{z}) = (\mathbf{x}^\top\mathbf{z} + 1)^2\) (with suitable constants), one dot product of the original vectors. The Gaussian kernel \(e^{-\lVert\mathbf{x} - \mathbf{z}\rVert^2/2s^2}\) even corresponds to infinitely many features. This is how support vector machines learn curved boundaries cheaply.

From hand-made to learned features

Feature engineering needs someone to know the right features. For a disc, squared radius is obvious; for recognising a face in pixels, nobody can write the features down. A deep network takes the next step: its hidden layers are a feature map, with weights of their own, learned by gradient descent together with the final linear model. Seen this way, a neural network is logistic regression on features it designs itself.

When it fails

  • Too many features. With as many features as examples, a linear model can fit anything, including noise; a degree-12 polynomial threads every point and swings wildly between them. That is overfitting, and regularization or fewer features is the cure.
  • Leakage. A feature that secretly contains the answer (a "cancelled" flag when predicting cancellations) gives brilliant training scores and a useless model.
  • Wrong guesses. Hand-made features encode assumptions. If the true boundary is a square, circle features help only so much.

History

Polynomial and basis-function regression are as old as least squares. The kernel trick goes back to Aizerman, Braverman and Rozonoer (1964) and became central with Boser, Guyon and Vapnik's support vector machines (1992). For decades, hand-designed features such as SIFT for images and MFCCs for speech were where most of the work in applied machine learning went, until deep networks learning their own features took over around 2012.

Connections

Builds on

Leads to

Further reading