← Back to NumPy Course | Chapter 6: Math Operations | Lesson 6 of 7

np.dot()

np.dot() multiplies vectors or matrices the linear-algebra way.

In this page:

  1. np.dot()
Syntax
python
np.dot(a, b)
a @ b

np.dot()

For two 1-D arrays it returns the inner product; for 2-D arrays it is matrix multiplication. Inner dimensions must match: (m, n) dot (n, p) gives (m, p). The @ operator is the modern equivalent for matrices.

Note: dot of two vectors sums the products of matching elements.

Example: np.dot()

python
import numpy as np

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

# Output:
# 32
# [[19 22]
#  [43 50]]
Related Topics
Common Mistakes
  1. Confusing dot with element-wise *
  2. Multiplying matrices with mismatched inner dimensions
  3. Assuming order does not matter
Chapter Summary
  • dot gives the inner product for vectors
  • It is matrix multiplication for 2-D
  • Inner dimensions must agree
  • Order matters
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.