Bytes
Data Science

NumPy Cheat Sheet

Last Updated: 16th August, 2024
icon

Arunav Goswami

Data Science Consultant at almaBetter

Discover the ultimate NumPy cheat sheet for Python, covering essential functions, commands and operations to streamline numerical computing tasks effectively

NumPy is an essential library for numerical computing in Python. It provides support for arrays, matrices, and a wide range of mathematical functions. This python numpy cheat sheet will cover important aspects of NumPy, including its functions, commands, syntax, and use cases with examples.

Introduction to NumPy

NumPy, short for Numerical Python, is a powerful library for performing mathematical and logical operations on arrays. It forms the foundation of many scientific computing packages in Python, such as SciPy, Pandas, and Matplotlib.

Key features of NumPy

Efficient Multidimensional Array Object: Known as ndarray.

Broadcasting Capabilities: Enables arithmetic operations on arrays of different shapes.

Mathematical Functions: A vast collection of mathematical functions for operations like algebra, statistics, and trigonometry.

Integration with C/C++ and Fortran: Provides tools for integrating with C/C++ and Fortran code.

Basic NumPy Syntax Cheat Sheet

Importing NumPy

To use NumPy, you must first import it into your Python environment:

import numpy as np

Creating Arrays

NumPy arrays can be created using various functions:

From a Python list:

np.array([123])

From a range of values:

np.arange(0102# [0, 2, 4, 6, 8]

From a list of lists (multidimensional array):

np.array([[12], [34]])

Array Attributes

Understanding array attributes is crucial for effective manipulation:

Shape and Size:

Shape = array.shape

Size = array.size

Dimensions:

array.ndim

Data type:

array.dtype

NumPy Array Operations

NumPy Arithmetic Functions Cheat Sheet

NumPy provides a suite of functions for element-wise operations:

Addition:

np.add(array1, array2)

Subtraction:

np.subtract(array1, array2)

Multiplication:

np.multiply(array1, array2)

Division:

np.divide(array1, array2)

Universal Functions (ufuncs)

Universal functions operate element-wise on arrays and include:

Square root:

np.sqrt(array)

Exponentiation:

np.exp(array)

Logarithm:

np.log(array)

Aggregate Functions

Aggregate functions compute a single value from an array:

Sum:

np.sum(array)

Mean:

np.mean(array)

Median:

np.median(array)

Standard Deviation:

np.std(array)

Indexing and Slicing

Basic Indexing

Accessing elements in a NumPy array:

Single element:

array[01]

Slicing:

array[0:21:3]

Fancy Indexing

Fancy indexing allows accessing multiple array elements at once:

Using lists or arrays:

array[[01], [12]]

Boolean Indexing

Boolean indexing uses boolean arrays to select elements:

Condition-based:

array[array > 5]

NumPy Functions Cheat Sheet

Array Creation

zeros: Creates an array filled with zeros.

np.zeros((33)) # array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])

ones: Creates an array filled with ones.

np.ones((22)) # array([[1., 1.], [1., 1.]])

full: Creates an array filled with a specified value.

np.full((22), 10# array([[10, 10], [10, 10]])

eye: Creates an identity matrix.

np.eye(4# array([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])

random.rand: Creates an array of given shapes with random values between 0 and 1.

np.random.rand(23# array([[0.748268, 0.180203, 0.389023], [0.0376, 0.011787, 0.996268]])

random.randint: Creates an array of given shapes with random integers.

np.random.randint(010, (33)) # array([[5, 0, 3], [3, 7, 9], [3, 5, 2]])

linspace: Creates an array of evenly spaced values over a specified interval.

np.linspace(0105# [0. , 0.25, 0.5 , 0.75, 1. ]

Array Operations

add: Element-wise addition.

np.add(array1, array2)

subtract: Element-wise subtraction.

np.subtract(array1, array2)

multiply: Element-wise multiplication.

np.multiply(array1, array2)

divide: Element-wise division.

np.divide(array1, array2)

Linear Algebra

dot: Dot product of two arrays.

np.dot(array1, array2)

matmul: Matrix multiplication.

np.matmul(array1, array2)

transpose: Transpose of an array.

np.transpose(array)

inv: Inverse of a matrix.

np.linalg.inv(matrix)

det: determinant of a matrix

np.linalg.det(matrix)

NumPy Statistics Cheat Sheet

Statistical Functions

sum: Sum of all elements.

np.sum(array)

mean: Mean of all elements.

np.mean(array)

median: Median of all elements.

np.median(array)

std: Standard deviation of all elements.

np.std(array)

min and max: Minimum and Maximum of all elements.

min_val = np.min(a)
max_val = np.max(a)

Advanced NumPy Features

Broadcasting

Broadcasting allows NumPy to perform element-wise operations on arrays of different shapes:

Example:

array1 = np.array([123])
array2 = np.array([[1], [2], [3]])
result = array1 + array2

Vectorization

Vectorization speeds up operations by eliminating the need for explicit loops:

Example:

array = np.arange(10000)
result = np.sin(array) # array([ 0. , 0.84147098, 0.90929743, ..., -0.30561439, 0.38778164, 0.14112001])

Reshaping Arrays

Reshaping changes the shape of an array without altering its data:

Reshape:

array = np.arange(6).reshape((23)) # array([[0, 1, 2], [3, 4, 5]])

Flatten:

array = array.flatten() # array([0, 1, 2, 3, 4, 5])

NumPy and Pandas Cheat Sheet

NumPy arrays are often used in conjunction with Pandas for data analysis:

Converting NumPy array to Pandas DataFrame:

import pandas as pd
df = pd.DataFrame(array)

Converting Pandas DataFrame to NumPy array:

array = df.values

Miscellaneous

Concatenation:

concatenated = np.concatenate((a, b), axis=0)

Stacking:

stacked = np.vstack((a, b))

Common Pitfalls

Broadcasting Errors: Ensure arrays are compatible with operations.

# This will raise an error if a and b have incompatible shapes.
result = a + b

Copy vs. View: Modifying a view affects the original array.

view = a.view()
view[0] = 99  # This changes a[0] as well.

Conclusion

NumPy is an indispensable Python library for numerical computations. This NumPy cheatsheet provides a quick reference to its essential functions, commands, and operations. Understanding and leveraging these features can significantly enhance your data processing and analysis capabilities. Whether you are performing simple arithmetic operations or complex linear algebra calculations, NumPy's efficient and versatile tools will undoubtedly streamline your workflow.

AlmaBetter
Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter