Stop 03 · Linear Algebra & CalculusTopics · 9/9

Linear Algebra in NumPy

Part of the Machine Learning Roadmap

In short

Every model you train is matrix maths underneath. NumPy lets you write that maths almost exactly as it looks on paper, and it runs hundreds of times faster than Python loops.

NumPy turns the maths into fast code: @ for matrix multiplication, .T for transpose, np.linalg for inverses, norms, eigenvalues and solving systems. Writing the maths in NumPy is the bridge from paper to working models.

The jaadu feeds a long paper bill into a chunky billing machine at a kirana counter while stacks of goods wait in neat rows.

Think of it like this

The shop's calculator vs mental maths

A kirana owner can add a long bill in his head, one item at a time, but the billing machine adds every line in a flash. Python loops are the mental maths. NumPy is the billing machine: you hand it the whole table of numbers and one operation like @ multiplies everything at once.

Watch it happen

Linear regression predictions as one matrix multiply

X (rows x features)
w (weights)
X @ w
+ b
Predictions

1/5X is a matrix: 1,000 flats as rows, 3 features as columns. Shape (1000, 3).

The big ideas

What you need to know

Shapes come first

Check .shape before every operation. For A @ B, the inner sizes must match: (m, n) @ (n, k) gives (m, k).

@ is not *

A @ B is matrix multiplication. A * B multiplies element by element. Mixing them up is the most common bug.

Transpose with .T

X.T flips rows and columns. You will see X.T @ X everywhere in regression formulas.

np.linalg has the tools

solve, inv, norm, eig and svd live here. Prefer np.linalg.solve over inverting a matrix yourself.

See it in code

Walk through the code

python
1import numpy as np2 3X = np.array([[850, 2, 3],4              [1200, 3, 5],5              [600, 1, 1],6              [1500, 3, 8]], dtype=float)7y = np.array([22000, 31000, 15000, 40000], dtype=float)8 9Xb = np.c_[np.ones(len(X)), X]           # add a column of 1s for the bias10print(Xb.shape)                          # (4, 4)11 12# Normal equation: solve (X^T X) w = X^T y13w = np.linalg.solve(Xb.T @ Xb, Xb.T @ y)14 15preds = Xb @ w16print(np.round(preds))                   # matches y on this tiny data17vals, vecs = np.linalg.eig(Xb.T @ Xb)    # eigenvalues, used later in PCA

1/5X is a 4x3 matrix of flats. y is the vector of rents.

Fitting linear regression with pure NumPy using the normal equation.

Save yourself the pain

Common mistakes

Oops

Writing for loops over rows to compute predictions.

Do this instead

Use X @ w. Vectorised code is shorter, clearer and far faster.

Oops

Using np.linalg.inv(A) @ b to solve a system.

Do this instead

Use np.linalg.solve(A, b). It avoids building the inverse and is more numerically stable.

Oops

Ignoring a shape of (n,) vs (n, 1).

Do this instead

A 1-D array and a column vector broadcast differently. Print shapes and reshape on purpose.

Your mission

Regression without scikit-learn

~30 min

Quick check

A has shape (5, 3) and B has shape (3, 2). What is the shape of A @ B?

Go deeper

  1. 1Solve a small system of linear equations with np.linalg.solve.
  2. 2Compute predictions for 100 rows at once with X @ w instead of a loop, and time both.
  3. 3Check every array's .shape before multiplying to catch mistakes early.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge