17/26
Part 3 of 4 · Picking entries out of a tableLearn and do
Two lists pick one entry per row
Each student chose one test to retake. Which score is that, for each student?
# Ana chose test 1, Ben test 3, Cal test 2
chosen = np.array([0, 2, 1])
print(square[np.arange(3), chosen])
[70 60 70]
np.arange(3) is [0, 1, 2], one number per row. The two lists pair up, position by position. The other two buttons are the usual mistakes.
square (3, 3)test 1
0test 2
1test 3
20 Ana7080901 Ben50100602 Cal807075
0test 2
1test 3
20 Ana7080901 Ben50100602 Cal807075
The two lists pair up, position by position
np.arange(3)chosenresultresult shape (3,): one number per student
Pair 1:
square[1, 2] is Ben, test 3: 60You get one number per student. In machine learning the same line pulls out the probability of the right answer for each example: probs[np.arange(n), labels].
Don't write square[:, chosen] for this. : keeps every row, and chosen then picks columns 0, 2 and 1 from each row. That's a (3, 3) table, not one score per student.
In the editor, write pick(probs, labels): entry i is probs[i, labels[i]]. The editor runs it on square with each student's chosen test, and the checks run it on probabilities.