Home Knowledge Base NumPy (Numerical Python)

NumPy (Numerical Python) is the foundational library for high-performance numerical computation in Python that provides an N-dimensional array object (ndarray) with vectorized operations executing in optimized C code — the bedrock upon which PyTorch, TensorFlow, Pandas, Scikit-Learn, and virtually every Python AI library is built.

What Is NumPy?

Why NumPy Matters for AI

Core NumPy Concepts

ndarray Properties: import numpy as np a = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) a.shape # (2, 3) — dimensions a.dtype # float32 — element type a.strides # (12, 4) — bytes to step along each dimension a.nbytes # 24 — total bytes in memory

Vectorization (Replace Loops):

Slow Python loop:

result = [x**2 + 2*x + 1 for x in data] # Millions of Python object operations

Fast NumPy (vectorized C):

result = data**2 + 2*data + 1 # Single C loop over contiguous memory

Broadcasting: NumPy automatically expands array dimensions to make shapes compatible: A = np.ones((4, 1)) # shape (4, 1) B = np.ones((1, 3)) # shape (1, 3) C = A + B # shape (4, 3) — no data copied, virtual expansion

Essential for: applying a bias vector (1, D) to a batch of activations (N, D).

Essential Operations for AI

OperationNumPy CodeUse Case
Matrix multiplynp.matmul(A, B) or A @ BLinear layers, attention
Dot productnp.dot(a, b)Similarity computation
Normalizea / np.linalg.norm(a, axis=-1, keepdims=True)Embedding normalization
Softmaxnp.exp(x) / np.sum(np.exp(x), axis=-1)Attention weights
Argmaxnp.argmax(logits, axis=-1)Classification prediction
Concatenatenp.concatenate([a, b], axis=0)Batch assembly
Reshapea.reshape(N, -1)Flatten for linear layer
Boolean maska[a > threshold]Filtering predictions

Memory Layout and Performance

C-contiguous (row-major): Default NumPy layout — rows stored contiguously in memory. Row operations are cache-efficient; column operations cause cache misses.

Fortran-contiguous (column-major): Columns stored contiguously. Used by LAPACK routines — operations on columns are cache-efficient.

Views vs Copies: Many NumPy operations return views (slices, transpose, reshape) — zero-copy operations that share underlying data. Modifying a view modifies the original. Use .copy() when you need independence.

NumPy and PyTorch Interoperability

NumPy → PyTorch (zero-copy if array is C-contiguous)

tensor = torch.from_numpy(numpy_array)

PyTorch → NumPy (zero-copy if tensor is on CPU and contiguous)

numpy_array = tensor.numpy()

Both share memory — modifying one modifies the other!

Use .copy() for independence:

numpy_array = tensor.detach().cpu().numpy().copy()

NumPy is the universal substrate of scientific Python computing — its efficient array abstraction and vectorized operations are the reason Python became the dominant language for AI and data science despite being an interpreted language, enabling researchers and engineers to write readable, high-level code that executes with near-C performance.

numpyvectorizationarray

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.