Installation and Import
Why NumPy Arrays?
Standard Python lists can hold mixed types and require Python-level loops for math. NumPy arrays are homogeneous (one data type throughout) and operations are vectorized — executed in bulk by compiled C code.Creating Arrays
From Python lists
Zeros, Ones, and Empty
Always pass multi-dimensional shapes as a tuple to
np.zeros, np.ones, and np.empty. Writing np.zeros(2, 3) raises a TypeError because NumPy interprets the second argument as a dtype parameter.Ranges
Linearly Spaced Values
Random Numbers
Array Properties
Indexing and Slicing
1D Arrays
2D Arrays
For 2D arrays, usearr[row_slice, column_slice]:
Boolean (Conditional) Filtering
Array Manipulation
Reshaping
ValueError.
Flattening
Transposing
Combining and Splitting Arrays
Sorting and Copying
Aggregate Functions
Vector Operations (Vectorization)
Vectorization means applying an operation to an entire array in a single statement, with no explicit Python loop. NumPy dispatches the calculation to compiled C code, making it dramatically faster.Broadcasting
Broadcasting lets you perform arithmetic between arrays of different shapes without manually copying data. NumPy logically expands the smaller array to match the larger one. Broadcasting rules (compared right to left):- Dimensions are compatible if they are equal, or if one of them is 1.
- If incompatible, NumPy raises a
ValueError.