Bird
Raised Fist0

Implement Precision, Recall, F1 from Confusion Matrix

CODINGbeginnerInterview freq: very_high
Choose your preparation mode6 modes available
๐ŸŽฏ
Implement Precision, Recall, and F1 Score from a Confusion Matrix
30 min ยท junior
junior

You are tasked with implementing precision, recall, and F1 score calculations from a confusion matrix for a binary classification problem. The implementation must handle edge cases such as zero true positives or zero predicted positives. The solution should be efficient enough to handle confusion matrices from datasets with up to 1 million samples.

  • The confusion matrix is provided as a 2x2 NumPy array.
  • The matrix format is [[TP, FP], [FN, TN]].
  • The implementation must return precision, recall, and F1 score as floats.
  • Handle division by zero gracefully, returning 0.0 for precision or recall if the denominator is zero.
๐Ÿ“‹
Guided Steps
1/4
Step 1
Understanding the Confusion Matrix
How do you interpret the values in the confusion matrix to calculate precision and recall?
๐Ÿ’ก Hint
Consider what each element of the matrix represents in terms of true and false predictions.
Correctly identify TP, FP, FN, TN from the matrix to calculate metrics accurately.
Mid: Correctly identifies TP, FP, FN, TN. Senior: Also explains implications of misinterpretation on metric calculations.
โš ๏ธCommon Wrong Answer
Misidentifying the matrix elements, leading to incorrect metric calculations.
Step 2
Calculating Precision
How do you calculate precision from the confusion matrix?
๐Ÿ’ก Hint
Precision is about the proportion of positive identifications that were actually correct.
Use the formula TP / (TP + FP) and handle division by zero.
Mid: Implements precision calculation. Senior: Also handles edge cases like zero predicted positives.
โš ๏ธCommon Wrong Answer
Ignoring division by zero, causing runtime errors.
Step 3
Calculating Recall
How do you calculate recall from the confusion matrix?
๐Ÿ’ก Hint
Recall is about the proportion of actual positives that were identified correctly.
Use the formula TP / (TP + FN) and handle division by zero.
Mid: Implements recall calculation. Senior: Also handles edge cases like zero actual positives.
โš ๏ธCommon Wrong Answer
Failing to handle division by zero, leading to incorrect results.
Step 4
Calculating F1 Score
How do you calculate the F1 score using precision and recall?
๐Ÿ’ก Hint
The F1 score is the harmonic mean of precision and recall.
Use the formula 2 * (precision * recall) / (precision + recall) and handle division by zero.
Mid: Implements F1 score calculation. Senior: Also explains why harmonic mean is used.
โš ๏ธCommon Wrong Answer
Using arithmetic mean instead of harmonic mean, leading to incorrect F1 score.
๐Ÿ“Š
Scoring Rubric
โ–ผ Show
Must Have - 60%
  • Correctly identifies TP, FP, FN, TN from the confusion matrix.
  • Implements precision calculation with division by zero handling.
  • Implements recall calculation with division by zero handling.
  • Implements F1 score calculation with division by zero handling.
Good to Have - 30%
  • Explains the significance of each metric in model evaluation.
  • Handles edge cases like zero true positives or zero predicted positives.
  • Provides clear and concise code with comments.
  • Demonstrates understanding of when to use each metric.
Bonus - 10%
  • Discusses the trade-offs between precision and recall in different scenarios.
  • Explains the choice of harmonic mean for F1 score.
  • Considers scalability of the implementation for large datasets.
โœ…
Model Answer
โ–ผ Reveal

The ideal solution correctly implements precision, recall, and F1 score calculations from a confusion matrix, handling edge cases like division by zero. The candidate should demonstrate understanding of the significance of each metric and provide a scalable solution.

  • Correctly identify TP, FP, FN, TN from the confusion matrix.
  • Implement precision as TP / (TP + FP) with division by zero handling.
  • Implement recall as TP / (TP + FN) with division by zero handling.
  • Implement F1 score as 2 * (precision * recall) / (precision + recall) with division by zero handling.
  • Explain the use of harmonic mean for F1 score and its implications.
Misidentifying confusion matrix elements.
Ignoring division by zero, causing runtime errors.
Using arithmetic mean instead of harmonic mean for F1 score.

Practice

(1/5)
1.
import numpy as np

def euclidean_distance(a, b):
    return np.sqrt(np.sum((a - b) ** 2))

vector1 = np.array([1, 2, 3])
vector2 = np.array([4, 5])

result = euclidean_distance(vector1, vector2)
print(result)
medium
A. Returns NaN due to broadcasting
B. Throws a ValueError due to shape mismatch
C. Returns 0.0
D. Returns a large number due to incorrect subtraction

Solution

  1. Step 1: Check vector shapes

    vector1 has shape (3,), vector2 has shape (2,).
  2. Step 2: Identify operation failure

    Subtraction fails due to shape mismatch, raising a ValueError.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Shape mismatch leads to ValueError. [OK]
Hint: Shape mismatch leads to ValueError. [OK]
Common Mistakes:
2. A logistic regression model is not converging during training. Which line in the code is most likely causing the issue?
import numpy as np

# Initialize weights
weights = np.zeros((m, 1))

# Training loop
for epoch in range(epochs):
    # Compute predictions
    predictions = 1 / (1 + np.exp(-np.dot(X, weights)))
    
    # Compute gradient
    gradient = np.dot(X.T, (predictions - y)) / n
    
    # Update weights
    weights -= learning_rate * gradient
medium
A. weights -= learning_rate * gradient
B. predictions = 1 / (1 + np.exp(-np.dot(X, weights)))
C. weights = np.zeros((m, 1))
D. gradient = np.dot(X.T, (predictions - y)) / n

Solution

  1. Step 1: Identify potential issues

    Convergence issues often arise from incorrect weight updates or learning rate problems.
  2. Step 2: Analyze weight update line

    If learning_rate is too high, weights update too aggressively, causing divergence.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Check learning rate in weight updates [OK]
Hint: Check learning rate in weight updates [OK]
Common Mistakes:
3. In a PCA implementation, you need to project data onto the first two principal components. Which approach is correct?
medium
A. Multiply the data matrix by the first two eigenvectors.
B. Multiply the data matrix by the first two eigenvalues.
C. Multiply the covariance matrix by the first two eigenvectors.
D. Multiply the covariance matrix by the first two eigenvalues.

Solution

  1. Step 1: Understand projection in PCA

    Projection involves multiplying the data matrix by the eigenvectors corresponding to the desired components.
  2. Step 2: Eliminate wrong options

    Multiply the data matrix by the first two eigenvalues. is incorrect because eigenvalues are not used for projection. Multiply the covariance matrix by the first two eigenvectors. and D incorrectly use the covariance matrix instead of the data matrix.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Project data using eigenvectors, not eigenvalues. [OK]
Hint: Project data using eigenvectors, not eigenvalues. [OK]
Common Mistakes:
4. How can you optimize the following logistic regression implementation for faster computation?
import numpy as np

# Initialize weights
weights = np.zeros((m, 1))

# Training loop
for epoch in range(epochs):
    # Compute predictions
    predictions = 1 / (1 + np.exp(-np.dot(X, weights)))
    
    # Compute gradient
    gradient = np.dot(X.T, (predictions - y)) / n
    
    # Update weights
    weights -= learning_rate * gradient
hard
A. Use a sparse matrix representation for X.
B. Use a smaller learning rate to ensure stability.
C. Vectorize the gradient computation to avoid loops.
D. Precompute the sigmoid function for all possible inputs.

Solution

  1. Step 1: Identify bottlenecks

    Matrix operations can be slow if X is dense and large.
  2. Step 2: Optimize matrix operations

    Using a sparse matrix representation for X reduces memory usage and speeds up computations.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Sparse matrices optimize large, sparse datasets [OK]
Hint: Sparse matrices optimize large, sparse datasets [OK]
Common Mistakes:
5. Which optimization would most effectively reduce the computational cost of the scaled dot-product attention mechanism for very large input sequences?
hard
A. Use a smaller batch size.
B. Use half-precision floating point operations.
C. Reduce the dimensionality of queries and keys.
D. Implement attention with sparse matrices.

Solution

  1. Step 1: Identify the bottleneck

    Computational cost is high due to dense matrix operations.
  2. Step 2: Choose the best optimization

    Sparse matrices reduce computation by focusing on non-zero elements.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Sparse matrices for large inputs [OK]
Hint: Sparse matrices for large inputs [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