Mean IoU from a Confusion Matrix

~12 mincode completion

Implement mean_iou(confusion) returning the mean of the per-class IoU.

Examples

Symmetric 2-class confusion: both IoUs are 3/5

Input
mean_iou([[3, 1], [1, 3]])
Output
0.6

A diagonal confusion matrix is perfect

Input
mean_iou([[5, 0], [0, 5]])
Output
1

Three classes: mean of 2/3, 3/5, 1/2

Input
mean_iou([[4, 1, 0], [0, 3, 1], [1, 0, 2]])
Output
0.58889

Hints

Hint 1

The reduction runs down the columns, so pass .

Hint 2

Watch for this: divided by row plus column without subtracting the diagonal.

Requirements

  • confusion: (K, K) array, confusion[i, j] = pixels of true class i

  • Return scalar mean of the K per-class IoU values

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

8 employers weight this skill

4 autonomy companies, 1 defense company, 1 health and bio company, 1 enterprise vendor, 1 AI product company. Top match scores 81.

Python
import numpy as np


def mean_iou(confusion):
    """
    Mean intersection over union from a confusion matrix.

    Args:
        confusion: (K, K) array, confusion[i, j] = pixels of true class i
                   predicted as class j

    Returns:
        scalar mean of the K per-class IoU values
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.