Placements
About Us
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.
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.
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.
To use NumPy, you must first import it into your Python environment:
import numpy as np |
NumPy arrays can be created using various functions:
From a Python list:
np.array([1, 2, 3]) |
From a range of values:
np.arange(0, 10, 2) # [0, 2, 4, 6, 8] |
From a list of lists (multidimensional array):
np.array([[1, 2], [3, 4]]) |
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 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 operate element-wise on arrays and include:
Square root:
np.sqrt(array) |
Exponentiation:
np.exp(array) |
Logarithm:
np.log(array) |
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) |
Accessing elements in a NumPy array:
Single element:
array[0, 1] |
Slicing:
array[0:2, 1:3] |
Fancy indexing allows accessing multiple array elements at once:
Using lists or arrays:
array[[0, 1], [1, 2]] |
Boolean indexing uses boolean arrays to select elements:
Condition-based:
array[array > 5] |
zeros: Creates an array filled with zeros.
np.zeros((3, 3)) # array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]) |
ones: Creates an array filled with ones.
np.ones((2, 2)) # array([[1., 1.], [1., 1.]]) |
full: Creates an array filled with a specified value.
np.full((2, 2), 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(2, 3) # 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(0, 10, (3, 3)) # array([[5, 0, 3], [3, 7, 9], [3, 5, 2]]) |
linspace: Creates an array of evenly spaced values over a specified interval.
np.linspace(0, 10, 5) # [0. , 0.25, 0.5 , 0.75, 1. ] |
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) |
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) |
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) |
Broadcasting allows NumPy to perform element-wise operations on arrays of different shapes:
Example:
array1 = np.array([1, 2, 3]) array2 = np.array([[1], [2], [3]]) result = array1 + array2 |
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 changes the shape of an array without altering its data:
Reshape:
array = np.arange(6).reshape((2, 3)) # array([[0, 1, 2], [3, 4, 5]]) |
Flatten:
array = array.flatten() # array([0, 1, 2, 3, 4, 5]) |
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 |
Concatenation:
concatenated = np.concatenate((a, b), axis=0) |
Stacking:
stacked = np.vstack((a, b)) |
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. |
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.