There are three multiplications in numpy, they are np.multiply(), np.dot() and * operation. In this tutorial, we will use some examples to disucss the differences among them for python beginners, you can learn how to use them correctly by this tutorial.
1. As to np.multiply() operation
1.1 np.multiply() on numpy array
We create two 2*2 numpy array (A, B) to show the value of np.multiply().
import numpy as np A = np.array([[1, 2], [3, 4]]) B = np.array([[1, 1], [2, 2]]) c = np.multiply(A, B) print(c)
The value of c is:
[[1 2] [6 8]]
Fromt the result, we can find the value of c is hadamard product of A and B.
1.2 np.multiply() on numpy matrix
We convert A and B to numpy matrix, then calculate np.multiply(A, B)
A = np.mat(A) B = np.mat(B) c = np.multiply(A,B) print(c)
The value of c is also:
[[1 2] [6 8]]
1.3 np.multiply() on numpy array vector
We create two numpy array vectors A and B.
The shape of vector is (num, ).
A = np.array([1, 2, 3, 4]) B = np.array([1, 1, 2, 2]) c = np.multiply(A,B) print(c)
The value of c is:
[1 2 6 8]
Which means that the value of c is also hadamard product of A and B.
2. As to np.dot() operation
2.1 np.dot() on numpy array
We create two 2*2 numpy array (A, B) to show the value of np.dot().
import numpy as np A = np.array([[1, 2], [3, 4]]) B = np.array([[1, 1], [2, 2]]) c = np.dot(A, B) print(c)
Run this code, we will find the value of c is:
[[ 5 5] [11 11]]
which means that np.dot(A,B) is matrix multiplication on numpy array.
2.2 np.dot() on numpy matrix
We convert these two numpy array (A, B) to numpy matrix.
A = np.mat(A) B = np.mat(B) c = np.dot(A,B) print(c)
Run this code, the value of c is:
[[ 5 5] [11 11]]
Which means that np.dot(A,B) is matrix multiplication on numpy matrix.
2.3 np.dot() on numpy array vector
Here are two array vectors (A, B)
A = np.array([1, 2, 3, 4]) B = np.array([1, 1, 2, 2]) c = np.dot(A,B) print(c)
The value of c is:
17
From the result, we can find np.dot(A, B) will sum all the values in A * B.
3. As to * Operation
3.1 * operation on numpy array
Here we create two 2*2 numpy array (A, B) to show the value of * operation.
import numpy as np A = np.array([[1, 2], [3, 4]]) B = np.array([[1, 1], [2, 2]]) c = A * B print(c)
Run this code, the value of c is:
[[1 2] [6 8]]
From the result, we will find: the value of c is hadamard product of A and B.
3.2 * operation on numpy matrix
We will convert two 2*2 numpy array (A, B) to matrix.
A = np.mat(A) B = np.mat(B)
The type of A and B is <class ‘numpy.matrixlib.defmatrix.matrix’>, not numpy.ndarray.
Then we wil calculate A * B
c = A * B print(c)
Run this code, the value of c is:
[[ 5 5] [11 11]]
We will find A * B is matrix multiplication.
3.3 * operation on numpy array vector
We also can use * to multipy two array vector
A = np.array([1, 2, 3, 4]) B = np.array([1, 1, 2, 2]) c = A * B print(c)
We can find the value of c is:
[1 2 6 8]
Which is the value of hadamard product of A and B.
To summurize:
NumPy Array | NumPy Matrix | NumPy Array Vector | |
np.multiply(A, B) | Hadamard Product | Hadamard Product | Hadamard Product |
np.dot(A, B) | Matrix Multiplication | Matrix Multiplication | Sum of Hadamard Product |
A * B | Hadamard Product | Matrix Multiplication | Hadamard Product |