CodeBlanks Logo Icon CodeBlanks

Recall

Recall is the ratio of captured true positives.

Recall

Recall measures how many of the true positives were caught: TP / (TP + FN). It matters where missing is costly: disease screening, fraud detection.

from sklearn.metrics import recall_score
y_true = [1, 1, 1, 0, 0]
y_pred = [1, 0, 0, 0, 0]
print(recall_score(y_true, y_pred))   # 1 / 3 = 0.333

A model that calls everything positive gets a recall of 1.0, but its precision drops a lot.

The precision-recall trade-off

When the threshold drops the model says positive more often: recall rises, precision falls. When it rises, the opposite happens:

p = m.predict_proba(X_test)[:, 1]
for esik in [0.3, 0.5, 0.7]:
    t = (p >= esik).astype(int)
    print(esik, precision_score(y_test, t), recall_score(y_test, t))

Which point to choose depends on the business problem; precision_recall_curve gives all thresholds at once.

Practice Threshold effect

For the probability array p, produce decisions at the 0.3 and 0.7 thresholds. For each threshold print the threshold, the precision and the recall with round(..., 2) on the same line.

Starter code

model.py
import numpy as np
from sklearn.metrics import precision_score, recall_score

y_true = np.array([1, 1, 1, 1, 0, 0, 0, 0])
p = np.array([0.9, 0.8, 0.45, 0.35, 0.6, 0.4, 0.2, 0.1])

# buraya yaz

Sample solution

model.py
import numpy as np
from sklearn.metrics import precision_score, recall_score

y_true = np.array([1, 1, 1, 1, 0, 0, 0, 0])
p = np.array([0.9, 0.8, 0.45, 0.35, 0.6, 0.4, 0.2, 0.1])

for esik in [0.3, 0.7]:
    t = (p >= esik).astype(int)
    print(esik, round(precision_score(y_true, t), 2), round(recall_score(y_true, t), 2))

Expected output

0.3 0.67 1.0
0.7 1.0 0.5

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 In cancer screening, which metric should be kept high first?

  • Precision
  • Accuracy
  • None of them
  • Recall Correct answer

Explanation: Missing a patient (FN) is much costlier than sending a healthy person for an extra test.

2 Compute recall.

Question
r = ____(y_true, y_pred)
Answer
r = recall_score(y_true, y_pred)

Explanation: recall_score gives TP / (TP + FN).

3 Match the threshold change with the results.

Term Meaning
Threshold drops Recall rises
Threshold rises Precision rises
Positive for everything Recall 1.0
precision_recall_curve All thresholds

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation