0Pricing
Python Academy · Lesson

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 result

Element-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 multiply

All lessons in this course

  1. NumPy Arrays and dtypes
  2. Array Operations and Broadcasting
  3. Indexing, Slicing, and Fancy Indexing
  4. Linear Algebra with NumPy
← Back to Python Academy