0

In the following code, why is there an output, mathematically it should give an error

import numpy as np                                                                                                                                                      
arr = np.array([[1., 2., 3.], [4., 5., 6.]])                                                                                                                                                                                                                                                                                               

out:
array([[1., 2., 3.],
       [4., 5., 6.]])

arr*arr                                                                                                                                                                  

out : array([[ 1.,  4.,  9.],
            [16., 25., 36.]])
1
  • 3
    That's element-wise multiplication when operating on arrays. The operator for matrix multiplication is @ Commented Jun 22, 2021 at 14:20

1 Answer 1

2

The * operator multiplies values in place. It seems like you want the @ operator, which performs a dot product between the two matrices.

Example:

a = np.array([[1, 2])
b = np.array([[3], [5]])

a * b returns: [[3, 6],[5, 10]]

a @ b returns: 13

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.