Where Do the Mistakes Pile Up?

Model EvaluationMedium+20 XP+20

Where Do the Mistakes Pile Up?

A wildlife charity runs a classifier over its camera-trap photos. Its test results come as a confusion matrix: matrix[i][j] counts the photos whose actual animal is classes[i] and whose predicted animal is classes[j]. Rows are actual classes, columns are predicted classes, and the diagonal holds the correct predictions.

Before spending money on more photos, the team wants three facts from the matrix.

  1. The most-confused pair. For two different classes aaa and bbb, their confusions are the aaa photos predicted as bbb plus the bbb photos predicted as aaa. Find the pair with the most confusions and return it as a tuple, its two classes in the order they appear in classes. If pairs tie, take the one whose first class comes earliest in classes, and then the one whose second class does.
  2. The class with the most errors. A class's errors are its actual photos that were predicted as anything else. Ties go to the class that comes earliest in classes.
  3. The class with the highest error rate, and that rate. A class's error rate is its errors divided by its number of actual photos. Skip any class with no actual photos. Ties go to the class that comes earliest in classes. Round the rate to 4 decimal places.

Task: write error_report(classes, matrix) and return the tuple (pair, most_errors_class, highest_rate_class, highest_rate).

There are always at least two classes, and at least one class has actual photos.

Confusion Matrix by Slice

Compare the Overall matrix to the Night matrix.

Cat
Dog
Horse
Cat
80
15
5
Dog
18
75
7
Horse
4
8
88

Examples

classes = ["deer", "fox", "badger", "hare"]
matrix = [[300, 6, 3, 9], [4, 36, 11, 2], [1, 2, 12, 5], [8, 0, 1, 31]]
returns (('deer', 'hare'), 'deer', 'badger', 0.4)
classes = ["positive", "neutral", "negative"]
matrix = [[220, 25, 3], [9, 20, 11], [1, 6, 47]]
returns (('positive', 'neutral'), 'positive', 'neutral', 0.5)

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run