Machine Learning · Chapter 11 of 26

Linear regression, and how to score it

By the end of this chapter you can fit a model that predicts a number, read what each of its coefficients says in rupees, compute a prediction by hand from what it learned, and score a price model honestly: its errors in rupees, R², and the floor any model must beat. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Regression
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Machine Learning are free. This one comes with the all labs plan.

Why you need this

Every bike Dobara buys has to be priced twice: once when Raju offers the seller a price, and again when the showroom puts a tag on it. Pay the seller ₹10,000 too much and the margin on a commuter bike is gone. Raju prices from experience, and he is good, but he cannot be in six branches at once, and Rachna wants to know how far off a price usually is.

Dobara has sold 1,600 used bikes, each with the price it really fetched. Everything you have learned so far predicts a class: defaulted or not. A classifier's answer is 0 or 1, and its score is the share it got right. A price is not right or wrong: ₹54,000 for a bike that sold for ₹55,000 is nearly right, ₹20,000 is badly wrong, and accuracy cannot tell the two apart. You need a model that answers with a number, and a way to say how far off its numbers are.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    price_line.py: a price line on age alone

    Write price_line(bikes) in price_line.py. bikes is a DataFrame with the columns of bikes.csv, as pd.read_csv reads it; do not change it. X is the one column age_years, as a table (a DataFrame of one column, not a Series), and y is sold_price_rs. Split the rows with train_test_split, test_size=0.25 and random_state=3, and fit a LinearRegression on the training rows only.

    Return three values separated by commas: the fitted LinearRegression; the rupees one more year of age adds to the price, as one float (negative when age lowers the price), not an array; and the price the line gives a bike of age 0, as one float.

    The Check fits a fresh copy of your model on the same training rows and compares the two, so the model must never see the test rows. It tries all 1,600 bikes, the first 800, and 60 made-up bikes.

  2. Task 2

    errors.py: three numbers for a price model

    Write test_errors(model, X_test, y_test) in errors.py. model is any fitted scikit-learn regressor, X_test the rows it never saw and y_test their real prices. Predict the test rows, then measure the predictions against y_test.

    Return a dict with exactly three keys: "mae", the mean absolute error in rupees; "rmse", the root mean squared error in rupees (the square root of the mean squared error, not the mean squared error itself); and "r2", R² (1 is perfect, 0 is no better than the mean, and on test rows it can be below 0).

    The Check tries a linear regression on three bike columns, a depth 4 tree, and a line fitted on electric scooters only that does worse than guessing the mean price.

  3. Task 3

    floor.py: compared to what?

    Write floor_and_lead(X, y) in floor.py. X is a DataFrame of numeric columns of bikes and y their prices. Split the rows with train_test_split, test_size=0.25 and random_state=5. Fit a DummyRegressor with its default setting (it predicts the mean price of the rows it was fitted on) and a LinearRegression, both on the training rows only, and measure each one's mean absolute error on the test rows.

    Return three floats in rupees, separated by commas: the DummyRegressor's test MAE, the LinearRegression's test MAE, and the lead, the first minus the second.

    The floor's mean must come from the training rows, not the test rows. The Check tries three numeric columns of the bikes (age_years, km_driven, owners), km_driven alone, and 80 made-up bikes.

  4. Task 4 · interview question

    Interview: r2_report.py, a negative R²

    Raju fitted a price line on cruisers only, used it on every test bike, and r2_score said -4.5. His r2_report(y_true, y_pred) never shows an R² below 0, even for a model that prices bikes worse than guessing the average, and on such a model its numbers do not agree with r2_score. y_true holds real prices and y_pred a model's predictions for the same bikes, as arrays or Series of the same length. Fix it.

    Return three floats separated by commas: the model's squared error, the sum over every bike of (real price minus predicted) squared; the mean's squared error, the sum over every bike of (real price minus the mean of the real prices) squared, what guessing the average price for every bike would give; and R², 1 minus the first divided by the second, which must agree with r2_score for every model, below 0 too.

    The Check tries a good line's test predictions, a line fitted on cruisers only, and 4 hand-made prices.

    Tanvi will ask: what does an R² of -4.5 on the test rows mean, and is it a bug?

What you will learn

  • LinearRegression: a price as a weighted sum of the features, with the weights learned from the examples.
  • coef_ and intercept_: what fit learned, read as sentences in rupees, and a prediction computed by hand.
  • Residuals: actual minus predicted, one a bike.
  • mean_absolute_error and root_mean_squared_error: the typical miss in rupees, and why the second is larger.
  • r2_score: the share of the variation in prices the model explains, and why it can be negative.
  • DummyRegressor: the mean price, the floor every price model must beat.
  • mean_absolute_percentage_error: the error as a share, and its trap.

More in Regression

Build "Linear regression, and how to score it" yourself

Dobara refurbishes used bikes in Indore and lends to its buyers. You build the models that decide whom to lend to and at what price to sell. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Machine Learning are free. This one comes with the all labs plan.