Bird
Raised Fist0

Implement Precision, Recall, F1 from Confusion Matrix

CODINGbeginnerInterview freq: very_high
Choose your preparation mode6 modes available
🤖
Implement Precision, Recall, F1 from Confusion Matrix
Calculate classification metrics from confusion matrix
codingbeginnerFreq: very_highEvaluation Coding
Prerequisites:Classification Metrics - Precision, Recall, F1
📝
Problem Statement

Implement functions to calculate Precision, Recall, and F1-score from a given confusion matrix.

Input: A confusion matrix as a 2D NumPy array. Output: Precision, Recall, and F1-score as floats.
Input: np.array([[50, 10], [5, 35]]) → Output: Precision: 0.8333, Recall: 0.875, F1-score: 0.8537
🎯
Interview Angles
  • Testing understanding of classification metrics and their calculation.
  • Evaluating ability to handle edge cases like division by zero.
  • Assessing knowledge of NumPy operations for efficient computation.
  • Understanding of multi-class classification evaluation.
🔵
Naive Approach

Calculate precision, recall, and F1-score by manually extracting true positives, false positives, and false negatives from the confusion matrix.

Time: O(1) - constant time operations. Space: O(1) - no additional space used.
Why Insufficient
This approach is straightforward but lacks generalization for multi-class confusion matrices.
🐍
Naive Code
Python
import numpy as np

def calculate_metrics_naive(confusion_matrix):
    tp = confusion_matrix[0, 0]
    fp = confusion_matrix[0, 1]
    fn = confusion_matrix[1, 0]
    precision = tp / (tp + fp) if (tp + fp) > 0 else 0.0
    recall = tp / (tp + fn) if (tp + fn) > 0 else 0.0
    f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0.0
    return precision, recall, f1_score

# Driver example
confusion_matrix = np.array([[50, 10], [5, 35]])
precision, recall, f1_score = calculate_metrics_naive(confusion_matrix)
print(f"Precision: {precision:.4f}, Recall: {recall:.4f}, F1-score: {f1_score:.4f}")
💡
Key Insight
Use NumPy operations to handle multi-class confusion matrices efficiently by summing over appropriate axes.
🐍
Optimized Code
Python
import numpy as np

def calculate_metrics_optimized(confusion_matrix):
    tp = np.diag(confusion_matrix)
    fp = np.sum(confusion_matrix, axis=0) - tp
    fn = np.sum(confusion_matrix, axis=1) - tp
    precision = np.sum(tp) / (np.sum(tp) + np.sum(fp)) if (np.sum(tp) + np.sum(fp)) > 0 else 0.0
    recall = np.sum(tp) / (np.sum(tp) + np.sum(fn)) if (np.sum(tp) + np.sum(fn)) > 0 else 0.0
    f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0.0
    return precision, recall, f1_score

# Driver example
confusion_matrix = np.array([[50, 10], [5, 35]])
precision, recall, f1_score = calculate_metrics_optimized(confusion_matrix)
print(f"Precision: {precision:.4f}, Recall: {recall:.4f}, F1-score: {f1_score:.4f}")
🔍
Line-by-Line
tp = np.diag(confusion_matrix)
Extracts true positives for each class from the diagonal of the confusion matrix.
fp = np.sum(confusion_matrix, axis=0) - tp
Calculates false positives by summing columns and subtracting true positives.
fn = np.sum(confusion_matrix, axis=1) - tp
Calculates false negatives by summing rows and subtracting true positives.
precision = np.sum(tp) / (np.sum(tp) + np.sum(fp)) if (np.sum(tp) + np.sum(fp)) > 0 else 0.0
Computes overall precision, handling division by zero.
recall = np.sum(tp) / (np.sum(tp) + np.sum(fn)) if (np.sum(tp) + np.sum(fn)) > 0 else 0.0
Computes overall recall, handling division by zero.
f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0.0
Computes F1-score, handling division by zero.
return precision, recall, f1_score
Returns the calculated metrics as a tuple.
⚡
Complexity
Time: O(n) - n is the number of classes, operations are linear in the number of classes. Space: O(1) - uses constant space for calculations.
Precision: 0.8333, Recall: 0.8750, F1-score: 0.8537
🔲
Edge Cases
All zeros in confusion matrix
Returns zero for all metrics due to division by zero handling.
if (np.sum(tp) + np.sum(fp)) > 0 else 0.0
Single class confusion matrix
Correctly calculates metrics for a single class.
np.diag(confusion_matrix)
⚠️
Common Mistakes
Symptom: Program crashes with a ZeroDivisionError.
Symptom: Incorrect precision, recall, or F1-score values.
Symptom: Inefficient code with unnecessary loops.
Symptom: Swapped precision and recall values.

Practice

(1/5)
1. What is the time complexity of computing the numerical gradient for a function with n parameters using central difference?
easy
A. O(1)
B. O(n^2)
C. O(n log n)
D. O(n)

Solution

  1. Step 1: Analyze the loop structure

    The numerical gradient computation involves a loop over n parameters, each requiring constant time operations.
  2. Step 2: Determine the complexity

    Each parameter requires a constant number of operations, leading to O(n) complexity.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Loop over n parameters, each constant time. [OK]
Hint: Loop over n parameters, each constant time. [OK]
Common Mistakes:
2. What is the time complexity of the following non-max suppression implementation?
def non_max_suppression(boxes, scores, iou_threshold):
    indices = np.argsort(scores)[::-1]
    keep = []
    while len(indices) > 0:
        current = indices[0]
        keep.append(current)
        indices = indices[1:]
        indices = [i for i in indices if iou(boxes[current], boxes[i]) < iou_threshold]
    return keep
easy
A. O(n^3)
B. O(n^2)
C. O(n)
D. O(n log n)

Solution

  1. Step 1: Analyze the loop structure

    The outer loop runs n times, and the inner loop (IoU calculation) also runs n times in the worst case.
  2. Step 2: Determine complexity

    Each IoU calculation is O(1), making the overall complexity O(n^2).
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Nested loops suggest O(n^2) [OK]
Hint: Nested loops suggest O(n^2) [OK]
Common Mistakes:
3. In the context of logistic regression, which approach is correct for handling a dataset with highly imbalanced classes?
medium
A. Use accuracy as the primary evaluation metric.
B. Apply SMOTE to balance the dataset.
C. Increase the learning rate to improve minority class prediction.
D. Use a linear activation function instead of sigmoid.

Solution

  1. Step 1: Identify the problem with imbalanced classes

    Imbalanced classes can lead to biased predictions towards the majority class.
  2. Step 2: Choose a method to address imbalance

    SMOTE (Synthetic Minority Over-sampling Technique) is effective for balancing datasets.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    SMOTE balances classes effectively [OK]
Hint: SMOTE balances classes effectively [OK]
Common Mistakes:
4. How would you modify the scaled dot-product attention mechanism to handle variable-length sequences efficiently?
medium
A. Use padding and mask the padded values.
B. Use a fixed-size window for all sequences.
C. Truncate all sequences to the same length.
D. Ignore sequences longer than a threshold.

Solution

  1. Step 1: Identify the challenge

    Variable-length sequences require handling of different sequence lengths.
  2. Step 2: Choose the correct approach

    Padding with masking is efficient and maintains sequence integrity.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Pad and mask for variable lengths [OK]
Hint: Pad and mask for variable lengths [OK]
Common Mistakes:
5. How can you optimize the K-Means implementation to improve its performance on large datasets?
hard
A. Use the Elkan algorithm to reduce distance calculations.
B. Increase the number of iterations.
C. Decrease the number of clusters.
D. Use a smaller subset of data for training.

Solution

  1. Step 1: Identify performance bottlenecks

    The Elkan algorithm optimizes K-Means by reducing the number of distance calculations needed.
  2. Step 2: Eliminate wrong options

    Increase the number of iterations. increases computation. Decrease the number of clusters. may not be suitable for the data's natural structure. Use a smaller subset of data for training. reduces accuracy.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Elkan reduces distance calculations. [OK]
Hint: Elkan reduces distance calculations. [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