liveBook · Manning

Linear regression

As explained in Grokking AI Algorithms, Second Edition

Linear regression is a statistical modeling method that represents a continuous response as a linear function of one or more input variables.

Linear regression fits a line to examples so you can predict a continuous number. Imagine knowing the weights and prices of several diamonds, then estimating the price of another diamond from its weight. Here, carat is the input, or independent variable; price is the output, or dependent variable. section 8.11

Start with known carat–price pairs and plot them as points. The points need not fall on one line: the aim is to find a line that captures their overall trend. A line has the form y = b0 + b1 × x. Its slope, b1, describes how the prediction changes as carat changes; its intercept, b0, positions the line. Once those values are fitted, substitute a new carat value for x to predict price. section 8.11

A small learning implementation follows this sequence:

  • Calculate the mean carat value and mean price. section 8.11
  • Measure how each carat value and price differs from its mean. Use those differences to calculate the line’s slope, then use the means and slope to calculate its intercept. section 8.11
  • Use the fitted line to make predictions for carat values it has not seen. section 8.11
def fit_regression_line(carats, prices):
   n = len(carats)

   mean_X = sum(carats) / n #A
   mean_Y = sum(prices) / n #A

   sum_x_squared = 0
   for x_i in carats:
       ans = (x_i - mean_X) ** 2
       sum_x_squared += ans #B

   sum_multiple = 0
   for i in range(n):
       ans = (carats[i] - mean_X) * (prices[i] - mean_Y)
       sum_multiple += ans #C

   b1 = sum_multiple / sum_x_squared #D

   b0 = mean_Y - (b1 * mean_X) #E

   min_x = min(carats)
   max_x = max(carats)

   y1 = b0 + b1 * min_x #F
   y2 = b0 + b1 * max_x #F

   return b0, b1, (min_x, y1), (max_x, y2)

This Python function makes the fitting steps visible: it computes means, the slope and intercept, then returns two points that can be used to draw the line. section 8.11

Check a diamond-price prediction

In one test example, a carat value scaled to 1,080 has an actual price of 2,869; the fitted line predicts 2,963. Comparing predictions with known prices in separate test data reveals errors that training data alone cannot show. An 80/20 training–test split is a common starting point, and R² is introduced as a way to measure the line’s performance. section 8.12

Choose regression when the answer is a continuous value, such as a price. If the answer is a category—such as whether a vehicle is a car or a truck—the task is classification instead. Algorithm choice also depends on the available features and their relationships. section 8.11 section 8.15

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Works through plotting, fitting a line from means and differences, Python implementation, and testing predictions with R².
Examples
Uses carat–price pairs, scatter plots, a Python fitting function, and held-out diamond prices.
What it assumes
Introduces independent and dependent variables in plain language while using means, graphs, and basic Python operations in the worked implementation.
What you can do after
Build it from scratch in Python to fit a line, predict prices, and check predictions against unseen examples.
How it connects
Connects supervised learning and regression to training–test splits, continuous prediction, and the contrast with classification.

Also explained in

Compare how 22 books explain linear regression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.