Building the Street, Row by Row

Classic MLMedium+20 XP+20

Building the Street, Row by Row

A factory sorts machine parts into pass (y=+1y = +1y=+1) and fail (y=−1y = -1y=−1) from a few measurements, using a linear support vector machine. For a row xxx the model computes a score

s=w⋅x+b=w1x1+w2x2+⋯+bs = w \cdot x + b = w_1 x_1 + w_2 x_2 + \dots + bs=w⋅x+b=w1​x1​+w2​x2​+⋯+b

and predicts +1+1+1 when s≥0s \ge 0s≥0, and −1-1−1 otherwise. The dividing line is s=0s = 0s=0, and the street around it runs from s=−1s = -1s=−1 to s=+1s = +1s=+1.

Where a row sits. Its margin is m=y sm = y\,sm=ys, its score multiplied by its true label:

  • m≥1m \ge 1m≥1: on its own side and outside the street, or exactly on the edge;
  • 0<m<10 < m < 10<m<1: on the correct side, but inside the street;
  • m≤0m \le 0m≤0: on the line, or on the wrong side of it.

What training lowers. For one row, the cost is

max⁡(0, 1−y (w⋅x+b))  +  λ2 ∥w∥2\max\big(0,\ 1 - y\,(w \cdot x + b)\big) \;+\; \frac{\lambda}{2}\,\lVert w \rVert^2max(0, 1−y(w⋅x+b))+2λ​∥w∥2

The first part, the hinge loss, is zero for a row outside the street on its own side, and grows the deeper a row sits inside the street or past the line. The second part grows with the size of the weights. Small weights mean a wide street (its width is 2/∥w∥2 / \lVert w \rVert2/∥w∥), so this part keeps pushing towards the widest street. The number λ\lambdaλ sets how hard it pushes.

One step of stochastic gradient descent, for one row. Work out the row's margin mmm using the current www and bbb. Then:

  • if m<1m < 1m<1 (strictly): every weight becomes wj−lr (λ wj−y xj)w_j - \text{lr}\,(\lambda\, w_j - y\, x_j)wj​−lr(λwj​−yxj​), and bbb becomes b+lr  yb + \text{lr}\; yb+lry;
  • otherwise: every weight becomes wj−lr  λ wjw_j - \text{lr}\;\lambda\, w_jwj​−lrλwj​, and bbb is left alone.

Every step shrinks the weights a little. Only a row inside the street or on the wrong side also pulls the line towards classifying it correctly. The bias is never shrunk.

Task: write train_svm(X, y, lr, lam, epochs, X_new).

  • X is a list of rows of numbers, and y gives each row's label, 1 or -1. lr is the learning rate and lam is λ\lambdaλ.
  • Start with every weight and the bias at 0.0. One epoch is one pass over the rows in the order given, taking one step per row, and each step uses the weights exactly as the previous step left them. Run epochs epochs; epochs may be 0.
  • Then predict each row of X_new with the final weights and bias.

Return a tuple (w, b, predictions): w is the list of weights and b the bias, both rounded to 4 decimal places, and predictions is a list of 1 or -1, one per row of X_new. Predict with the unrounded values.

Examples

X = [[1.0, 2.0], [2.0, -1.0]]
y = [1, -1]
lr = 0.5
lam = 0.1
epochs = 1
X_new = [[1.0, 1.0], [0.0, -2.0]]
returns ([-0.525, 1.45], 0.0, [1, -1])
X = [[1.0, 2.0], [2.0, 0.5], [-1.0, -1.0], [-2.0, 0.0], [3.0, 1.0]]
y = [1, 1, -1, -1, 1]
lr = 0.05
lam = 0.1
epochs = 5
X_new = [[0.5, 0.5], [-0.5, -2.0], [2.0, -1.0]]
returns ([0.5904, 0.3625], -0.05, [1, -1, 1])

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run