NumPy and Arrays
What is NumPy?
NumPy (Numerical Python) is the foundation of the Python data stack. Pandas stores its columns as NumPy arrays, scikit-learn expects NumPy arrays as input, and deep learning libraries like PyTorch copy many of its ideas. When a data scientist at MTN crunches millions of call records or a researcher at NASA processes sensor readings, NumPy is doing the heavy lifting underneath.
The core object is the ndarray: a grid of values that all share the same type.
import numpy as np
prices = np.array([1520, 1535, 1548, 1561, 1550])
print(prices.shape) # (5,)
print(prices.dtype) # int64
Why Not Just Use Lists?
Two reasons: speed and vectorised maths.
# With a list you need a loop
naira = [185000, 92000, 143000]
usd = [n / 1550 for n in naira]
# With NumPy the operation applies to every element at once
naira = np.array([185000, 92000, 143000])
usd = naira / 1550
NumPy runs these operations in optimised C code. On a million values it is often 50 to 100 times faster than a Python loop. This style of applying one operation to a whole array is called vectorisation, and it is the habit that separates beginner and professional data code.
Creating Arrays
np.zeros(5) # [0. 0. 0. 0. 0.]
np.ones((2, 3)) # 2 rows, 3 columns of 1s
np.arange(0, 10, 2) # [0 2 4 6 8]
np.linspace(0, 1, 5) # [0. 0.25 0.5 0.75 1. ]
np.random.seed(42)
np.random.normal(50000, 10000, size=1000) # 1,000 simulated salaries
Setting a random seed makes your random numbers repeatable, so a colleague who runs your notebook gets the same results.
Element-wise Operations and Broadcasting
Arithmetic between arrays happens element by element:
jan = np.array([120, 98, 143])
feb = np.array([130, 105, 139])
growth = (feb - jan) / jan * 100
print(growth.round(1)) # [ 8.3 7.1 -2.8]
When you combine an array with a single number, NumPy broadcasts the number across every element. That is why naira / 1550 works. Broadcasting also works between arrays of compatible shapes, for example subtracting a row of column means from a whole 2D table.
Aggregations
sales = np.array([120, 98, 143, 110, 167])
sales.sum() # 638
sales.mean() # 127.6
sales.std() # standard deviation
sales.min(), sales.max()
sales.argmax() # 4 -> the index of the largest value
On 2D arrays, the axis argument controls direction: axis=0 aggregates down the rows (one result per column), and axis=1 aggregates across columns (one result per row).
# rows = branches, columns = months
branch_sales = np.array([[120, 130, 125],
[ 98, 105, 110],
[143, 139, 150]])
branch_sales.sum(axis=1) # total per branch: [375 313 432]
branch_sales.mean(axis=0) # average per month
Indexing, Slicing and Boolean Masks
sales[0] # first element
sales[1:4] # elements 1, 2 and 3
branch_sales[2, 1] # row 2, column 1 -> 139
branch_sales[:, 0] # the whole first column
Boolean masking is the most important indexing technique for data work. A comparison returns an array of True/False values, and you can use it to filter:
mask = sales > 115
print(mask) # [ True False True False True]
print(sales[mask]) # [120 143 167]
You will use exactly this idea in pandas when you write df[df["amount"] > 100000].
Reshaping
a = np.arange(12) # 12 values
a.reshape(3, 4) # 3 rows x 4 columns
a.reshape(-1, 1) # a single column; -1 means "work it out"
scikit-learn expects features as a 2D array, so reshape(-1, 1) appears often when you train a model on a single feature.
Tip: Use the array visualiser below to see what each operation does to the values, then try the same operations in a Colab notebook.
Try it yourself
Key Takeaways
- NumPy's ndarray is the numeric foundation that pandas, scikit-learn and most of the Python data stack are built on.
- Vectorised operations apply to a whole array at once and are dramatically faster than Python loops.
- Broadcasting lets you combine arrays with scalars or compatible shapes without writing loops.
- Boolean masks such as arr[arr > 100] are the core filtering technique, and pandas uses the same idea.
- The axis argument controls whether an aggregation runs down the rows (axis=0) or across the columns (axis=1).
Quick Quiz
1.What is vectorisation in NumPy?
2.Given sales = np.array([120, 98, 143]), what does sales[sales > 100] return?
3.For a 2D array where rows are branches and columns are months, what does arr.sum(axis=1) give you?
Ready to go further?
CareerEx gives you structured 12-week training, live classes every Saturday and Sunday, real tutor feedback, and a certificate. Join the next cohort.
Join CareerEx