NumPy: Why Fast Arrays Matter
A small problem with Python lists
Say a teacher has the marks of five students, and the board announces five grace marks for everyone. With a normal Python list, the obvious thing to try does not work:
1marks_list = [72, 85, 90, 64, 78]
2marks_list + 5TypeError: can only concatenate list (not "int") to list
And multiplying does something you probably did not want:
1marks_list * 2[72, 85, 90, 64, 78, 72, 85, 90, 64, 78]
It copied the list twice instead of doubling each mark. Python lists are built to hold things, not to do maths on them. To add 5 to every mark you would need a loop. For 5 marks that is fine. For the 10 million numbers in lesson 2.1, a loop was over 20 times slower than NumPy.
NumPy fixes both problems at once. It gives Python a new kind of list, built for maths and built for speed.
The NumPy array
The core of NumPy is the array. It looks a lot like a list, but maths works on the whole thing at once:
1import numpy as np
2
3marks = np.array([72, 85, 90, 64, 78])
4marks + 5array([77, 90, 95, 69, 83])
1marks * 2array([144, 170, 180, 128, 156])
Every mark got 5 added, then every mark got doubled. No loop. This is called a vectorised operation: you write the maths once, and NumPy applies it to every element.
Tip:Everyone writes import numpy as np. The np is just a short nickname, and it is so universal that you will see np. in nearly every ML notebook in the world.
Two arrays of the same length can be combined element by element too. Say theory is out of 70 and practical is out of 30:
1theory = np.array([55, 62, 68, 48, 60])
2practical = np.array([25, 28, 27, 22, 24])
3theory + practicalarray([80, 90, 95, 70, 84])
The first student's theory is added to the first student's practical, the second to the second, and so on.
Why arrays are so fast
A Python list is flexible. It can hold a number, a string and another list side by side. To allow that, Python stores each item separately and keeps a note of where each one lives. When you loop, Python has to go and find each item and check what type it is, one at a time.
A NumPy array makes a deal: every element must be the same type, like all whole numbers. In return, the numbers are packed tightly side by side in memory, like eggs in a tray. The fast C code underneath can then zip through them without checking anything. That is the engine from lesson 2.1 at work.
Quick answers about your data
Arrays come with built-in functions for the questions you ask all the time:
1print(marks.sum()) # total
2print(marks.mean()) # average
3print(marks.max()) # highest
4print(marks.min()) # lowest389 77.8 90 64
Want to know who topped? argmax gives the position of the highest value, which you can use to look up the name:
1names = ["Aarav", "Diya", "Kabir", "Meera", "Rohan"]
2print(names[marks.argmax()])Kabir
Tip:If you leave out print() and let the notebook show a single number on its own, you may see np.float64(77.8) instead of 77.8. It is the same number. NumPy is just telling you what type it is. Using print() shows the plain value.
Asking questions with filters
This is one of the most useful tricks in all of ML code. Comparing an array with a number gives you a True or False for every element:
1marks > 75array([False, True, True, False, True])
Now put that inside square brackets, and NumPy keeps only the elements where the answer was True:
1marks[marks > 75]array([85, 90, 78])
Read it as "marks, where marks is above 75". And since True counts as 1 and False as 0, adding them up counts how many passed the test:
1print((marks > 75).sum())3
This True/False list is called a boolean mask, because it hides some elements and shows others. You will use it constantly: keep only flats under ₹20,000, drop rows where the value is missing, count how many predictions were wrong.
Tables: two-dimensional arrays
So far each array was a single row of numbers. But ML data is usually a table, like the flats table in lesson 1.6. NumPy handles tables too. Here are five students and their marks in three subjects:
1# maths science english
2scores = np.array([[72, 65, 80], # Aarav
3 [85, 90, 78], # Diya
4 [90, 88, 95], # Kabir
5 [64, 70, 60], # Meera
6 [78, 82, 74]]) # Rohan
7scores.shape(5, 3)
shape tells you the size: 5 rows and 3 columns. You will check shape all the time, because many ML errors come down to data not being the shape you thought it was.
To pick things out, you give a row and a column, in that order. Counting starts from 0:
1print(scores[2, 0]) # row 2 (Kabir), column 0 (maths)
2print(scores[2]) # all of row 2: Kabir's marks
3print(scores[:, 0]) # every row, column 0: all the maths marks90 [90 88 95] [72 85 90 64 78]
The colon on its own means "all of them". So scores[:, 0] reads as "all rows, column 0", which is the whole maths column.
The axis question
Now for the one idea that confuses almost everyone at first. If you ask a table for its average, which average do you mean? The average of each subject, or the average of each student? You choose with axis:
1print(scores.mean(axis=0)) # average of each subject
2print(scores.mean(axis=1).round(1)) # average of each student[77.8 79. 77.4] [72.3 84.3 91. 64.7 78. ]
| maths | science | english | axis=1 → | |
|---|---|---|---|---|
| Aarav | 72 | 65 | 80 | 72.3 |
| Diya | 85 | 90 | 78 | 84.3 |
| Kabir | 90 | 88 | 95 | 91.0 |
| Meera | 64 | 70 | 60 | 64.7 |
| Rohan | 78 | 82 | 74 | 78.0 |
| axis=0 ↓ | 77.8 | 79.0 | 77.4 |
A way to remember it: axis=0 squashes the rows together, so you are left with one number per column. axis=1 squashes the columns together, so you are left with one number per row. If you ever forget, check the shape of the answer. Three numbers means one per subject. Five means one per student.
How this connects to ML
You now know the shape of almost every ML dataset. In lesson 1.6, X held the features and y held the target. Underneath, they are NumPy arrays:
Key Points
- X is a 2D array: one row per example, one column per feature. Like scores above.
- y is a 1D array: one answer per example. Like marks.
- The learned numbers in model.coef_ from lesson 1.6 were a NumPy array too.
pandas, which you meet next, is built on NumPy. So is scikit-learn. Most of the time you will not call NumPy directly, but it is running under almost every line of ML code you write.
Where people get confused
Key Points
- "Lists and arrays are the same thing." They look similar but behave differently. [1, 2] * 2 gives [1, 2, 1, 2]. np.array([1, 2]) * 2 gives [2, 4].
- "axis=0 means rows, so it gives one answer per row." It is the opposite. axis=0 works down the rows and gives one answer per column.
- "I can mix numbers and text in an array." You can try, but NumPy will quietly turn everything into text. np.array([1, "two", 3]) becomes three strings, and maths on it will fail.
- "Indexing starts at 1." In Python and NumPy it starts at 0. The first student is scores[0].
Quick check
prices = np.array([100, 250, 80]). What does prices * 1.18 give you, and what might it mean?
array([118. , 295. , 94.4]). Every price multiplied by 1.18, which is each price with 18% GST added. One line, no loop.
How would you get only the rents below 15,000 from an array called rents?
rents[rents < 15000]. The comparison makes a True/False mask, and the square brackets keep only the True ones.
A table has 100 rows (flats) and 4 columns (features). What shape does table.mean(axis=0) have?
Four numbers, one per column. axis=0 squashes the 100 rows together, leaving the average of each feature.
Recap
Key Points
- A NumPy array is a list built for maths: every element is the same type, packed tightly for speed.
- Maths on an array applies to every element at once. No loops needed.
- Comparisons make True/False masks, and arr[mask] keeps only the matching elements.
- 2D arrays are tables. shape gives rows and columns. arr[row, col] picks elements, and : means "all".
- axis=0 gives one answer per column, axis=1 gives one answer per row.
Next up: pandas. NumPy tables only know positions like "column 0". pandas gives columns real names like "rent", and makes working with real data files far easier.
Theia
Can you explain this concept?
Practise NumPy: Why Fast Arrays Matter with realistic interview questions. Choose spoken or typed answers, get feedback on your reasoning, then return to the material that needs work.
Sign in to practise this conceptThe session stays focused on this lesson. Your selected interview level sets the bar.