liveBook · Manning

NumPy

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

NumPy is an open-source Python library for numerical computing that provides multidimensional arrays and operations on them.

NumPy provides multidimensional arrays and operations on them. Its C- and Fortran-based core makes it a practical foundation for numerical work in Python, including data-processing libraries such as pandas. An image, for example, can be treated as a two-dimensional array; the performance question is how to manipulate it without unnecessary work or allocation. section 4.0 section 4.1

Three choices have a large effect on array workloads:

  • Prefer views over copies when sharing the original data is acceptable. A view changes the perspective on existing data with little computing or memory cost; its shape gives dimension sizes, while its strides give byte steps between elements. section 4.4
  • Express work as operations on whole arrays rather than element-by-element loops. Broadcasting supports operations across arrays of different dimensions. section 4.4
  • Check the numerical libraries behind the installation. BLAS and LAPACK choices affect performance, and their threading behavior matters before adding Python multiprocessing around NumPy calls. section 4.3.2 section 4.4

Moving a pandas calculation onto arrays

Extract columns with `to_numpy()`, then perform the calculation on the resulting arrays. In one measured tip-analysis workload, vectorized NumPy code took 11 ms versus 35 ms for vectorized pandas code on the same computer. The default conversion can share underlying data with the pandas object; `to_numpy(copy=True)` avoids changes reaching the original but adds copying time and memory use. Benchmark the actual workload rather than assuming the same gain elsewhere. section 7.3.1 section 7.3.3

When array operations are not enough, NumPy arrays can also supply data to Cython code. That route, and alternatives such as NumExpr, should follow measurement: their relative results depend on the algorithm, data, software and hardware. section 7.3.3 section 5.1

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

How deep it goes
Explains views, shape, strides, broadcasting, library configuration and threading; assumes prior NumPy exposure rather than teaching a full introduction.
Examples
Uses image-manipulation routines and a benchmarked pandas tip calculation converted to NumPy arrays.
What it assumes
Assumes some NumPy exposure and teaches its performance implications through practical array workloads.
What you can do after
Apply it via NumPy and pandas to reduce copying, use whole-array operations and benchmark array-based calculations.
How it connects
Connects NumPy to pandas, Cython, NumExpr, broadcasting, BLAS and LAPACK.

Also explained in

Compare how 7 books explain NumPy →

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.