Ordinary least squares fits a straight line by minimizing the overall squared differences between observed and predicted values. The line passes through the intersection of the mean feature value and mean target value. section 8.11
Build the regression line in a few practical steps:
- Calculate the means of x and y. section 8.11
- Measure each x and y value’s distance from its mean. section 8.11
- Square the x deviations and multiply paired x and y deviations. section 8.11
- Divide the cross-product sum by the squared-x sum to find the slope, then calculate the intercept. section 8.11
Predicting diamond prices
For carat and price data, the means are 1,229 and $3,431. After calculating deviations and sums, the slope is about 3.25. Use y = c + mx to draw the fitted line and predict prices for selected carat values. section 8.11
Figure 8.10 Intuition of regression lines
Use the subway-route analogy to see how one straight line can minimize the total distance to many scattered points. section 8.11



