Start learning free

LESSON 63 OF 165 · MACHINE LEARNING: DOING IT RIGHT

When One Answer Is Rare

Practice

This time the answer you predict is rare. y is 1 for a big spender, a customer who spent over 400, and only about 11% of customers are one. When one answer is rare, accuracy fools you: a model that always says "no" is right on almost 9 customers in 10, and it never finds a single big spender.

A baseline to beat: DummyClassifier(strategy="most_frequent"), from sklearn.dummy, is that lazy model on purpose. It learns only the most common answer and gives it back for every row. A real model should beat it at the thing you care about.

Recall: recall_score(y_true, y_pred), from sklearn.metrics, looks only at the rows that really are 1. It gives back the share of them the model also called 1: 1.0 means it found them all, 0.0 means it found none.

from sklearn.metrics import recall_score
real = [1, 0, 0, 1, 0]
said = [1, 0, 0, 0, 0]
print(recall_score(real, said))  # 0.5: it found 1 of the 2 real 1s

Making rare rows count: LogisticRegression(class_weight="balanced") gives each training row a weight by how rare its answer is, so the few 1 rows together count as much as all the 0 rows. The model says yes more often: it finds more big spenders and makes some wrong yes calls, so its accuracy drops a little. In report, p holds the model's 0s and 1s, so p.sum() counts its yes answers.

Your script must print:

Big spenders: 0.113
Always no | accuracy: 0.893
Always no | recall: 0.0 | said yes: 0 of 75
Plain | accuracy: 0.907
Plain | recall: 0.25 | said yes: 3 of 75
Balanced | accuracy: 0.827
Balanced | recall: 0.75 | said yes: 17 of 75

Your task: the starting code reads customers.csv, takes visits, orders and days_since_last as X, sets y to 1 where spent is over 400, splits with stratify=y, and defines report(name, model), which fits a model and prints its test accuracy. Print the share of big spenders, report the always-no baseline and a plain LogisticRegression, add a recall line to report, then report the balanced model. The plain model beats the baseline on accuracy and still misses most big spenders; the balanced one finds 3 in 4.


Assignment
Follow the Coding Tutorial to print the share of big spenders, compare three models on accuracy and recall, and give the rare rows more weight with class_weight="balanced".


Hint
y holds 0s and 1s, so y.mean() is the share of 1s. The recall line goes inside report, below the accuracy line, so every model you report prints it.


STEP 1 OF 5
    Ctrl + Enter to check