Practice
1.
import numpy as np
def iou(box1, box2):
x1 = max(box1[0], box2[0])
y1 = max(box1[1], box2[1])
x2 = min(box1[2], box2[2])
y2 = min(box1[3], box2[3])
intersection = max(0, x2 - x1) * max(0, y2 - y1)
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
union = area1 + area2 - intersection
return intersection / union
box1 = [1, 1, 3, 3]
box2 = [2, 2, 4, 4]
print(iou(box1, box2))easy
Solution
Step 1: Calculate intersection and union areas
Intersection area is 1, union area is 7.Step 2: Compute IoU
IoU = 1/7 = 0.14285714285714285.Final Answer:
Option C -> Option CQuick Check:
Intersection over Union formula [OK]
Hint: Intersection over Union formula [OK]
Common Mistakes:
2. What is the time complexity of computing the ROC curve using the sklearn library?
easy
Solution
Step 1: Understand ROC computation
ROC curve computation involves sorting the scores, which is O(n log n).Step 2: Eliminate wrong options
O(n^2) is incorrect as it suggests quadratic complexity, O(n) suggests linear complexity which ignores sorting, and O(log n) is too low.Final Answer:
Option A -> Option AQuick Check:
ROC curve involves sorting, hence O(n log n) [OK]
Hint: ROC curve involves sorting, hence O(n log n) [OK]
Common Mistakes:
3. In a multi-class classification problem, how should you modify the confusion matrix implementation to handle more than two classes?
medium
Solution
Step 1: Understand multi-class needs
Each class needs its own row and column in the matrix.Step 2: Choose correct matrix size
A (n_classes, n_classes) matrix captures all class interactions.Final Answer:
Option D -> Option DQuick Check:
Multi-class needs (n_classes, n_classes) matrix. [OK]
Hint: Multi-class needs (n_classes, n_classes) matrix. [OK]
Common Mistakes:
4. In a KNN classifier using KD-Tree, which scenario would make a brute force approach more suitable?
medium
Solution
Step 1: Understand KD-Tree limitations
KD-Trees are inefficient in high-dimensional spaces due to the curse of dimensionality.Step 2: Evaluate scenarios
A: Large datasets benefit from KD-Trees. C: Small datasets are manageable with brute force. D: Categorical features require different handling.Final Answer:
Option A -> Option AQuick Check:
High dimensions favor brute force over KD-Tree [OK]
Hint: High dimensions favor brute force over KD-Tree [OK]
Common Mistakes:
5. How would you adapt the ROC curve implementation to handle multi-class classification?
medium
Solution
Step 1: Understand multi-class ROC adaptation
One-vs-rest strategy is commonly used to compute ROC for each class separately.Step 2: Eliminate wrong options
Use the average of ROC curves for each class. is incorrect as averaging doesn't apply directly, Options C and D are mathematically incorrect.Final Answer:
Option A -> Option AQuick Check:
Use one-vs-rest for multi-class ROC [OK]
Hint: Use one-vs-rest for multi-class ROC [OK]
Common Mistakes:
