Skip to content
    Neuron City

    REGRESSION · 10 MIN

    Linear Regression

    Fit a line y = w·x + b that minimises the mean squared error between predictions and data.

    PIXEL explains

    • Error for one point: (y − ŷ)². MSE averages these.
    • Gradient descent nudges w and b opposite to the gradient of the error.
    • The learning rate controls step size — too big diverges, too small is slow.

    Real world: Estimating a cab fare from distance travelled.

    Watch it move

    epoch 0: w=0.00 b=0.00

    1/9

    Code

    xs, ys = [1, 2, 3, 4], [2, 4, 6, 8]
    w, b, lr = 0.0, 0.0, 0.05
    for _ in range(500):
        dw = sum(2*(w*x+b-y)*x for x, y in zip(xs, ys)) / len(xs)
        db = sum(2*(w*x+b-y) for x, y in zip(xs, ys)) / len(xs)
        w, b = w - lr*dw, b - lr*db
    print(round(w, 2), round(b, 2))
    Output ›
    2.0 0.0
    Try it in the online compiler →

    Checkpoint quiz

    If the loss keeps exploding, you should…