np.array([1, 2, 3]) | Create array from list |
np.zeros((3, 4)) | Create array of zeros |
np.ones((2, 3)) | Create array of ones |
np.empty((2, 2)) | Create empty array |
np.full((3, 3), 7) | Create array filled with value |
np.eye(4) | Create identity matrix |
np.arange(0, 10, 2) | Create array with range |
np.linspace(0, 1, 5) | Create evenly spaced array |
arr.shape | Array dimensions |
arr.ndim | Number of dimensions |
arr.size | Total number of elements |
arr.dtype | Data type of elements |
arr.itemsize | Size of each element in bytes |
arr.nbytes | Total bytes consumed |
np.int32, np.int64 | Integer types |
np.float32, np.float64 | Float types |
np.complex64, np.complex128 | Complex types |
np.bool_ | Boolean type |
arr.astype(np.float64) | Convert data type |
arr[0] | First element |
arr[-1] | Last element |
arr[2, 3] | 2D array element at row 2, col 3 |
arr[1:4] | Slice from index 1 to 3 |
arr[::2] | Every second element |
arr[::-1] | Reverse array |
arr[[0, 2, 4]] | Select by index array |
arr[arr > 5] | Boolean indexing |
arr[np.where(arr > 5)] | Where condition |
np.argmax(arr) | Index of max value |
np.argmin(arr) | Index of min value |
np.nonzero(arr) | Indices of non-zero elements |
arr.reshape(3, 4) | Reshape to 3x4 |
arr.flatten() | Flatten to 1D (copy) |
arr.ravel() | Flatten to 1D (view) |
arr.T | Transpose |
arr.transpose(1, 0, 2) | Transpose with axes order |
arr.squeeze() | Remove single-dimensional entries |
np.expand_dims(arr, axis=0) | Add dimension |
np.concatenate([a, b], axis=0) | Concatenate along axis |
np.vstack([a, b]) | Stack vertically |
np.hstack([a, b]) | Stack horizontally |
np.dstack([a, b]) | Stack depth-wise |
np.stack([a, b], axis=0) | Stack along new axis |
np.split(arr, 3) | Split into 3 equal parts |
np.vsplit(arr, 2) | Split vertically |
np.hsplit(arr, 2) | Split horizontally |
np.array_split(arr, 3) | Split (allows unequal) |
arr + 5, arr - 5 | Add/subtract scalar |
arr * 2, arr / 2 | Multiply/divide scalar |
arr ** 2 | Power |
a + b, a * b | Element-wise operations |
np.dot(a, b) | Dot product |
a @ b | Matrix multiplication |
np.mean(arr) | Mean |
np.median(arr) | Median |
np.std(arr) | Standard deviation |
np.var(arr) | Variance |
np.sum(arr) | Sum |
np.prod(arr) | Product |
np.min(arr), np.max(arr) | Min/Max |
np.percentile(arr, 75) | 75th percentile |
np.sqrt(arr) | Square root |
np.exp(arr) | Exponential |
np.log(arr), np.log10(arr) | Natural/base-10 log |
np.sin(arr), np.cos(arr) | Trigonometric |
np.abs(arr) | Absolute value |
np.round(arr, 2) | Round to 2 decimals |
np.floor(arr), np.ceil(arr) | Floor/ceiling |
np.linalg.inv(A) | Matrix inverse |
np.linalg.det(A) | Determinant |
np.linalg.matrix_rank(A) | Matrix rank |
np.trace(A) | Trace (sum of diagonal) |
np.linalg.norm(A) | Matrix norm |
np.linalg.eig(A) | Eigenvalues and eigenvectors |
np.linalg.svd(A) | Singular value decomposition |
np.linalg.qr(A) | QR decomposition |
np.linalg.cholesky(A) | Cholesky decomposition |
np.linalg.solve(A, b) | Solve Ax = b |
np.linalg.lstsq(A, b) | Least squares solution |
np.random.rand(3, 4) | Uniform [0, 1) |
np.random.randn(3, 4) | Standard normal |
np.random.randint(0, 10, (3, 4)) | Random integers |
np.random.uniform(0, 1, 10) | Uniform distribution |
np.random.normal(0, 1, 10) | Normal distribution |
np.random.choice(arr, 5) | Random choice |
np.random.shuffle(arr) | Shuffle in place |
np.random.permutation(arr) | Random permutation |
np.random.seed(42) | Set random seed |
np.save("arr.npy", arr) | Save single array (binary) |
np.load("arr.npy") | Load .npy file |
np.savez("arrs.npz", a=arr1, b=arr2) | Save multiple arrays |
np.savetxt("arr.csv", arr, delimiter=",") | Save as text/CSV |
np.loadtxt("arr.csv", delimiter=",") | Load from text/CSV |
np.genfromtxt("data.csv", delimiter=",") | Load with missing values |