| Term | Definition |
|---|---|
| NumPy | Numerical Python library providing support for large multi-dimensional arrays and matrices with mathematical functions |
| ndarray | N-dimensional array object, the core data structure in NumPy for homogeneous data |
| dtype | Data type object describing the data type of array elements (int32, float64, bool, etc.) |
| Vectorization | Performing operations on entire arrays without explicit loops, enabling faster computation |
| Broadcasting | Mechanism allowing NumPy to perform operations on arrays of different shapes |
import numpy as npnp. prefix| Function | Description |
|---|---|
| np.array(object) | Create array from list, tuple, or nested sequences |
| np.zeros(shape) | Create array filled with zeros |
| np.ones(shape) | Create array filled with ones |
| np.empty(shape) | Create uninitialized array (contains arbitrary values) |
| np.full(shape, value) | Create array filled with specified value |
| np.eye(n) or np.identity(n) | Create n×n identity matrix |
| Function | Description |
|---|---|
| np.arange(start, stop, step) | Create array with evenly spaced values within interval (excludes stop) |
| np.linspace(start, stop, num) | Create array with num evenly spaced values (includes stop) |
| np.logspace(start, stop, num) | Create array with logarithmically spaced values |
| Function | Description |
|---|---|
| np.random.rand(d0, d1, ...) | Random values from uniform distribution [0, 1) |
| np.random.randn(d0, d1, ...) | Random values from standard normal distribution |
| np.random.randint(low, high, size) | Random integers from low (inclusive) to high (exclusive) |
| np.random.random(size) | Random floats in [0.0, 1.0) |
| np.random.choice(a, size) | Random sample from given array |
| np.random.seed(value) | Set random seed for reproducibility |
| Attribute | Description |
|---|---|
| arr.shape | Tuple of array dimensions (rows, columns, ...) |
| arr.ndim | Number of array dimensions |
| arr.size | Total number of elements in array |
| arr.dtype | Data type of array elements |
| arr.itemsize | Size in bytes of each element |
| arr.nbytes | Total bytes consumed by array (size × itemsize) |
| arr.T | Transposed array |
| Type | Description |
|---|---|
| int8, int16, int32, int64 | Signed integers (8, 16, 32, 64 bits) |
| uint8, uint16, uint32, uint64 | Unsigned integers |
| float16, float32, float64 | Floating point numbers (half, single, double precision) |
| complex64, complex128 | Complex numbers |
| bool | Boolean (True/False) |
| object | Python object type |
| string_, unicode_ | Fixed-length string types |
| Function | Description |
|---|---|
| arr.reshape(shape) | Return array with new shape (must have same total elements) |
| arr.resize(shape) | Modify array shape in-place |
| arr.flatten() | Return flattened 1D copy of array |
| arr.ravel() | Return flattened 1D view (when possible) |
| arr.T or arr.transpose() | Transpose array (swap rows and columns) |
| np.expand_dims(arr, axis) | Add new axis at specified position |
| np.squeeze(arr) | Remove single-dimensional entries |
| Function | Description |
|---|---|
| np.concatenate((a1, a2), axis) | Join arrays along existing axis |
| np.vstack((a1, a2)) | Stack arrays vertically (row-wise) |
| np.hstack((a1, a2)) | Stack arrays horizontally (column-wise) |
| np.dstack((a1, a2)) | Stack arrays depth-wise (along third axis) |
| np.column_stack((a1, a2)) | Stack 1D arrays as columns into 2D array |
| np.row_stack((a1, a2)) | Stack 1D arrays as rows (equivalent to vstack) |
| Function | Description |
|---|---|
| np.split(arr, indices, axis) | Split array into multiple sub-arrays |
| np.vsplit(arr, indices) | Split array vertically |
| np.hsplit(arr, indices) | Split array horizontally |
| np.dsplit(arr, indices) | Split array depth-wise |
| Operation | Description |
|---|---|
| arr1 + arr2 or np.add() | Element-wise addition |
| arr1 - arr2 or np.subtract() | Element-wise subtraction |
| arr1 * arr2 or np.multiply() | Element-wise multiplication |
| arr1 / arr2 or np.divide() | Element-wise division |
| arr1 // arr2 or np.floor_divide() | Floor division |
| arr1 % arr2 or np.mod() | Modulus |
| arr1 ** arr2 or np.power() | Exponentiation |
| Function | Description |
|---|---|
| np.sqrt(arr) | Square root |
| np.exp(arr) | Exponential (e^x) |
| np.log(arr), np.log10(arr), np.log2(arr) | Natural, base-10, base-2 logarithm |
| np.abs(arr) or np.absolute(arr) | Absolute value |
| np.sign(arr) | Sign of elements (-1, 0, 1) |
| np.ceil(arr), np.floor(arr) | Ceiling and floor |
| np.round(arr, decimals) | Round to specified decimals |
| np.clip(arr, min, max) | Limit values to range [min, max] |
| Function | Description |
|---|---|
| np.dot(a, b) or a.dot(b) | Dot product (1D) or matrix multiplication (2D) |
| a @ b | Matrix multiplication operator |
| np.matmul(a, b) | Matrix multiplication |
| np.linalg.inv(arr) | Matrix inverse |
| np.linalg.det(arr) | Matrix determinant |
| np.linalg.eig(arr) | Eigenvalues and eigenvectors |
| np.linalg.solve(A, b) | Solve linear system Ax = b |
| np.trace(arr) | Sum of diagonal elements |
| np.linalg.norm(arr) | Matrix or vector norm |
| Function | Description |
|---|---|
| np.sum(arr, axis) | Sum of elements along axis |
| np.mean(arr, axis) | Arithmetic mean |
| np.median(arr, axis) | Median value |
| np.std(arr, axis) | Standard deviation |
| np.var(arr, axis) | Variance |
| np.min(arr, axis) or arr.min() | Minimum value |
| np.max(arr, axis) or arr.max() | Maximum value |
| np.argmin(arr, axis) | Index of minimum value |
| np.argmax(arr, axis) | Index of maximum value |
| np.cumsum(arr, axis) | Cumulative sum |
| np.cumprod(arr, axis) | Cumulative product |
| np.percentile(arr, q, axis) | q-th percentile |
| np.quantile(arr, q, axis) | q-th quantile |
| Function | Description |
|---|---|
| np.sort(arr, axis) | Return sorted copy of array |
| arr.sort() | Sort array in-place |
| np.argsort(arr, axis) | Return indices that would sort array |
| np.lexsort(keys) | Sort using multiple keys |
| np.partition(arr, kth) | Partial sort (kth element in sorted position) |
| Function | Description |
|---|---|
| np.where(condition, x, y) | Return elements from x or y depending on condition |
| np.argwhere(condition) | Return indices where condition is True |
| np.searchsorted(arr, values) | Find indices to insert values to maintain order |
| np.unique(arr, return_counts) | Find unique elements (optionally with counts) |
| np.count_nonzero(arr) | Count non-zero elements |
| np.nonzero(arr) | Return indices of non-zero elements |
| Function | Description |
|---|---|
| np.logical_and(a, b) | Element-wise AND |
| np.logical_or(a, b) | Element-wise OR |
| np.logical_not(arr) | Element-wise NOT |
| np.logical_xor(a, b) | Element-wise XOR |
| np.all(arr, axis) | Test if all elements are True |
| np.any(arr, axis) | Test if any element is True |
| np.isnan(arr) | Test for NaN values |
| np.isinf(arr) | Test for infinity |
| np.isfinite(arr) | Test for finite values |
| Function | Description |
|---|---|
| np.intersect1d(a, b) | Find common elements in two arrays |
| np.union1d(a, b) | Union of elements from two arrays |
| np.setdiff1d(a, b) | Elements in a that are not in b |
| np.setxor1d(a, b) | Elements in either array but not both |
| np.in1d(a, b) | Test whether elements of a are in b |
| Function | Description |
|---|---|
| np.save('file.npy', arr) | Save single array to binary file |
| np.load('file.npy') | Load array from binary file |
| np.savez('file.npz', a=arr1, b=arr2) | Save multiple arrays in uncompressed archive |
| np.savez_compressed('file.npz', a=arr1) | Save multiple arrays in compressed archive |
| Function | Description |
|---|---|
| np.savetxt('file.txt', arr, delimiter=',') | Save array to text file |
| np.loadtxt('file.txt', delimiter=',') | Load array from text file |
| np.genfromtxt('file.csv', delimiter=',') | Load data with missing values handling |
| Concept | Description |
|---|---|
| C-contiguous (row-major) | Elements stored row-by-row (NumPy default) |
| F-contiguous (column-major) | Elements stored column-by-column (Fortran order) |
| arr.flags | Information about memory layout |
| np.ascontiguousarray(arr) | Return C-contiguous array |
| np.asfortranarray(arr) | Return F-contiguous array |
| Task | Function |
|---|---|
| Create from list | np.array([1, 2, 3]) |
| Create zeros | np.zeros((3, 4)) |
| Create range | np.arange(0, 10, 2) |
| Reshape | arr.reshape(3, 4) |
| Flatten | arr.flatten() or arr.ravel() |
| Transpose | arr.T |
| Join arrays | np.concatenate((a, b), axis=0) |
| Sum all elements | arr.sum() |
| Mean value | arr.mean() |
| Max value | arr.max() |
| Sort | np.sort(arr) |
| Unique elements | np.unique(arr) |