Quick Stats
Completed
0
Time Spent
0m
Streak
0
User
Arrays with NumPy
Fast numerical computing.
Why arrays
AI is math on big grids of numbers — an image is a grid of pixel values, a dataset is a grid of rows and features, a model's weights are grids. Plain Python lists are too slow and clumsy for this. NumPy gives you the ndarray, a fast, compact array built for exactly this kind of numeric work, and it's the foundation every other AI library is built on.
import numpy as np
x = np.array([1, 2, 3, 4])
print(x.mean()) # 2.5
print(x * 2) # [2 4 6 8] — whole array at once
print(x[x > 2]) # [3 4] — boolean filteringVectorization: the big idea
Notice what x * 2 did: it multiplied every element without a loop. This is vectorization — expressing an operation over a whole array at once instead of element by element. It's both faster (the work happens in optimized C under the hood) and clearer (one line instead of a loop). Internally, every ML framework — PyTorch, TensorFlow, scikit-learn — runs on vectorized array math.
What you'll actually use
- Creating arrays —
np.array([...]),np.zeros((3, 3)),np.arange(10). - Element-wise math —
+ - * /across whole arrays. - Aggregations —
.mean(),.sum(),.max(),.std(). - Boolean indexing —
x[x > 2]to filter, the basis of data cleaning. - Shape —
x.shapetells you the dimensions, which you'll check constantly.
Why bother before pandas
Pandas (next up) is built on NumPy, and so are the tensors in deep learning frameworks. Understanding arrays and vectorization means the rest of the AI stack stops feeling like magic — it's arrays all the way down.
Stop thinking in loops and start thinking in arrays. "Do this to every number at once" is the mental shift that makes both NumPy and every ML framework click.
Try this: Make an array of ten numbers and, in one line each, compute their mean, keep only the values above the mean, and double the whole array. Doing it without a single for loop is your first taste of thinking in vectors.