Section 3.1: What a Model Is, and What Fitting Means
A model takes a listing's features and returns a number, and the numbers inside it, its parameters, decide which number. Fitting chooses those parameters from listings whose price is already known, by making a loss, one number that scores the predictions against the known prices, as small as it can. Every lesson in this section fits scikit-learn's LinearRegression, the least squares fit Lesson 2.3.4 computed with NumPy. The section covers installing scikit-learn and the interface its models share, holding listings back from the fit, the loss a fit minimizes, and the metrics and residuals that say how far off the predictions are.
Lesson 2.3.4 left a warning in its Assignment: a smaller sum on the listings a model was fitted to says nothing yet about new listings. This section turns that warning into a rule, that a score says how a model does on new listings only when it comes from rows the fit never saw. The quiet failures start here too, a fill computed before the split that has already seen the test rows, a squared error read as if it were money, an R² compared across two different sets of rows, and none of them raises an error.
Lessons
- Installing scikit-learn, and What a Model Actually Is - scikit-learn installed, a model as a function with learned parameters, scikit-learn's
fitandpredict,LinearRegressionshown to be 2.3.4's least squares, and predictions for listings the fit never saw. - The Split, and What a Held-Out Set Is For - why a training score lies,
train_test_split, what a held-out row must be, a split stratified on price bands so each band keeps its share on both sides, and one split as one draw. - Loss Functions - What You Are Actually Minimizing - what a loss is, squared error and why the mean wins it, absolute error and why the median wins it, two losses fitting two different lines to the same listings, and the loss you minimize against the number you report.
- Regression Metrics and Reading a Residual - residuals, MAE, MSE and RMSE (mean absolute error, mean squared error and its square root), R² against a baseline, residual plots, and choosing the metric that matches the question.
By the end you will be able to:
- Install scikit-learn into
(bootcamp), fitLinearRegressionon a listing's area, show that its slope and intercept are the least squares fitnp.linalg.lstsqcomputes, and predict for listings it never saw. - Split listings into training and test rows reproducibly, stratify the split on price bands, and state what a held-out row must be.
- Find the constant each loss prefers by scanning, and say which loss
LinearRegressionminimizes and why. - Score predictions with MAE, RMSE and R² against a baseline, and read a residual plot for the failure the headline number hides.