20/26
Part 4 of 4 · Multiply, reshape, and put it togetherLearn and do

@ multiplies the last two axes

The final grade weights the tests: 20% for test 1, 30% for test 2, 50% for test 3.

# one weight per test
weights = np.array([0.2, 0.3, 0.5])
print(square @ weights)
[83.  70.  74.5]

@ is matrix multiply. Here it multiplies each student's row by the weights, number by number, and adds them up. That's one final grade per student. Ana: 0.2*70 + 0.3*80 + 0.5*90 = 83.

Tap a grade to see its row of scores meet the weights:

square @ weightsInteractive
square(3, 3)@weights(3,)→(3,)the two 3s meet, get added up, and disappear
weightstest 1test 2test 3
test 1test 2test 3AnaBenCal
grade
Ben: 0.2×50 + 0.3×100 + 0.5×60 = 70. One row of square times one column of weights, number by number, added up.

Now compare two ways of grading. Scheme A is the weights above. Scheme B counts only test 3. Put each scheme in a column:

# test 1
schemes = np.array([[0.2, 0.0],
                    # test 2
                    [0.3, 0.0],
                    # test 3
                    [0.5, 1.0]])
print(square @ schemes)
[[83.  90. ]
 [70.  60. ]
 [74.5 75. ]]

Each row is a student. Each column is a scheme. Entry [i, j] is student i's row times scheme j's column, added up. Ben gets 70 under scheme A and 60 under scheme B.

square @ schemesInteractive
square(3, 3)@schemes(3, 2)→(3, 2)the two 3s meet, get added up, and disappear
scheme Ascheme Btest 1test 2test 3
test 1test 2test 3AnaBenCal
scheme Ascheme B
Ben, scheme A: 0.2×50 + 0.3×100 + 0.5×60 = 70. One row of square times one column of schemes, number by number, added up.

In the editor, fill in schemes with one column per scheme. Scheme A is the weights above. Scheme B counts tests 2 and 3, half each. Then fill in grades with one @: one row per student, one column per scheme.