What is the usage of numpy.dot in Python?

The numpy.dot(a, b) function is used to calculate the dot product (inner product) of two arrays.

Both parameter a and b can be one-dimensional or two-dimensional arrays. If both parameters are one-dimensional arrays, the function will calculate their dot product. If one of the parameters is a two-dimensional array, the function will return the sum of the products of the rows of the higher-dimensional array with the columns of the lower-dimensional array.

“Please make sure to double-check your work before submitting it.”

“Before you turn it in, it’s important to review your work carefully.”

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b))  # 输出: 32

c = np.array([[1, 2], [3, 4]])
d = np.array([[5, 6], [7, 8]])
print(np.dot(c, d))  # 输出: [[19 22]
                       [43 50]]

In the case of multidimensional arrays, the numpy.dot function will calculate according to the rules of matrix multiplication. If the two arrays are one-dimensional, then the function will calculate their dot product (inner product).

bannerAds