Start learning free

LESSON 37 OF 165 · MACHINE LEARNING: YOUR FIRST MODEL

Precision and Recall

Practice

The confusion matrix shows two kinds of mistakes. Two numbers sum them up, each from its own side. Precision asks: of the customers the model said yes to, what share really came back? Recall asks: of the customers who really came back, what share did the model say yes to?

Both functions are in sklearn.metrics, and both take the real answers first and the model's answers second. precision_score(real, said) gives back a number from 0 to 1: the share of the model's yes answers that were right. recall_score(real, said) gives back the share of the real yes answers that the model found. Here is an example with five customers:

from sklearn.metrics import precision_score, recall_score

real = [0, 0, 0, 1, 1]
said = [0, 1, 1, 1, 0]
print(round(precision_score(real, said), 3))   # 0.333: 1 of 3 yes answers was right
print(recall_score(real, said))                # 0.5: 1 of 2 buyers was found

Which one matters depends on what each mistake costs. Say the shop mails a $3 catalog to every customer the model says yes to. Then you want high precision, so few catalogs are wasted on people who never return. Now say it sends a free email about a sale, and every returning customer it misses is a lost sale. Then you want high recall, so few buyers are missed.

For the shop's test customers, your finished code prints the lines below. The last two lines come from the lesson's check: precision is 35 / 42, and recall is 35 / 49.

[[19  7]
 [14 35]]
Precision: 0.833
Recall: 0.714
Said yes: 42 - of them came back: 35
Came back: 49 - of them found: 35

Your task: the tree, its predictions and the printed confusion matrix are already in the editor. Keep the precision score in precision and print it rounded to 3 digits. Then do the same for the recall score, kept in recall.


Assignment
Follow the Coding Tutorial to work out the tree's precision and recall and print both, rounded to 3 digits.


Hint
Like confusion_matrix, both functions take y_test first and predictions second. Round only inside the print, with round(x, 3), so the variable keeps the full number.


STEP 1 OF 4
    Ctrl + Enter to check