Thenumpy.ndarray.dot()
function computes thedot product of two arrays. It is widely used inlinear algebra,machine learning anddeep learning for operations likematrix multiplication andvector projections.
Example:
Pythonimportnumpyasnpa=np.array([1,2,3])b=np.array([4,5,6])result=np.dot(a,b)print(result)
Understanding the Dot Product
- Ifboth inputs are 1D arrays,
dot()
computes theinner product, resulting in a scalar. - Ifeither input is an N-dimensional array,
dot()
performsmatrix multiplication. - Ifone input is a scalar,
dot()
performselement-wise multiplication.
Syntax : numpy.ndarray.dot(arr, out=None)
Parameters:
arr
(array_like): The input array for the dot product.out
(ndarray, optional): Output argument (stores the result).
Returns:
- Ascalar,vector ormatrix depending on input shape.
Code Implementation
Code #1 : Usingnumpy.ndarray.dot()
for Matrix Multiplication
Pythonimportnumpyasgeekarr1=geek.eye(3)arr=geek.ones((3,3))*3gfg=arr1.dot(arr)print(gfg)
Output[[3. 3. 3.] [3. 3. 3.] [3. 3. 3.]]
Code #2 : Performing Multiple Dot Products
Pythonimportnumpyasgeekarr1=geek.eye(3)arr=geek.ones((3,3))*3gfg=arr1.dot(arr).dot(arr)print(gfg)
Output[[27. 27. 27.] [27. 27. 27.] [27. 27. 27.]]
In this article, we explored thenumpy.ndarray.dot()
function, which computes the dot product of two arrays. We demonstrated its application using identity matrices and uniform arrays, highlighting its significance in matrix operations and numerical computing.