Library Overview03/06
Starting sandbox…
Beginner

NumPy

NumPy

Core Principle: NumPy provides a fixed-size, homogeneously-typed multidimensional array object (ndarray) alongside compiled, vectorized functions for high-performance numerical computation.

Version at time of writing: 2.5.x · import numpy as np · Python 3.12–3.14

Architectural Model

A NumPy array consists of a contiguous block of memory accompanied by three essential structural metadata descriptors: shape, dtype, and strides (the byte offset required to traverse consecutive elements along each dimension). Core NumPy operations, including reshaping, transposing, slicing, and broadcasting, execute either as metadata manipulations without memory duplication or as optimized, compiled C-loops over underlying buffers.

Once you hold that picture, the behaviours that feel arbitrary stop being arbitrary:

  • arr.T is instant on a 10 GB array because it only swaps strides (see Memory Layout and Strides)
  • arr[1:5] shares memory with arr but arr1,2,3 does not (see Views vs Copies)
  • adding a (3,1) array to a (1,4) array gives (3,4) without allocating either operand (see Broadcasting)
  • appending to an array is expensive, because "fixed-size" is not negotiable

Computational Relevance in Biology

Nearly every quantitative bio object is a matrix: an expression matrix (genes × samples), a distance matrix, a position weight matrix, a coverage vector, a genotype matrix. NumPy is where those live, and a per-element Python loop over a 20,000 × 500 matrix is ~10 million interpreter round-trips versus one compiled pass.

Biopython returns NumPy arrays in several places (Bio.motifs PWMs, Bio.PDB atom coordinates, alignment substitution counts), and pandas is NumPy underneath. You cannot skip this layer.

Core Reference Modules

The object ndarray · NumPy dtypes · Array Creation

Getting data out Indexing and Slicing · Boolean Masking · Fancy Indexing · Views vs Copies

Doing math Broadcasting · ufuncs · Vectorization · Axes and Reductions

Changing shape Reshaping and Stacking · Memory Layout and Strides

Specialised Structured Arrays · NumPy Random Generator · Linear Algebra with NumPy · Missing Data in NumPy · NumPy File IO

Applied Workflows

Applied - Expression Matrices with NumPy · Applied - Sequence Composition Analysis · Applied - Multiple Sequence Alignment Analysis

Installation and Verification

pip install "numpy>=2.4,<3"
python -c "import numpy as np; print(np.__version__); np.show_config()"

np.show_config() tells you which BLAS you are linked against, which is useful when diagnosing suboptimal matrix multiplication performance.

Essential API Patterns

[1]python
out1ms
array([[14, 32],
       [32, 77]])

Limitations and Alternatives

  • Labelled data → Pandas
  • Ragged / variable-length rows → lists, or an awkward-array style library
  • Mostly-zero matrices → scipy.sparse (single-cell count matrices are >90% zero; a dense NumPy array will exhaust your RAM)
  • Data larger than memory → Dask, Zarr, or chunked reading

Common Pitfalls & Behavioral Edge Cases

  • Integer arrays overflow silently. np.int8(127) + 1 == -128. Read counts in int32 can overflow on large studies.
  • arr == np.nan is always False. Use np.isnan. See Missing Data in NumPy.
  • Modifying a slice modifies the original. See Views vs Copies.
  • Floating-point equality: use np.isclose / np.allclose, never ==.
  • NumPy 2 changed scalar promotion rules (NEP 50); for example, np.float32(1) + 1.0 preserves float32 precision. See Versions and Compatibility.

Related References

Pandas · Ecosystem Map · Learning Path