Start learning free

LESSON 43 OF 165 · MACHINE LEARNING: MODELS

How Wrong, on Average?

Practice

A guessed amount is almost never exactly right, so you can't count right and wrong answers the way you did for classes. Instead you measure how far off the guesses are on the test rows, in dollars.

Three functions from sklearn.metrics take the real answers first, then the guesses. mean_absolute_error gives back the average miss (the MAE), a miss up or down counted the same. mean_squared_error gives back the average of the squared misses, in squared dollars; its square root, ** 0.5, is the RMSE, back in dollars, where big misses weigh more. r2_score gives back 1 for perfect guesses and 0 for guesses no better than always saying the mean.

from sklearn.metrics import mean_absolute_error, mean_squared_error

real = [10, 20, 30, 40]
guess = [10, 20, 30, 60]
print(mean_absolute_error(real, guess))         # 5.0
print(mean_squared_error(real, guess) ** 0.5)   # 10.0: the one big miss counts more

A miss of 88 dollars means little on its own. Compare it with a lazy baseline: guess the training rows' average spent for every test customer. A list times a number repeats it: [5] * 3 is [5, 5, 5], and len(y_test) is the number of test rows.

The starting code fits the model from the last lessons and keeps its guesses for all 75 test rows in predictions. Your finished program prints these lines; the last one comes from a check on mean_guess:

Model MAE: 87.66
Model RMSE: 109.73
Model R2: 0.396
Mean guess MAE: 108.21
Mean guess RMSE: 141.2
Each mean guess: 189.63 for 75 test rows

Your task: print the model's MAE and RMSE rounded to 2 decimals and its R2 rounded to 3. Then make mean_guess, the mean of y_train repeated once per test row, and print its MAE and RMSE the same way.


Assignment
Follow the Coding Tutorial to measure how far off the model is and compare it with always guessing the mean.


Hint
Pass y_test first, then the guesses: predictions for the model, mean_guess for the baseline. y_train.mean() gives back the average of the training answers.


STEP 1 OF 6
    Ctrl + Enter to check