Numpy ndarray.dot() function | Python (original) (raw)
Last Updated : 07 Apr, 2025
The **numpy.ndarray.dot()
*function computes the dot product of two arrays. It is widely used in **linear algebra,* **machine learning and **deep learning for operations like **matrix multiplication and **vector projections.
**Example:
Python `
import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) result = np.dot(a, b) print(result)
`
**Understanding the Dot Product
- If **both inputs are 1D arrays,
dot()
computes the **inner product, resulting in a scalar. - If **either input is an N-dimensional array,
dot()
performs **matrix multiplication. - If **one input is a scalar,
dot()
performs **element-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:
- A **scalar, **vector or **matrix depending on input shape.
Code Implementation
**Code #1 : Using numpy.ndarray.dot()
for Matrix Multiplication
Python `
import numpy as geek arr1 = geek.eye(3) arr = geek.ones((3, 3)) * 3 gfg = arr1.dot(arr) print(gfg)
`
Output
[[3. 3. 3.] [3. 3. 3.] [3. 3. 3.]]
**Code #2 : Performing Multiple Dot Products
Python `
import numpy as geek arr1 = geek.eye(3) arr = geek.ones((3, 3)) * 3 gfg = arr1.dot(arr).dot(arr) print(gfg)
`
Output
[[27. 27. 27.] [27. 27. 27.] [27. 27. 27.]]
In this article, we explored the numpy.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.