Every photo, song and sentence becomes numbers before AI can use it. Today we learn the Python tool that holds and crunches those numbers.
→ / Space: next · ←: back · R: restart slide · Home: restart presentation · P: draw · B: whiteboard · F: full screen
Ask the class first. Then click a card to show the answer.
By the end of today: you can create, slice, reshape and do maths on NumPy arrays without writing a single loop.
Each tiny square is a number: 0 is black, 255 is white.
The wave is measured 44,100 times every second.
ChatGPT turns every word into a number before it reads it.
| Maths | CS | |
|---|---|---|
| Aarav | 78 | 91 |
| Diya | 45 | 72 |
| Kabir | 92 | 95 |
Rows and columns of numbers. Most company data looks like this.
NumPy is the Python library that stores these grids and does maths on them, fast. Pandas, scikit-learn, TensorFlow and PyTorch all work with NumPy arrays.
Same answer, about 50× faster. And the NumPy code is one short line. When a model trains on millions of numbers, this speed decides whether it takes minutes or days.
A list stores addresses. Python must follow each arrow and check each item's type, one at a time. Items can even be mixed: [85, "hi", 3.5]
All numbers sit side by side and have the same type. The CPU can work on the whole block at once, with fast code written in C.
Vectorization = one instruction for the whole array, instead of a Python loop. In this class we write marks + 5, never a for loop.
Guess each answer before you click ▶.
A list of lists becomes a 2D array: rows = students, columns = subjects (Maths, Physics, Chem, CS). We will use this table all class.
marks.shape(5, 4)5 rows, 4 columns. The most useful line in NumPy. Check it often.
marks.ndim2Number of dimensions. A list is 1D, a table is 2D, a colour photo is 3D.
marks.dtypeint64Type of every item. Whole numbers here. With decimals it becomes float64.
marks.size20Total number of items: 5 × 4 = 20.
arange or linspace?Slice rule: t[start:stop:step]. Start is included, stop is not. Counting starts at 0. Negative numbers count from the end.
m[row, column]: on its own means "all". So m[:, 0] = all rows, column 0 = everyone's Maths.
In AI: this is how we pick rows of data, like "only fraud payments" or "only emails marked spam", in one line.
The one rule: the new shape must hold the same number of items. 12 = 3 × 4 = 2 × 6 = 2 × 2 × 3. Write -1 and NumPy works out that number for you.
3 numbers per pixel: red, green, blue.
Your phone photo is an array of shape (3000, 4000, 3): 36 million numbers. This is exactly Exercise Q4 and Q5 today.
Some models want each example as one row of numbers.
img.flatten() # (8,8) → (64,)A famous dataset has photos of digits 0–9, each 28 × 28 pixels.
28 × 28 = 784 numbers100 photos together have shape (100, 28, 28).
imgs.reshape(100, -1)100 rows, 784 columns: one row per photo. -1 worked out the 784.
→ (100, 784)Grace marks per subject: +5 Maths, 0 Physics, +3 Chem. The row is copied down to every student.
Dashed boxes are not really copied. NumPy pretends, so it uses no extra memory.
Rule: line the shapes up from the right. Each pair must be equal, or one of them must be 1. A missing number counts as 1.
The column is copied across to all 4 subjects.
Remember: axis=0 goes down → one answer per column (subject). axis=1 goes across → one answer per row (student).
probs.max()0.8The biggest value. Good, but it does not tell us which label.
probs.argmax()1The position of the biggest value.
labels[probs.argmax()]'dog'Same trick for our class: names[marks[:, 3].argmax()] → 'Kabir', the CS topper.
std (standard deviation) = how far marks usually are from the average. Small std: everyone is close. Big std: marks are spread out. In Module 2 we use mean and std to scale data before training.
Row 0 (blue) becomes column 0. Use .T when two shapes do not line up for matrix multiplication.
One line: np.dot(qty, price) or qty @ price → 245. Multiply the pairs, then add. A neuron in a neural network does exactly this.
Click any orange answer box to see how it was made.
* and @ are not the sameEach box times the box in the same place. Shapes must match (or broadcast).
Row times column, then add. Top-left: 1×10 + 2×30 = 70.
Same inputs, different answers. If your model gives strange numbers, check whether you meant * or @.
4 emails, 3 numbers each: links, CAPITAL words, says "FREE".
reshape · shape (4, 3)Weights turn each email into 2 scores: not spam, spam.
matrix multiplicationA bias of shape (2,) is added to every row of (4, 2).
broadcastingFor each email, pick the bigger score.
statistics · axis=1Q10 today is this exact spam filter. In Module 2, a model will learn W and b by itself.
When you come back: open Colab and the class notebook link from the group.
Class group link → File → Save a copy in Drive
It loads NumPy and the marks table. Wait for the green tick ✓
Write your answer in the 💻 cell, then run the ✅ check cell below it
The check message tells you what to fix
Read the hint in the question. Still stuck: raise your hand
We solve the questions together, a few at a time. You try first, then I show the answer on the screen.
Finished early?Help your neighbour. Point, but do not type for them. Then try the Bonus at the end.
Submit: a screenshot showing all 10 green ✅ ticks, plus the link to your notebook.
Fix: print both .shapes. Line them up from the right. Often you need .reshape(5, 1).
Fix: rows × columns must equal the size. 8 × 8 = 64. Or use -1.
Fix: 5 rows have positions 0 to 4. The last one is [-1].
Fix: run cells top to bottom. Or Runtime → Run all.
A class has 30 students and 5 subjects. What is the shape of marks.mean(axis=0)? Why?
Write the answer on paper or post it in the class group.
Today you created arrays, sliced them, reshaped a flat row into a photo, used broadcasting, found statistics along an axis and multiplied matrices, all without a single loop.