Np.dot() in Python Numpy generates the product of two arrays. Specifically, for np.dot(a, b)
,
Situation 1:
If both a and b are 1-D arrays, it is inner product of vectors.
Situation 2:
If both a and b are 2-D arrays, it is matrix multiplication. Using matmul
or a @ b
is preferred.
Situation 3:
If either a or b is 0-D (scalar), it is equivalent to multiply
. Using np.multiply(a, b)
or a * b
is preferred.
Example 1: both a and b are 1-D arrays
The following shows how two 1-D arrays is calculated when using np.dot().
1-D array a:
1 | 2 | 3 |
1-D array b:
3 | 4 | 7 |
np.dot(a, b):
1*3+2*4+3*7 = 32 |
The following is the python code to replicate what is shown above.
# import numpy
import numpy as np
# generate two 1-D arrays
a = np.array([1,2,3])
b = np.array([3,4,7])
# check data type
print(type(a))
print(type(b))
# use np.dot()
np.dot(a,b)
Output:
<class 'numpy.ndarray'> <class 'numpy.ndarray'> 32
Note that, every for two lists, it will behave the same. The following is the illustration.
# import numpy
import numpy as np
# generate two lists
a = [1,2,3]
b=[3,4,7]
# check data type
print(type(a))
print(type(b))
# use np.dot()
np.dot(a,b)
Output:
<class 'list'> <class 'list'> 32
Example 2: a and b are 2-D arrays
2-D array a:
1 | 2 | 4 |
2 | 1 | 5 |
2-D array b:
3 | 2 |
2 | 1 |
4 | 4 |
np.dot(a, b):
1*3+2*2+4*4=23 | 1*2+2*1+4*4=20 |
2*3+1*2+5*4=28 | 2*2+1*1+5*4=25 |
The following is the python code to replicate what is shown above.
# import numpy
import numpy as np
# create array_a
array_a=np.array([[1,2,4],[2,1,5]])
print("array_a:\n",array_a)
# create array_b
array_b=np.array([[3,2],[2,1],[4,4]])
print("array_b:\n",array_b)
# apply np.dot()
np.dot(array_a,array_b)
Output:
array_a: [[1 2 4] [2 1 5]] array_b: [[3 2] [2 1] [4 4]] array([[23, 20], [28, 25]])
You can also use numpy.matmul() for the same purpose. @
operator can be used as a shorthand for np.matmul
on ndarrays.
# use np.matmul() will generate the same result as np.dot()
np.matmul(array_a,array_b)
Output:
array([[23, 20], [28, 25]])
# use @ will generate the same result as np.dot()
array_a@array_b
Output:
array([[23, 20], [28, 25]])
Example 3: a or b is scalar
scalar a (0-D):
3
2-D array b:
3 | 2 |
2 | 1 |
4 | 4 |
np.dot(a, b):
3*3=9 | 3*2=6 |
3*2=6 | 3*1=3 |
3*4=12 | 3*4=12 |
The following is the python code to replicate what is shown above.
# import numpy
import numpy as np
# create scalar_a
scalar_a=3
print("scalar_a:\n",scalar_a)
print("check whether it is scalar:\n",np.isscalar(scalar_a))
# create array_b
array_b=np.array([[3,2],[2,1],[4,4]])
print("array_b:\n",array_b)
# apply np.dot()
np.dot(scalar_a,array_b)
Output:
scalar_a: 3 check whether it is scalar: True array_b: [[3 2] [2 1] [4 4]]
array([[ 9, 6], [ 6, 3], [12, 12]])
Since scalar_a is a scalar, we can just use * to do the calculation. Below is the Python code. We can see that it will generate the same result as np.dot().
# use * for the production of a scalar and a 2-D array
scalar_a*array_b
Output:
array([[ 9, 6], [ 6, 3], [12, 12]])
Note that, since scalar_a is a scalar, we can not use the @
operator here. If we use it, it will generate error.
# use @ for the production of a scalar and a 2-D array. It will generate error.
scalar_a@array_b
Output:
ValueError: matmul: Input operand 0 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)