Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Compute Intersection over Union (IoU) for two bounding boxes, a common metric in computer vision to measure overlap between a predicted box and a ground-truth box. IoU is defined as:
Implement the function
Rules:
[x1, y1, x2, y2].max(0, ...) so non-overlapping boxes give intersection area 0.area_a + area_b - intersection_area.Output:
| Argument | Type |
|---|---|
| box_a | np.ndarray |
| box_b | np.ndarray |
| Return Name | Type |
|---|---|
| value | float |
Input boxes are [x1,y1,x2,y2] floats
Use NumPy + Python only
Return scalar float IoU in [0,1]
Compute the overlap rectangle by taking max of the two top-left corners and min of the two bottom-right corners.
Intersection area is max(0, x_right-x_left) * max(0, y_bottom-y_top) so non-overlaps give 0.
IoU = inter_area / (area_a + area_b - inter_area); guard against zero union if boxes have zero area.