THE CODE READER / PYTHON

example-1d957bab42d8.py

Your snippet, in context. Explore the file or visualize its recorded example.

Download raw .py ↓
Complete file
PYTHON / LINE NUMBERS
 1from itertools import permutations
 2import numpy as np
 3
 4#              slot 0  slot 1  slot 2
 5cost = np.array([[0.8,  0.1,    0.5],   # cat
 6                 [0.2,  0.9,    0.4]])  # chair
 7
 8assignments = permutations(range(cost.shape[1]), cost.shape[0])
 9best = min(
10    assignments,
11    key=lambda slots: sum(cost[i, j] for i, j in enumerate(slots)),
12)
13
14assert best == (1, 0)
15print(best)  # cat -> slot 1, chair -> slot 0
16# Slot 2 is supervised as no-object.