.dot()
In NumPy, the np.dot()
method computes the dot product of two arrays. For 1D arrays, it returns a scalar value. For 2D arrays, it performs matrix multiplication and returns a new 2D array containing the result.
The np.dot()
method is vital in many fields such as linear algebra, machine learning, and data science. In machine learning, it’s used in operations like calculating weights and biases in neural networks. In data science, it helps in performing projections and transformations of data.
Syntax
numpy.dot(a, b, out=None)
Parameters:
a
: The first array, which could be 1D, 2D, or higher dimensional.b
: The second array, having compatible dimensions witha
, and also could be 1D, 2D, or higher dimensional.out
(Optional): An array for storing the result. If not provided, a new array is created.
Return value:
- If both inputs are scalars: Returns their product.
- If both are 1D arrays: Returns their dot product (a scalar).
- If both are 2D arrays: Returns their matrix product.
Example 1: Product of Two Scalars
This example uses the .dot()
method to calculate the product of two scalars:
import numpy as np# Define two scalarsnum1 = 3num2 = 5# Compute the product of the scalarsres = np.dot(num1, num2)# Print the resultprint(res)
The output for the example will be:
15
Example 2: Dot Product of Two Arrays
This example uses the .dot()
method to calculate the dot product of two arrays:
import numpy as np# Define two arraysarr1 = [1, 2, 3]arr2 = [4, 5, 6]# Compute the dot product of the arraysres = np.dot(arr1, arr2)# Print the resultprint(res)
The output for the example will be:
32
Codebyte Example: Matrix Product of Two Arrays
This codebyte example uses the .dot()
method to calculate the matrix product of two arrays:
The output for the example will be:
[[19 22][43 50]]
Frequently Asked Questions
1. What’s the difference between np.dot()
and np.matmul()
?
np.dot()
and np.matmul()
behave similarly for 2D arrays. However, np.matmul()
is preferred for matrix multiplication in higher dimensions (3D and above), as it strictly follows matrix multiplication rules, whereas np.dot()
may flatten or broadcast inputs differently.
2. What happens if the input shapes are incompatible?
If the shapes of the inputs don’t align according to dot product rules (i.e., the number of columns in a
must match with the number of rows in b
for performing matrix multiplication), NumPy will raise a ValueError
indicating that the shapes are not aligned.
3. Is np.dot()
efficient for large-scale operations?
Yes, np.dot()
is optimized and internally uses BLAS (Basic Linear Algebra Subprograms) libraries for high performance, especially on large arrays or matrices.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours