NumPy
Beginner 12 min read NumPy

NumPy Arrays

The engine under pandas, made explicit

NumPy arrays are typed, dense, vectorized collections — the engine under pandas. Understanding arrays makes the whole data stack faster to reason about and to tune.

ndarraydtypeshapereshapevectorizationarray
Prerequisites: Python for Data Work
Before we start

What you will learn

  • Build arrays and control their dtype and shape
  • Create common arrays (zeros, ones, arange, linspace)
  • Reshape and broadcast-compatible views
Introduction

What is this?

A NumPy ndarray is a fixed-type, contiguous grid of numbers. Unlike Python lists, every element shares one dtype, so operations run in C-level loops with no type checks. Shape tuples and dtypes define the array; reshape, arange, linspace and zeros build them from spec.

Motivation

Why do you need it?

  • Pandas DataFrames are built on numpy arrays — mastery pays double.
  • Vectorized math is 10-100x faster than Python loops.
  • dtype control (float64 vs int32) drives memory and precision decisions.
  • Machine learning and stats models consume numpy arrays directly.
Think about it

Mental model

Imagine

A pallet of identical, labeled crates

Think of

A NumPy array

Every crate is the same kind (dtype) and arranged in a known grid (shape) so the loader can move the whole pallet at once without inspecting each crate — that's why operations are so fast.

Mechanics

How it works

  • np.array([...]) converts a list; the common dtype is inferred unless you pass dtype=.
  • np.zeros(shape), np.ones, np.arange(start, stop, step), np.linspace(a, b, n) build blanks fast.
  • arr.shape, arr.dtype, arr.size, arr.ndim describe any array.
  • arr.reshape(new_shape), .ravel()/flatten() and .transpose() produce views or copies.
  • Slicing arr[1:5, ::2] is cheap and shares memory — views, not copies.

From spec to array to operation.

Shape
dtype
Construct
Operate
Result
Hands-on

Examples

Build, inspect, reshape

Build, inspect, reshape
import numpy as np
grid = np.arange(12).reshape(3, 4)
print(grid.shape, grid.dtype)
print(grid[1, :]) # row 1
print(grid.sum(), grid.mean(axis=0))

arange supplies 0..11, reshape folds it into 3×4, and axis=0 summarizes down columns — all vectorized.

Real world

In production

  • A latency monitor buckets millions of samples into histograms with np.histogram in one call.
  • A pricing engine vectorizes discount math over every SKU rather than looping price-by-price.
  • A feature store serializes arrays in fixed-dtype blobs, halving storage.
Guidelines

Best practices

  • Check dtype on every build; surprise int/float boundaries change results.
  • Prefer np.arange/linspace over list building for numeric sequences.
  • Reshape when you create, not in hot loops — views are cheap, recompute is not.
  • Aggregate with axis to keep results readable rather than anonymous ravelled totals.
Watch out

Common mistakes

  • Building arrays from Python loops instead of np.arange/fromiter.
  • Assuming reshape copies data when it often shares a view — mutating surprises follow.
  • Mixing int and float arrays in operations and getting a promoted dtype you didn't expect.
  • Indexing mistakes producing '0-d arrays' that break indexing later.
When things break

Debugging tips

  • When math doesn't work as expected, print .dtype first — int truncation and promotion explain 90% of surprises.
  • A 'shape mismatch' error names the two shapes; find the transpose/reshape step.
  • Memory spikes → large dtype; switch to float32 or a view.
  • If slicing returns a view and later code mutates, use .copy() at the boundary.
Interviews

Interview checkpoint

Practice

Hands-on lab

Blast Through a Numeric Rebuild
25 min · beginner
In progress
Objectives
  • Construct grids with arange/linspace/zeros
  • Inspect shape, dtype and axis behavior
  • Vectorize a comparison of list vs array speed

Blast Through a Numeric Rebuild

25 min · beginner

0/4 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

np.arange(5) produces:

2

All elements of a NumPy array share the same dtype.

Go deeper

Official documentation