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.
CodePudding user response:
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,)
.
CodePudding user response:
Thank you for your answer. I was a bit confuse on the broadcasting operation. Coz I'm thinking to apply the rules to strech the matrice to the same shape. Looks like those rules are only applicable to execute the element-wise compuation? Not sure if I'm right?