NumPy Basics
| Task | Code | Description |
|---|
| Import NumPy | import numpy as np | Import NumPy library |
| Check version | np.version | Display NumPy version |
| Create array | np.array([1,2,3]) | Create 1D array |
| Create 2D array | np.array([[1,2,3],[4,5,6]]) | Create 2D array |
| Create zeros | np.zeros((3,3)) | 3×3 matrix of zeros |
| Create ones | np.ones((2,4)) | 2×4 matrix of ones |
| Create constant array | np.full((2,3), 7) | Matrix filled with 7 |
| Create identity matrix | np.eye(3) | 3×3 identity matrix |
| Create random array | np.random.rand(2,3) | Uniform random numbers |
| Create random int | np.random.randint(1,10,(3,3)) | Random integers |
Array Inspection
| Task | Code | Description |
|---|
| Shape of array | arr.shape | (rows, columns) |
| Dimensions | arr.ndim | No. of dimensions |
| Total elements | arr.size | Total elements |
| Data type | arr.dtype | Element data type |
| Change type | arr.astype(float) | Convert type |
| Item size | arr.itemsize | Bytes per element |
| Info summary | arr.flags | Memory info |
Array Creation Helpers
| Task | Code | Description |
|---|
| Range array | np.arange(0,10,2) | Evenly spaced values |
| Linspace | np.linspace(0,1,5) | 5 values from 0 to 1 |
| Random normal | np.random.randn(3,3) | Standard normal distribution |
| Empty array | np.empty((2,2)) | Uninitialized values |
| Identity matrix | np.identity(4) | 4×4 identity matrix |
| Diagonal matrix | np.diag([1,2,3]) | Diagonal matrix |
Mathematical Operations
| Task | Code | Description |
|---|
| Add arrays | a + b | Element-wise addition |
| Subtract arrays | a - b | Element-wise subtraction |
| Multiply arrays | a * b | Element-wise multiplication |
| Divide arrays | a / b | Element-wise division |
| Dot product | np.dot(a,b) | Matrix multiplication |
| Matrix product | a @ b | Matrix multiplication operator |
| Exponentiation | np.exp(a) | e^a |
| Square root | np.sqrt(a) | √a |
| Logarithm | np.log(a) | Natural log |
| Power | np.power(a,2) | a² |
| Rounding | np.round(a,2) | Round to 2 decimals |
Statistical Operations
| Task | Code | Description |
|---|
| Sum | np.sum(a) | Total sum |
| Mean | np.mean(a) | Average |
| Median | np.median(a) | Median |
| Standard deviation | np.std(a) | Std. deviation |
| Variance | np.var(a) | Variance |
| Min value | np.min(a) | Minimum |
| Max value | np.max(a) | Maximum |
| Argmin index | np.argmin(a) | Index of min value |
| Argmax index | np.argmax(a) | Index of max value |
| Cumulative sum | np.cumsum(a) | Running total |
| Cumulative product | np.cumprod(a) | Running product |
Array Indexing & Slicing
| Task | Code | Description |
|---|
| Get element | a[0], a[1,2] | Access element |
| Slice array | a[1:4] | Elements 1–3 |
| 2D slicing | a[0:2, 1:3] | Subarray slice |
| Step slicing | a[::2] | Every 2nd element |
| Reverse | a[::-1] | Reverse array |
| Conditional | a[a > 5] | Filter values |
| Fancy indexing | a[[0,2,4]] | Select specific indices |
| Replace values | a[a < 0] = 0 | Conditional assignment |
Reshaping & Combining
| Task | Code | Description |
|---|
| Reshape | a.reshape(3,2) | Change shape |
| Flatten | a.flatten() | 1D version |
| Ravel | a.ravel() | Flattened view |
| Transpose | a.T | Swap rows/columns |
| Concatenate | np.concatenate((a,b), axis=0) | Stack vertically |
| Stack horizontally | np.hstack((a,b)) | Horizontal merge |
| Stack vertically | np.vstack((a,b)) | Vertical merge |
| Split array | np.split(a, 3) | Split into parts |
Linear Algebra
| Task | Code | Description |
|---|
| Matrix product | np.dot(A,B) | Multiply matrices |
| Transpose | A.T | Transpose matrix |
| Determinant | np.linalg.det(A) | Determinant |
| Inverse | np.linalg.inv(A) | Matrix inverse |
| Eigenvalues | np.linalg.eig(A) | Eigenvalues/vectors |
| Rank | np.linalg.matrix_rank(A) | Rank of matrix |
| Solve linear eq. | np.linalg.solve(A, b) | Solve Ax = b |
Broadcasting
| Task | Code | Description |
|---|
| Add scalar | a + 10 | Add scalar to array |
| Multiply scalar | a * 5 | Multiply array |
| Add vectors | a + b | Automatically expands shapes |
| Different shapes | a + np.array([1,2,3]) | Matches smaller dimension |
Random Module
| Task | Code | Description |
|---|
| Random float [0,1) | np.random.rand(3,3) | Uniform random |
| Random integers | np.random.randint(0,10,(3,3)) | Random ints |
| Normal distribution | np.random.randn(3,3) | Standard normal |
| Random choice | np.random.choice([1,2,3,4]) | Random pick |
| Shuffle array | np.random.shuffle(a) | In-place shuffle |
| Seed for repeat | np.random.seed(42) | Reproducibility |
Useful Utilities
| Task | Code | Description |
|---|
| Unique elements | np.unique(a) | Get unique values |
| Sort array | np.sort(a) | Sort elements |
| Concatenate arrays | np.concatenate((a,b)) | Join arrays |
| Compare arrays | np.array_equal(a,b) | Check equality |
| Copy array | b = a.copy() | Deep copy |
| Repeat elements | np.repeat(a, 2) | Repeat each element |
| Tile array | np.tile(a, 3) | Repeat whole array |
Logical Operations
| Task | Code | Description |
|---|
| Element-wise AND | np.logical_and(a>0, a<5) | Logical AND |
| Element-wise OR | np.logical_or(a>5, a==0) | Logical OR |
| Element-wise NOT | np.logical_not(a) | Logical negation |
| Any True? | np.any(a > 0) | Any element True |
| All True? | np.all(a > 0) | All elements True |
| Where condition | np.where(a>0, 'Yes', 'No') | Conditional output |