Skip to content
DeepForgeby svx
All problems
EasyML Fundamentalsml-177

Top-2 Margin

Problem

Compute the top-2 margin of each probability row: the largest value minus the second largest (0.0 for rows with fewer than two entries).

Return the list of margins; empty input returns [].

Hint

A small margin means the model is uncertain between the top two classes.

Test cases

case 1
in: [[[0.7,0.2,0.1],[0.5,0.5,0],[0.3,0.3,0.3]]]
expected: [0.49999999999999994,0,0]
case 2
in: [[[0.9]]]
expected: [0]
case 3
in: [[[0.2,0.8]]]
expected: [0.6000000000000001]
case 4
in: [[]]
expected: []
Starter codePython
def top2_margin(probs):
    # Your code here
    pass

4 test cases · run your code against them in the editor