Module 2 Lesson: NumPy Foundations for Data Work
Let us begin
NumPy is the first technical library in this course because it teaches array thinking.
An array is like a clean numeric grid. It can be one row of numbers, a table of numbers, or a higher-dimensional structure. In everyday analysis, you will mostly use one-dimensional and two-dimensional arrays.
pandas is built on top of ideas that NumPy makes clear: shape, dtype, vectorized operations, and axis-based summaries.
Lists are useful, but arrays are better for numeric work
A Python list can hold many kinds of values:
values = [10, 20, 30]
That is useful. But if we want fast numeric operations on many values, NumPy gives us a cleaner tool.
import numpy as np
values = np.array([10, 20, 30])
values + 5
Result:
array([15, 25, 35])
The operation touches every value without a manual loop.
Array shape
Shape tells us the structure.
scores = np.array([
[80, 75, 90],
[60, 70, 65],
[95, 92, 88],
])
scores.shape
Result:
(3, 3)
This means three rows and three columns.
Common attributes:
scores.ndim # number of dimensions
scores.size # total number of values
scores.dtype # value type
dtype
The dtype is the type of values inside the array.
Examples:
- int64 for whole numbers;
- float64 for decimal numbers;
- bool for True or False.
NumPy arrays usually work best when values are the same type. This is one reason they are efficient.
Indexing and slicing
NumPy uses zero-based indexing.
scores[0, 0]
This selects the first row and first column.
scores[0, :]
This selects all columns from the first row.
scores[:, 1]
This selects the second column for every row.
Read slices as:
> Which rows, then which columns?
Boolean masks
A mask is a True or False rule.
scores >= 80
This creates a boolean array.
scores[scores >= 80]
This selects values that pass the rule.
Masks are important because pandas filtering uses the same idea.
Vectorized operations
Vectorized operations apply a calculation to many values at once.
Manual style:
adjusted = []
for value in [80, 75, 90]:
adjusted.append(value + 5)
NumPy style:
scores_row = np.array([80, 75, 90])
adjusted = scores_row + 5
The NumPy version is shorter, clearer, and closer to how analysis is written with pandas.
Aggregation and axes
Aggregation means summarizing many values into fewer values.
scores.mean()
This gives the overall mean.
scores.mean(axis=0)
This summarizes down the rows and returns one value per column.
scores.mean(axis=1)
This summarizes across columns and returns one value per row.
Axis can feel strange at first. Use plain language:
axis=0: summarize each column;axis=1: summarize each row.
Broadcasting
Broadcasting lets NumPy combine arrays with compatible shapes.
scores + np.array([1, 2, 3])
Here NumPy adds 1 to the first column, 2 to the second column, and 3 to the third column.
Broadcasting is powerful, but do not guess silently. Always check shape.
Small simulation
Simulation helps us ask "what if" questions.
rng = np.random.default_rng(seed=42)
practice_minutes = rng.normal(loc=45, scale=10, size=100)
practice_minutes.mean()
The seed makes the random values repeatable. That matters for notebooks.
Takeaway
NumPy teaches the numeric foundation: arrays have shape and dtype; selections can use positions or masks; operations can be vectorized; summaries often depend on axis. In the next module, pandas adds labels, columns, and file import around these ideas.
