liveBook · Manning

Square trick

As explained in Grokking Machine Learning

A book-specific line-adjustment technique that updates a model's slope and intercept using the signed prediction error and a feature value.

The square trick nudges a line toward one data point at a time. It uses the signed prediction error to update both the slope and the y-intercept. section 3.3

For a point with feature value x and target y:

  • Calculate the prediction: predicted = intercept + slope × x. section 3.3
  • Compute the signed error: y − predicted. section 3.3
  • Update the intercept by learning rate × error. section 3.3
  • Update the slope by learning rate × x × error. section 3.3

Moving a house-price line

For a house-price model, the slope represents price per room and the intercept represents the base price. If the actual price is above the prediction, the positive error moves the line upward; the feature value also determines how much the line rotates. Repeating this with randomly selected points produces a fitted linear regression line. section 3.3

Figure 3.6 An illustration of the linear regression algorithm. We start at the top left with a random line and end in the bottom left with a line that fits the dataset well. At each stage, two things happen: (1) we pick a random point, and (2) the point asks the line to move closer to it. After many iterations, the line will be in a good position. This figure has only three iterations for illustrative purposes, but in real life, many more iterations are needed.

Visualizes repeated random-point updates from an initial random line toward a line that fits the dataset. section 3.3

How Grokking Machine Learning covers it

How deep it goes
Defines signed-error updates for slope and intercept, then stops short of deriving them through gradient descent.
Examples
Uses house-price points, line rotations and translations, Python pseudocode, and plots across training epochs.
What it assumes
Assumes basic line equations and teaches slope, y-intercept, prediction, and signed error before applying updates.
What you can do after
Apply it via a small Python function that updates a linear model from individual labeled examples.
How it connects
Connects linear regression with slope, y-intercept, prediction error, learning rate, and gradient descent.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Machine Learning. The section links point to the parts of the book it draws on.