user14635413
user14635413

Reputation:

matrices multiplication using numpy

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.]])

Upvotes: 0

Views: 50

Answers (1)

Jonathan Schwartz
Jonathan Schwartz

Reputation: 86

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

Upvotes: 2

Related Questions