NumPy Cheatsheet 2025: From Basics to Advanced in One Guide

Introduction

When it comes to numerical computing in Python, NumPy is the foundation. It provides fast, memory-efficient arrays and mathematical operations that power almost every data science, AI, and machine learning workflow. From creating arrays to performing advanced linear algebra, NumPy simplifies complex numerical tasks with concise methods.

This blog is your NumPy cheatsheet, covering the most important functions grouped by category. It’s designed for quick reference, so you can save time while coding and focus on insights rather than syntax.

Array Creation

Creating arrays is the starting point in NumPy.

  • np.array() – Create an array from a list or sequence.
  • np.zeros() – Create an array filled with zeros.
  • np.ones() – Create an array filled with ones.
  • np.arange(start, stop, step) – Create evenly spaced values within a range.
  • np.linspace(start, stop, num) – Create evenly spaced values between two numbers.
  • np.eye(n) – Identity matrix with ones on the diagonal.
  • np.identity(n) – Identity matrix (similar to np.eye).
  • np.full(shape, value) – Create an array with a constant value.
  • np.empty(shape) – Create an uninitialized array (faster than zeros/ones).
  • np.logspace(start, stop, num) – Logarithmically spaced values.
  • np.geomspace(start, stop, num) – Geometric progression.
  • np.meshgrid() – Create coordinate matrices from vectors.

Array Inspection

Check properties of arrays to understand their shape and type.

  • arr.shape – Dimensions of the array.
  • 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 memory consumed by elements.
  • arr.T – Transpose of the array.
  • arr.flat – 1D iterator over elements.
  • np.info(obj) – Information about NumPy objects.

Indexing & Slicing

NumPy offers powerful ways to access and modify array elements.

  • arr[] – Basic indexing.
  • arr[start:end] – Slice elements.
  • arr[::step] – Slice with steps.
  • arr[boolean_mask] – Boolean indexing.
  • arr[[1,3,5]] – Fancy indexing with integer lists.
  • np.take(arr, indices) – Take elements at specific indices.
  • np.put(arr, indices, values) – Set values at specific indices.
  • np.delete(arr, index/axis) – Delete elements along an axis.
  • np.extract(condition, arr) – Extract elements based on condition.

Mathematical Operations

NumPy provides vectorized math operations for fast computation.

  • np.add(), np.subtract(), np.multiply(), np.divide() – Basic arithmetic.
  • np.power() – Raise elements to a power.
  • np.sqrt() – Square root of each element.
  • np.exp() – Exponential values.
  • np.log(), np.log10() – Natural and base-10 log.
  • np.abs() – Absolute values.
  • np.sign() – Sign of each element.
  • np.floor(), np.ceil() – Round down/up.
  • np.round(arr, decimals) – Round to given decimals.
  • np.cumsum() – Cumulative sum.
  • np.cumprod() – Cumulative product.
  • np.diff() – Discrete difference along axis.
  • np.gradient() – Gradient of an array.
  • np.trapz() – Integration using trapezoidal rule.

Statistical Functions

Quickly compute descriptive statistics on arrays.

  • np.mean() – Mean of elements.
  • np.median() – Median value.
  • np.std() – Standard deviation.
  • np.var() – Variance.
  • np.min(), np.max() – Minimum and maximum values.
  • np.percentile() – Percentile score.
  • np.quantile() – Quantiles of data.
  • np.argmax(), np.argmin() – Indices of max/min values.
  • np.corrcoef() – Correlation coefficients.
  • np.cov() – Covariance matrix.
  • np.histogram(arr, bins) – Histogram of array.
  • np.bincount() – Count occurrences of values.

Reshaping & Combining Arrays

Reshape and manipulate arrays with ease.

  • arr.reshape() – Change array shape.
  • arr.ravel() – Flatten into 1D.
  • np.transpose() – Permute dimensions.
  • np.expand_dims(arr, axis) – Add a new axis.
  • np.squeeze(arr) – Remove single-dimensional axes.
  • np.vstack() – Stack arrays vertically.
  • np.hstack() – Stack arrays horizontally.
  • np.concatenate() – Join arrays along an axis.
  • np.stack(arrays, axis) – Stack arrays along new axis.
  • np.column_stack() – Stack 1D arrays as columns.
  • np.dstack() – Stack arrays along depth.
  • np.split() – Split arrays into sub-arrays.

Linear Algebra

NumPy includes efficient functions for matrix computations.

  • np.dot() – Dot product.
  • np.matmul() – Matrix multiplication.
  • np.linalg.inv() – Inverse of a matrix.
  • np.linalg.det() – Determinant.
  • np.linalg.eig() – Eigenvalues and eigenvectors.
  • np.linalg.norm() – Matrix or vector norm.
  • np.trace() – Sum of diagonal elements.
  • np.triu(), np.tril() – Upper/lower triangular part of a matrix.
  • np.linalg.qr() – QR decomposition.
  • np.linalg.svd() – Singular Value Decomposition.
  • np.linalg.cholesky() – Cholesky decomposition.

Random Number Generation

Simulate randomness and generate distributions.

  • np.random.rand() – Random values in [0,1).
  • np.random.randn() – Standard normal distribution.
  • np.random.randint() – Random integers.
  • np.random.choice() – Random sample from array.
  • np.random.seed() – Set random seed for reproducibility.
  • np.random.uniform(low, high, size) – Uniform distribution.
  • np.random.binomial(n, p, size) – Binomial distribution.
  • np.random.poisson(lam, size) – Poisson distribution.
  • np.random.beta(a, b, size) – Beta distribution.
  • np.random.gamma(shape, scale, size) – Gamma distribution.
  • np.random.normal(mean, std, size) – Normal distribution.

Useful Array Functions

Handy methods for day-to-day data manipulation.

  • np.sort() – Sort array.
  • np.unique() – Unique values.
  • np.where() – Conditional selection.
  • np.clip() – Limit values to a range.
  • np.tile() – Repeat array patterns.
  • np.repeat() – Repeat elements.
  • np.pad(arr, pad_width) – Pad array with values.
  • np.flip(arr) – Reverse elements.
  • np.roll(arr, shift) – Roll array elements.
  • np.isin(arr, values) – Check membership.
  • np.any(arr), np.all(arr) – Logical checks.
  • np.nonzero(arr) – Indices of non-zero elements.
  • np.count_nonzero(arr) – Count non-zero values.
  • np.isnan(arr), np.isinf(arr) – Detect NaN or infinity.
  • np.nan_to_num(arr) – Replace NaN/inf with numbers.
  • np.place(arr, mask, values) – Place values where condition is True.

Input & Output

Save and load arrays efficiently.

  • np.save(filename, arr) – Save array to .npy file.
  • np.load(filename) – Load .npy file.
  • np.savetxt(filename, arr, delimiter) – Save to text/CSV.
  • np.loadtxt(filename, delimiter) – Load from text/CSV.
  • np.genfromtxt(filename) – Load with missing values.
  • np.fromfile() / np.tofile() – Read/write raw binary.

Masked Arrays & Special Functions

Advanced tools for handling missing data and special cases.

  • np.ma.masked_where(condition, arr) – Mask elements.
  • np.ma.filled() – Fill masked values.
  • np.ma.mean() – Mean while ignoring masked values.
  • np.interp(x, xp, fp) – 1D linear interpolation.
  • np.polyfit(x, y, deg) – Polynomial fitting.
  • np.polyval(p, x) – Evaluate polynomial.

Conclusion

NumPy is the cornerstone of scientific computing in Python. With its optimized arrays and mathematical functions, it helps developers, analysts, and researchers work efficiently with large datasets.

This cheatsheet highlighted the most commonly used methods plus advanced techniques—from array creation to linear algebra, from statistics to random distributions. Keep this guide handy, and over time you’ll memorize the core functions. As your projects grow, you’ll find that mastering NumPy is the first step toward unlocking advanced machine learning, AI, and data science workflows.

External Resources

Real Python – NumPy Tutorials
https://realpython.com/tutorials/numpy/

GeeksforGeeks – NumPy
https://www.geeksforgeeks.org/python-numpy/

TutorialsPoint NumPy Tutorial
https://www.tutorialspoint.com/numpy/index.htm

2 thoughts on “NumPy Cheatsheet 2025: From Basics to Advanced in One Guide”

Leave a Comment