NumPy
Beginner 12 min read NumPy

NumPy Introduction

The array engine under pandas, sklearn and torch

NumPy's ndarray is a fast, homogeneous, n-dimensional array with vectorized math — the primitive everything from pandas to deep learning is built on.

ndarraynumpyvectorizationarraydtypeC extension
Before we start

What you will learn

  • Create and inspect ndarrays
  • Explain why array ops beat Python loops
  • Relate ndarray to later tools (pandas, torch)
Introduction

What is this?

NumPy provides the ndarray: a homogeneous, fixed-size, n-dimensional array whose operations are implemented in optimized C and can run over every element without Python-level looping. This 'vectorization' is why the scientific Python stack is fast, and it's the storage core pandas wraps into Series/DataFrames.

Motivation

Why do you need it?

  • Pure Python loops on 10M numbers take seconds; the ndarray does it in milliseconds.
  • pandas columns ARE numpy arrays; pandas speed inherits numpy speed.
  • Everything downstream (scikit-learn, TensorFlow/PyTorch) imports, converts or extends ndarrays.
  • One `arr.mean()` replaces a hand-written loop, count, sum and divide.
Think about it

Mental model

Imagine

A professional kitchen blender vs chopping by hand

Think of

Vectorized numpy vs Python loops

The blender processes a whole batch at once with one mechanism; hand-chopping processes one piece at a time. Vectorized ops are the blender.

Mechanics

How it works

  • The ndarray stores a homogeneous block of memory — every cell the same dtype, contiguously laid out.
  • Operations dispatch to compiled C loops (and optionally SIMD) that iterate the block without Python overhead.
  • Ufuncs (universal functions) like np.add, np.sqrt apply element-wise across the whole array.
  • Broadcasting lets differently-shaped compatible arrays combine element-wise.
  • This design scales to GPUs: torch/tensorflow mirror the same layout and semantics.

From Python list to fast ndarray.

Python list
np.array(list)
dtype
Ufuncs in C
pandas/sklearn
Hands-on

Examples

Loops vs vectorization

Loops vs vectorization
import numpy as np, time
arr = np.arange(10_000_000, dtype="float64") + 1
t0 = time.perf_counter()
total = (arr * 2).sum() # vectorized
t1 = time.perf_counter()
s = 0
for x in arr: s += x * 2 # python loop
t2 = time.perf_counter()
print(round(t1 - t0, 4), round(t2 - t1, 2)) # e.g. 0.008 vs 1.9

The arithmetic happens once per element in C for the whole 10-million-wide array. The loop pays per-element Python overhead. The gap grows with size — the prime reason vectorization is non-negotiable.

Real world

In production

  • A fraud pipeline scores 50M transactions against matrix weights in numpy before any Python abstraction touches them.
  • Embedding similarity in production is a numpy (or torch) matrix multiply over precomputed vectors.
  • Feature engineering libraries like featuretools and category encoders ship numpy kernels for the hot paths.
Guidelines

Best practices

  • Prefer array operations over explicit Python loops everywhere in hot code.
  • Choose the smallest dtype that fits (float32 vs float64 halves memory and speeds up).
  • Use np.arange/np.linspace/np.zeros instead of building lists then converting.
Watch out

Common mistakes

  • Calling np.array() on a pure-Python structure per row inside a loop (rebuild cost each time).
  • Ignoring dtype: float64 arrays are slower and heavier than float32 when precision allows.
  • Copying with np.array(arr) instead of np.asarray when reuse is safe.
When things break

Debugging tips

  • Print arr.dtype, arr.shape and arr.strides when behaviour looks wrong — they explain most mysteries.
  • Check performance with arr.nbytes: cast dtype/VT down to shrink memory and speed ops.
  • If results are unexpectedly rounded, inspect dtype, not the algorithm.
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/2
1

The ND in ndarray stands for:

2

pandas columns internally hold numpy arrays.

Go deeper

Official documentation