🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.dot()

AI & DATA SCIENCE // np-dot

np.dot() computes the dot product for 1D arrays, matrix multiplication for 2D arrays, and a generalized sum-product over the last axes for higher-dimensional arrays.

Syntax

np.dot(a, b)
a.dot(b)

Deep Dive Course

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.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b))
localhost:3000

2Practical Example

Here is a real-world application of np.dot() showing how it is used in production NumPy code.

editor.html
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(np.dot(A, B))
localhost:3000

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.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b))
Example 02Advanced Example
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(np.dot(A, B))

Best Practices

  • Use np.dot() specifically for vector dot products and simple 2D matrix multiplication, where its behavior matches expectations exactly
  • Prefer @ or np.matmul() over np.dot() for anything involving batches of matrices (3D+ arrays), since their broadcasting-aware behavior is more predictable
  • Remember np.dot() on two 1D arrays returns a plain scalar, not a 1-element array

Interview Question

For two 2D arrays, how does np.dot() compare to the @ operator?

Hint: Think about where their behavior matches, and where it diverges.

For two 2D arrays specifically, np.dot(A, B) and A @ B produce exactly the same result, since both perform standard matrix multiplication in that case. The difference only shows up for arrays with more than 2 dimensions: @, and np.matmul(), treats extra leading dimensions as a batch of matrices and multiplies corresponding pairs, following NumPy's broadcasting rules, while np.dot()'s generalization to higher dimensions follows a different, less intuitive rule that usually isn't what you actually want for batched matrix operations.

Exercises

MediumPractice using np.dot() in a real scenario.
View Solution
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.dot(a, b))

Frequently Asked Questions

For two 2D arrays, how does np.dot() compare to the @ operator?

For two 2D arrays specifically, np.dot(A, B) and A @ B produce exactly the same result, since both perform standard matrix multiplication in that case. The difference only shows up for arrays with more than 2 dimensions: @, and np.matmul(), treats extra leading dimensions as a batch of matrices and multiplies corresponding pairs, following NumPy's broadcasting rules, while np.dot()'s generalization to higher dimensions follows a different, less intuitive rule that usually isn't what you actually want for batched matrix operations.

Related Functions

np-matmulnp-vdotnp-inner