🔢 NumPy — Map of Content

What is NumPy?

NumPy (Numerical Python) is the foundational library for numerical computing in Python. Its core object, the ndarray, is a fast, memory-efficient, homogeneously-typed N-dimensional array that supports vectorized operations, broadcasting, and linear algebra. pandas is built directly on top of it.

import numpy as np

Version note

This vault assumes NumPy ≥ 1.24. The legacy np.random global-state API still works but np.random.default_rng() (Generator API) is the modern recommended approach — both are covered in 07-Random-Module.


📂 Folder Contents

#NoteCovers
0101-Arrays-Basicsndarray creation, attributes, dtypes
0202-Indexing-SlicingBasic/fancy indexing, boolean masks, views vs copies
0303-Broadcasting-OperationsElement-wise ops, broadcasting rules, ufuncs
0404-Math-Statistical-Functionssum, mean, std, aggregations, axis logic
0505-Reshaping-Arraysreshape, flatten, transpose, concatenate, stack
0606-Linear-Algebradot, matmul, linalg module, eigenvalues, solving systems
0707-Random-Modulenp.random, distributions, seeding, sampling
0808-Boolean-Masking-WhereBoolean indexing, np.where, np.select, np.any/np.all
0909-Sorting-Searchingsort, argsort, searchsorted, unique, argmax
1010-IO-Filessave, load, savetxt, genfromtxt, .npy/.npz
1111-Performance-VectorizationVectorization, memory layout, dtype sizing, timing
1212-Common-Errors-GotchasShape mismatches, view/copy traps, dtype surprises

🗺️ Conceptual Map

graph TD
    A[NumPy] --> B[ndarray core]
    A --> C[Operations]
    A --> D[Specialized Modules]

    B --> B1[Creation]
    B --> B2[Attributes / dtype]
    B --> B3[Indexing & Slicing]

    C --> C1[Broadcasting & ufuncs]
    C --> C2[Math / Statistics]
    C --> C3[Sorting & Searching]
    C --> C4[Reshaping]

    D --> D1[linalg]
    D --> D2[random]
    D --> D3[IO: save/load]

⚡ Quick Reference — Most-Used Calls

np.array([1, 2, 3])                # create from list
np.zeros((3, 4)) / np.ones((3, 4))  # pre-filled arrays
np.arange(0, 10, 2)                  # like range(), returns array
np.linspace(0, 1, 5)                  # 5 evenly spaced points
 
arr.shape / arr.dtype / arr.ndim       # inspect structure
arr[arr > 5]                             # boolean filter
arr.reshape(2, 3)                          # change shape
arr.sum(axis=0) / arr.mean(axis=1)           # aggregate along an axis
np.where(arr > 0, 1, -1)                       # conditional element-wise
a @ b                                            # matrix multiplication
np.random.default_rng(42).random(5)                # reproducible random numbers
np.save("arr.npy", arr) / np.load("arr.npy")         # persist to disk

  • Pandas Reference — pandas’ Series/DataFrame wrap NumPy arrays internally; .to_numpy() converts back
  • Excel Reference — array formulas and SUMPRODUCT are Excel’s closest analog to NumPy’s vectorized math
  • ML Study Notes — NumPy is the computational backbone for cost functions, gradients, and matrix math in linear regression

How to use this vault section

Each note follows the same skeleton: Definition → Syntax → Key Parameters → Examples → Notes/Gotchas, matching the Pandas folder’s structure.