Linear Algebra with NumPy
Perform matrix multiplication, decompositions, and solving linear systems.
np.dot and @
Matrix multiplication: use np.dot(A, B) or the @ operator (Python 3.5+).
import numpy as np
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8]])
print(A @ B)
# [[19 22]
# [43 50]]
print(np.dot(A, B)) # same resultElement-wise vs Matrix Multiply
* is element-wise (Hadamard product). @ is matrix multiplication. Never confuse them.
import numpy as np
A = np.array([[1,2],[3,4]])
B = np.array([[2,0],[1,3]])
print(A * B) # [[ 2 0] [ 3 12]] — element-wise
print(A @ B) # [[ 4 6] [10 12]] — matrix multiplyAll lessons in this course
- NumPy Arrays and dtypes
- Array Operations and Broadcasting
- Indexing, Slicing, and Fancy Indexing
- Linear Algebra with NumPy