For two 1D arrays, vectors, np.dot(a, b) computes the standard dot product: multiplying corresponding elements and summing the results into a single scalar. For two 2D arrays, it performs conventional matrix multiplication, identical to the @ operator in that specific case. For higher-dimensional inputs, its behavior generalizes in a way that's less intuitive, a sum-product over the last axis of a and the second-to-last axis of b, which is exactly why @, or np.matmul(), is generally preferred over dot() for anything beyond simple vectors, since matmul's rules for arrays with more than 2 dimensions are more predictable and batch-oriented.
1Understanding np.dot()
For two 1D arrays, vectors, np.dot(a, b) computes the standard dot product: multiplying corresponding elements and summing the results into a single scalar. For two 2D arrays, it performs conventional matrix multiplication, identical to the @ operator in that specific case. For higher-dimensional inputs, its behavior generalizes in a way that's less intuitive, a sum-product over the last axis of a and the second-to-last axis of b, which is exactly why @, or np.matmul(), is generally preferred over dot() for anything beyond simple vectors, since matmul's rules for arrays with more than 2 dimensions are more predictable and batch-oriented.
For plain matrix multiplication, prefer @ (or np.matmul()) over np.dot() — they agree for 1D and 2D inputs, but dot()'s behavior for higher-dimensional arrays is less intuitive and can silently do something different from what matmul() would.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b))2Practical Example
Here is a real-world application of np.dot() showing how it is used in production NumPy code.
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(np.dot(A, B))3Best Practices
Follow these guidelines when working with np.dot():
1. Use np.dot() specifically for vector dot products and simple 2D matrix multiplication, where its behavior matches expectations exactly
2. Prefer @ or np.matmul() over np.dot() for anything involving batches of matrices (3D+ arrays), since their broadcasting-aware behavior is more predictable
3. Remember np.dot() on two 1D arrays returns a plain scalar, not a 1-element array
Tip: For plain matrix multiplication, prefer @ (or np.matmul()) over np.dot() — they agree for 1D and 2D inputs, but dot()'s behavior for higher-dimensional arrays is less intuitive and can silently do something different from what matmul() would.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b))