Skip to content
Python for Data Science
Python for Data 8 min read

NumPy: Fast Arrays

Meet the array — NumPy's fast, vectorized alternative to Python lists — and learn creation, indexing, broadcasting, aggregation, and random numbers.

Download notebook Open Google ColabIn Colab: File → Upload notebook → pick the downloaded file.

Almost everything in data science — pandas, scikit-learn, even deep learning frameworks — is built on top of NumPy and its core data structure, the ndarray. In this lesson you'll see why arrays crush plain Python lists for numerical work, and learn the handful of operations you'll use every single day.

Why not just use lists?

Python lists are flexible, but that flexibility has a price. Watch what happens when you try to do math with them:

Python — runs in your browser

To actually multiply every element you'd need a loop or a list comprehension. NumPy arrays behave the way a mathematician expects — operations apply elementwise:

Python — runs in your browser

This style — operating on whole arrays at once instead of looping — is called vectorization, and it isn't just prettier, it's dramatically faster because the loop happens in optimized C code instead of Python. Let's measure it:

Python — runs in your browser

A 10–100x speedup is typical. On millions of rows, that's the difference between "instant" and "coffee break".

Creating arrays

You'll create arrays in a few standard ways — from lists, from constructors, or as evenly spaced sequences:

Python — runs in your browser

Note the difference between the last two: arange takes a step size (and excludes the stop), while linspace takes a count of points (and includes both endpoints). linspace is the go-to for plotting smooth curves.

Every array carries metadata describing itself:

Python — runs in your browser

Unlike lists, an array holds values of one dtype — that uniformity is exactly what makes it fast.

Indexing, slicing, and boolean masks

One-dimensional indexing works like lists, but 2-D arrays take a comma-separated [row, column] pair:

Python — runs in your browser

The real superpower is boolean masking — filtering with a condition instead of a loop:

Python — runs in your browser

Keep this pattern in mind — pandas filtering in the next lessons works exactly the same way.

Elementwise math and broadcasting

All the usual math works elementwise, and NumPy ships fast versions of common functions (np.sin, np.exp, np.sqrt, …):

Python — runs in your browser

Broadcasting is NumPy quietly repeating the smaller operand so shapes line up. It saves you from writing loops to, say, subtract a column mean from every row — a trick you'll use constantly when scaling features for machine learning.

Aggregations and the axis argument

Reducing an array to summary numbers is a one-liner:

Python — runs in your browser

Remembering axis

axis names the dimension that disappears. axis=0 collapses the rows (you get column summaries); axis=1 collapses the columns (you get row summaries). When in doubt, check the shape of the result.

Random numbers, the modern way

Simulations, synthetic datasets, and train/test splits all need randomness. The modern API is np.random.default_rng, which gives you a generator object — pass a seed to make results reproducible:

Python — runs in your browser

Seeding matters more than it looks: it makes your experiments reproducible, so a colleague (or future you) can rerun your notebook and get identical results.

Check your understanding

5 questions · free
  1. Q1.What does [1, 2, 3] * 2 return in plain Python?

  2. Q2.Why is a vectorized NumPy operation so much faster than a Python loop?

  3. Q3.np.linspace(0, 10, 5) returns…

  4. Q4.For a 2-D array m of monthly sales with shape (n_stores, n_months), how do you get the total per store?

  5. Q5.What is the purpose of the seed in np.random.default_rng(42)?

Exercise: Analyze a week of temperatures

Simulate one week (7 days) of daily temperatures for two cities as a (2, 7) array using np.random.default_rng — give Jakarta a mean around 31 °C and Bandung around 24 °C. Then compute: (1) each city's average temperature, (2) each city's hottest day, (3) how many days each city was above 30 °C, and (4) all readings above the overall average, using a boolean mask.

Next up: pandas — where NumPy arrays get labels, column names, and a whole toolkit for real-world tabular data.