Bird
Raised Fist0

Precision vs Recall - When to Prioritize Each

CODINGbeginnerInterview freq: very_high
Choose your preparation mode6 modes available
⚖️
Precision vs Recall

In a medical diagnosis system, would you prioritize precision or recall? Justify your choice.

📊
Comparison
DimensionPrecisionRecall
False Positive CostLowHigh
False Negative CostHighLow
Class Imbalance HandlingModerateHigh
Confidence in Positive PredictionsHighModerate
Criticality of Positive ClassModerateHigh
✅
Verdict
Default to recall in scenarios where missing a positive case is costly, such as medical diagnosis. Switch to precision when false positives are more costly, such as in spam detection systems.
💡
Key Insight
The choice between precision and recall hinges on the relative cost of false positives versus false negatives.
⚠️
Interview Trap
❌ Wrong: Always prioritize precision because it ensures high confidence.
Ignoring the cost of false negatives can lead to missing critical positive cases.
✅ Consider the cost of false positives vs false negatives and the criticality of capturing all positive cases.
Precision
"Minimizes false positives, ensuring high confidence in positive predictions."
  • May miss true positives, leading to lower recall.
  • Not suitable when false negatives are costly.
  • Can be misleading if class imbalance is high.
  • Requires a large number of true positives to be meaningful.
Ideal When
  • When false positives are costly (e.g., spam detection).
  • When the positive class is rare and needs high confidence.
  • When the cost of acting on a false positive is high.
  • When the dataset is balanced or has a manageable class imbalance.
Email spam filters prioritize precision to avoid marking legitimate emails as spam.
Recall
"Captures all true positives, minimizing false negatives."
  • May increase false positives, reducing precision.
  • Not suitable when false positives are costly.
  • Can lead to overfitting if not balanced with precision.
  • Requires careful threshold tuning to maintain balance.
Ideal When
  • When missing a positive case is costly (e.g., cancer detection).
  • When the positive class is critical to capture.
  • When the dataset has a high class imbalance favoring the negative class.
  • When the system can tolerate false positives.
Medical diagnosis systems prioritize recall to ensure no potential cases are missed.
🐍
Precision - Example
▼ Show
Python
def calculate_precision(tp, fp):
    if tp + fp == 0:
        return 0.0
    return tp / (tp + fp)

# Example usage
precision = calculate_precision(70, 30)
print(f'Precision: {precision}')
🐍
Recall - Example
▼ Show
Python
def calculate_recall(tp, fn):
    if tp + fn == 0:
        return 0.0
    return tp / (tp + fn)

# Example usage
recall = calculate_recall(70, 10)
print(f'Recall: {recall}')
✅
Use Precision When
  • •When false positives are more costly than false negatives.
  • •When the positive class is rare and high confidence is needed.
  • •When the dataset is balanced or has a manageable class imbalance.
✅
Use Recall When
  • •When false negatives are more costly than false positives.
  • •When capturing all positive cases is critical.
  • •When the dataset has a high class imbalance favoring the negative class.
↳
Nuances
  • In highly imbalanced datasets, precision can be misleading if not paired with recall.
  • In scenarios where both false positives and false negatives are costly, F1 score might be a better metric.
  • Precision is not always the best choice even if false positives are costly, if the positive class is critical.

Practice

(1/5)
1. What does the following Python code output when implementing logistic regression with a sigmoid activation function?
import numpy as np

def sigmoid(z):
    return 1 / (1 + np.exp(-z))

z = np.array([0, 2, -2])
output = sigmoid(z)
print(output)
easy
A. [0.5, 0.88, 0.12]
B. [0.5, 0.88, 0.11]
C. [0.5, 0.87, 0.12]
D. [0.5, 0.86, 0.12]

Solution

  1. Step 1: Calculate sigmoid for each element

    Sigmoid function is applied element-wise: sigmoid(0) = 0.5, sigmoid(2) ≈ 0.88, sigmoid(-2) ≈ 0.12.
  2. Step 2: Check precision of calculations

    Common mistake is rounding errors. Correct values are [0.5, 0.880797, 0.119203], rounded to [0.5, 0.88, 0.12].
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Sigmoid(0)=0.5, Sigmoid(2)≈0.88, Sigmoid(-2)≈0.12 [OK]
Hint: Sigmoid(0)=0.5, Sigmoid(2)≈0.88, Sigmoid(-2)≈0.12 [OK]
Common Mistakes:
2. What is the time complexity of updating the embeddings for a single word pair in the Skip-Gram model with negative sampling?
easy
A. O(k)
B. O(vocab_size)
C. O(n)
D. O(1)

Solution

  1. Step 1: Identify the operations involved

    Updating embeddings involves computing gradients and updating weights for the target word and k negative samples.
  2. Step 2: Determine the complexity

    Each update involves a constant number of operations per sample, leading to O(k) complexity where k is the number of negative samples.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Complexity is O(k) due to k negative samples [OK]
Hint: Complexity is O(k) due to k negative samples [OK]
Common Mistakes:
3. You implemented linear regression using gradient descent, but the model is not converging. What is the most likely cause?
medium
A. The learning rate is too low.
B. The learning rate is too high.
C. The dataset is too small.
D. The model has too many features.

Solution

  1. Step 1: Identify symptoms of non-convergence

    Non-convergence often results from oscillations due to a high learning rate.
  2. Step 2: Eliminate other options

    Low learning rate causes slow convergence, not non-convergence. Dataset size and feature count affect overfitting, not convergence.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    High learning rate causes oscillations. [OK]
Hint: High learning rate causes oscillations. [OK]
Common Mistakes:
4. How can you optimize a naive 2D convolution implementation for large images and kernels?
hard
A. Use a larger stride to reduce computation.
B. Use a smaller kernel to decrease computation.
C. Implement convolution using FFT (Fast Fourier Transform).
D. Increase the kernel size to cover more area.

Solution

  1. Step 1: Identify optimization techniques

    Using FFT can significantly reduce the computational complexity of convolution from O(N^2 * K^2) to O(N^2 log N) for large images and kernels.
  2. Step 2: Eliminate wrong options

    Use a larger stride to reduce computation. reduces output size, not computation. Increase the kernel size to cover more area. increases computation. Use a smaller kernel to decrease computation. limits kernel effectiveness.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    FFT reduces convolution complexity. [OK]
Hint: FFT reduces convolution complexity. [OK]
Common Mistakes:
5. How can you optimize the dropout layer implementation to reduce computational overhead during training?
hard
A. Use in-place operations to apply the mask.
B. Use a fixed mask for all training iterations.
C. Precompute masks for all layers before training.
D. Reduce the dropout rate to minimize mask application.

Solution

  1. Step 1: Identify Optimization Opportunities

    In-place operations reduce memory usage and computational overhead by modifying data directly.
  2. Step 2: Eliminate Wrong Options

    Use a fixed mask for all training iterations. reduces randomness, harming model performance. Precompute masks for all layers before training. increases memory usage. Reduce the dropout rate to minimize mask application. changes model behavior, not efficiency.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    In-place operations save memory and time. [OK]
Hint: In-place operations save memory and time. [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard