Search code examples
numpybroadcast

Shape of result: np.random.randn(10,5) @ np.random.randn(5)


What is the shape of the result equals to 10 for this numpy equation: np.random.randn(10,5) @ np.random.randn(5). Thank you folks.


Solution

  • According to the documentation page for numpy.matmul:

    If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed.

    This means after appending an extra dimension on the second operand, the operation is performed between two 2D arrays (10, 5) and (5, 1). The matrix multiplication follows the (i, j) @ (j, k) = (i, k) rule, so the output is shaped (10, 1), and the extra appended dimension is then removed: (10,).