Matrix vector multiplication Numpy np.dot () and np.multiply () and the difference between *

Numpy in matrix-vector multiplication are np.dot (a, b), np.multiply (a, b) * and, when just getting started with the rather ambiguous, then his own order a bit. First to introduce the theory, and then combined with in-depth look at an example.

Array matrix
Multiplication np.multiply (a, b), or a * b np.multiply (a, b)
Matrix Multiplication np.dot(a,b) np.dot (a, b), or a * b

We can see that:
when the time the object is an array, the elements corresponding to a multiplication using np.multiply (a, b), or a * b, matrix multiplication with np.dot (a, b)
when the object is a matrix when multiplying the corresponding elements used np .multiply (a, b), matrix multiplication with np.dot (a, b), or a * b

Note: the matrix array and the corresponding elements are multiplied, the multiplied output of the array / matrix of the same size

For np.array objects

>>> a
array([[1, 2],
       [3, 4]])

When the object is an array when using the multiplication element np.multiply (a, b), or a * b

>>> np.multiply(a,a)
array([[ 1,  4],
       [ 9, 16]])
       
>>> a*a
array([[ 1,  4],
       [ 9, 16]])

When the object is an array when the matrix multiplication with np.dot (a, b), np.matmul (a, b) or a.dot (b)

>>> np.dot(a,a)
array([[ 7, 10],
      [15, 22]])
      
>>> np.matmul(a,a)
array([[ 7, 10],
      [15, 22]])
      
>>> a.dot(a)
array([[ 7, 10],
      [15, 22]])

For np.matrix objects

>>> A
matrix([[1, 2],
        [3, 4]])

When the object is the matrix when the multiplication element using np.multiply (a, b)

>>> np.multiply(A,A)
matrix([[ 1,  4],
        [ 9, 16]])

When the object is a matrix when the matrix multiplication with np.dot (a, b), np.matmul (A, A), a.dot (b) or a * b

>>> np.dot(A,A)
matrix([[ 7, 10],
        [15, 22]])
        
>>> np.matmul(A,A)
matrix([[ 7, 10],
        [15, 22]])
        
>>> A.dot(A)
matrix([[ 7, 10],
        [15, 22]])
        
>>> A*A
matrix([[ 7, 10],
        [15, 22]])
Released seven original articles · won praise 26 · views 4939

Guess you like

Origin blog.csdn.net/hxx123520/article/details/104713555