Linear Algebra with NumPy
Perform matrix multiplication, decompositions, and solving linear systems.
Linear Algebra with NumPy is a free Python Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 multiplyTranspose
arr.T transposes the array (swaps rows and columns). For higher dimensions, use np.transpose(arr, axes).
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
print(A.shape) # (2, 3)
print(A.T.shape) # (3, 2)
print(A.T)np.linalg.inv and np.linalg.det
Compute the matrix inverse and determinant.
import numpy as np
A = np.array([[2.,1.],[1.,3.]])
print(np.linalg.inv(A))
# [[ 0.6 -0.2]
# [-0.2 0.4]]
print(np.linalg.det(A)) # 5.0Solving Linear Systems
np.linalg.solve(A, b) solves Ax = b efficiently using LU decomposition.
import numpy as np
# Solve: 2x + y = 5; x + 3y = 10
A = np.array([[2.,1.],[1.,3.]])
b = np.array([5.,10.])
x = np.linalg.solve(A, b)
print(x) # [1. 3.] → x=1, y=3Eigenvalues and Eigenvectors
np.linalg.eig(A) returns eigenvalues and eigenvectors. np.linalg.eigh for symmetric matrices (faster, more stable).
import numpy as np
A = np.array([[4., 2.], [1., 3.]])
vals, vecs = np.linalg.eig(A)
print("Eigenvalues:", vals) # [5. 2.]
print("Eigenvectors:\n", vecs)SVD: Singular Value Decomposition
np.linalg.svd(A) decomposes A into U, Sigma, Vt. Used for dimensionality reduction (PCA) and pseudo-inverses.
import numpy as np
A = np.array([[1,2],[3,4],[5,6]], dtype=float)
U, s, Vt = np.linalg.svd(A, full_matrices=False)
print("Singular values:", s)
# Reconstruct: U @ np.diag(s) @ Vt ≈ Anp.linalg.norm
Compute vector and matrix norms.
import numpy as np
v = np.array([3., 4.])
print(np.linalg.norm(v)) # 5.0 (Euclidean)
print(np.linalg.norm(v, ord=1)) # 7.0 (Manhattan)
A = np.eye(3)
print(np.linalg.norm(A, "fro")) # 1.732 (Frobenius)Cross and Outer Products
np.cross(a, b) for 3-D cross product. np.outer(a, b) for outer product.
import numpy as np
a = np.array([1,0,0])
b = np.array([0,1,0])
print(np.cross(a, b)) # [0 0 1]
print(np.outer([1,2,3], [4,5]))
# [[ 4 5]
# [ 8 10]
# [12 15]]np.linalg.lstsq
Least-squares solution for overdetermined systems (more equations than unknowns).
import numpy as np
# Fit y = ax + b to noisy data
X = np.column_stack([np.arange(5), np.ones(5)])
y = np.array([1.1, 2.0, 3.1, 3.9, 5.2])
coeffs, _, _, _ = np.linalg.lstsq(X, y, rcond=None)
print(f"slope={coeffs[0]:.2f}, intercept={coeffs[1]:.2f}")np.einsum
np.einsum expresses any tensor contraction using Einstein summation notation — extremely flexible for complex linear algebra.
import numpy as np
A = np.random.rand(3, 4)
B = np.random.rand(4, 5)
# Matrix multiplication via einsum:
C = np.einsum("ij,jk->ik", A, B)
print(C.shape) # (3, 5)
# Trace:
M = np.eye(4)
print(np.einsum("ii->", M)) # 4.0Quick Check
What operator performs matrix multiplication in NumPy (Python 3.5+)?
Recap
Use @ for matrix multiplication, np.linalg.solve for linear systems, np.linalg.eig for eigenvalues, and np.linalg.svd for decomposition. np.einsum handles arbitrary tensor contractions.
Frequently asked questions
Is the “Linear Algebra with NumPy” lesson free?
Yes — the full text of “Linear Algebra with NumPy” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “Linear Algebra with NumPy”?
Perform matrix multiplication, decompositions, and solving linear systems. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Linear Algebra with NumPy” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- NumPy Arrays and dtypes
- Array Operations and Broadcasting
- Indexing, Slicing, and Fancy Indexing
- Linear Algebra with NumPy