liveBook · Manning

Vectorization

As explained in Fast Python: High performance techniques for large datasets

Vectorization is the representation of data or computations as vectors or arrays so they can be processed numerically or collectively.

Vectorization expresses work over an array or column instead of writing a Python loop for each value. In NumPy, array operations and broadcasting normally use vectorized implementations; they can be orders of magnitude faster than element-by-element Python code. The useful question is not just whether code looks array-oriented, but how it executes. section 4.2.2 section 4.2.4

Choose the operation that matches both the computation and the data shape:

  • Use NumPy array operations for elementwise work. Broadcasting applies an operation across compatible shapes without excessive copying; transposing can make a row-oriented operation fit the available broadcasting pattern through a fast view. section 4.2.2
  • Do not assume np.vectorize makes a scalar Python function fast. It applies the function element by element and is essentially a loop: an image-brightening example takes milliseconds with np.vectorize but microseconds with multiplication. The scalar function prevents overflow, however, so replacing it requires preserving that behavior. section 4.2.4
  • In pandas, prefer column operations to row iteration when the calculation permits it. For a trip-data example, dividing the tip column by the total column expresses a per-trip fraction without traversing rows in Python. section 7.2.2 section 7.6

Check the result before comparing speed

A row-loop baseline accumulates all tips and totals, then divides their sums. The vectorized pandas expression divides each trip’s tip by its total and takes the mean. Those are different calculations: first decide which statistic is wanted, then benchmark equivalent implementations. The example excludes zero totals before division and uses sampled data frames for performance testing. section 7.2.2

def get_tip_mean_vector(df):
    frac_tip = df["tip_amount"] / df["total_amount"]
    return frac_tip.mean()

A compact pandas column expression for the mean of per-trip tip fractions. section 7.2.2

Vectorized thinking also guides parallel implementations. A CUDA C Mandelbrot kernel accepts an array of positions and computes one value per position, treating the matrix as one-dimensional for thread indexing. In the reported benchmark, its roughly 3.1 ms GPU time is 20 times faster than the compared Numba version; preparing the positions and converting between NumPy and CuPy involve separate work and a transfer. section 9.3.5

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Works through broadcasting, np.vectorize, pandas alternatives, and a GPU kernel; does not delve into the CUDA C listing.
Examples
Compares image brightening methods, tip-fraction calculations on trip data, and a CUDA C Mandelbrot generator.
What it assumes
Assumes familiarity with Python functions and array or data-frame operations; develops vectorized thinking before Cython and GPU implementations.
What you can do after
Apply it via NumPy and pandas operations, checking calculation semantics and timing before treating vectorized syntax as a speedup.
How it connects
Connects broadcasting and universal functions to pandas column operations, Cython, CPU vectorization, and GPU processing.

Also explained in

Compare how 18 books explain vectorization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.