0 Comments

If you’re working with numerical data in Python, chances are you’ve heard of NumPy. It’s the foundation of scientific computing in Python, providing powerful tools for working with arrays, matrices, and numerical functions.

This NumPy functions cheat sheet is designed to be your quick reference guide. Whether you’re a beginner or an experienced developer, this post covers the most commonly used NumPy functions, categorized and explained with examples. Bookmark this page and level up your Python game!


🔢 Array Creation Functions

NumPy offers several methods to create arrays quickly:

pythonCopyEditimport numpy as np
  • np.array() – Create array from list pythonCopyEdita = np.array([1, 2, 3])
  • np.zeros() – Array of all zeros pythonCopyEditnp.zeros((2, 3)) # 2x3 matrix
  • np.ones() – Array of all ones pythonCopyEditnp.ones((3, 3))
  • np.full() – Array filled with a specific value pythonCopyEditnp.full((2, 2), 5)
  • np.eye() – Identity matrix pythonCopyEditnp.eye(3)
  • np.arange() – Range of values pythonCopyEditnp.arange(0, 10, 2)
  • np.linspace() – Evenly spaced values pythonCopyEditnp.linspace(0, 1, 5)

🧮 Array Math Functions

Use these functions to perform element-wise mathematical operations:

  • np.add(), np.subtract(), np.multiply(), np.divide()
  • np.power() – Raise elements to a power
  • np.mod() – Modulus of array elements

Example:

pythonCopyEdita = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.add(a, b)  # Output: [5 7 9]

🧠 Statistical Functions

  • np.mean() – Mean of array elements
  • np.median() – Median value
  • np.std() – Standard deviation
  • np.var() – Variance
  • np.min() / np.max() – Minimum/Maximum values
  • np.percentile() – Percentile of elements

Example:

pythonCopyEditarr = np.array([1, 2, 3, 4])
np.mean(arr)  # Output: 2.5

🧩 Array Reshaping Functions

These functions help reshape or manipulate the structure of arrays:

  • np.reshape() – Change array shape
  • np.ravel() – Flatten array
  • np.transpose() – Transpose matrix
  • np.swapaxes() – Swap axes in multidimensional arrays

Example:

pythonCopyEditarr = np.array([[1, 2], [3, 4]])
arr.T  # Output: [[1 3] [2 4]]

🔄 Sorting and Searching Functions

  • np.sort() – Sort elements
  • np.argsort() – Indices of sorted elements
  • np.where() – Conditional filtering
  • np.argmin() / np.argmax() – Index of min/max

Example:

pythonCopyEditarr = np.array([10, 5, 8])
np.where(arr > 6)  # Output: (array([0, 2]),)

🎲 Random Functions (NumPy Random)

  • np.random.rand() – Random floats in [0, 1)
  • np.random.randint() – Random integers
  • np.random.randn() – Random samples from normal distribution
  • np.random.choice() – Random pick from array
  • np.random.seed() – Set random seed for reproducibility

Example:

pythonCopyEditnp.random.seed(0)
np.random.randint(1, 10, size=(2, 3))

📏 Linear Algebra Functions

  • np.dot() – Dot product
  • np.matmul() – Matrix multiplication
  • np.linalg.inv() – Inverse of matrix
  • np.linalg.det() – Determinant
  • np.linalg.eig() – Eigenvalues and eigenvectors

Example:

pythonCopyEditA = np.array([[1, 2], [3, 4]])
np.linalg.inv(A)

🧪 Useful Utility Functions

  • np.unique() – Unique elements
  • np.isnan() – Check for NaN values
  • np.isinf() – Check for infinite values
  • np.clip() – Limit values within range

Example:

pythonCopyEditarr = np.array([1, 10, 100])
np.clip(arr, 0, 50)  # Output: [1 10 50]

❓ FAQs About NumPy Functions

Q1: What is the most commonly used function in NumPy?
np.array() is the foundation for working with NumPy arrays.

Q2: Are NumPy operations faster than regular Python lists?
Yes, NumPy is optimized with C under the hood and supports vectorized operations.

Q3: Can NumPy handle missing values?
NumPy itself doesn’t handle missing values well. Use np.isnan() to identify them or consider using pandas.

Q4: How do I generate reproducible random numbers in NumPy?
Use np.random.seed(0) before generating random numbers.

Q5: Is NumPy good for large datasets?
Yes, NumPy is memory-efficient and ideal for large numerical datasets.


🏁 Conclusion

This NumPy functions cheat sheet is your go-to reference when working with arrays, matrices, statistics, and linear algebra in Python. By mastering these core functions, you’ll not only save time but also write cleaner, faster, and more efficient code.

Whether you’re analyzing data, building ML models, or solving scientific problems, NumPy functions make your workflow seamless. Keep this cheat sheet handy and continue exploring the vast world of Python programming!

Looking for a quick reference for NumPy? Check out this NumPy functions cheat sheet with commonly used functions, examples, and tips for efficient coding in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts