NumPy is a powerful numerical computing library in Python, providing support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. This cheat sheet serves as a quick reference guide for essential NumPy functionalities, covering array creation, manipulation, indexing, basic operations, linear algebra, and more.
Whether you're a beginner getting started with NumPy or an experienced user looking for a handy reference, this cheat sheet will help you efficiently perform various numerical computations using NumPy in Python.
To start using NumPy library, first, import it using the following command:
import numpy as np
After importing it, we can begin working with arrays and further operations.
NumPy provides multiple ways to create arrays, and their usage depends on the specifics of the tasks.
From Python list
We can create a NumPy array from a pre-existing list using the following function:
my_list = [1, 2, 3, 4, 5]a = np.array(my_list)print(a)
Using arange
With the following function, we can create a NumPy array from a range of numbers:
start = 1stop = 10step = 1.5b = np.arange(start, stop, step)print(b)
Using linspace
With the following function, we can create a NumPy array with evenly spaced values over a specified interval:
start = 1stop = 10num = 5c = arr = np.linspace(start, stop, num)print(c)
Using zeros
With the following function, we can create a NumPy array of a specific shape, initialized with zeros:
shape = 10d = np.zeros(shape)print(d)shape = (2,2)d = np.zeros(shape)print(d)
Using ones
With the following function, we can create a NumPy array of a specific shape, initialized with ones:
shape = 10e = np.ones(shape)print(e)shape = (2,2)e = np.ones(shape)print(e)
Using random
With the following function, we can create a NumPy array of a specific shape, initialized with random values:
shape = (2,2)f = np.random.random(size=shape)print(f)
Using full
With the following function, we can create an array initialized with a constant value:
g = np.full((2,3), 7)print(g)
Using eye
With the following function, we can create an identity matrix of our predefined dimension:
h = np.eye((2))print(h)
Using empty
With the following function, we can create an empty NumPy array:
shape = (3,2)i = np.empty(shape)print(i)
A NumPy array has multiple properties and when working with arrays, it is helpful to access them to understand the array better. Some of them are:
print(a.shape) # Print shape of arrayprint(b.ndim) # Print number of dimensionsprint(c.size) # Print number of elementsprint(d.dtype) # Print the data type of arrayprint(d.astype(int)) # Convert the array to a different datatypeprint(len(e)) # Print length of the array
There are various functions in NumPy that facilitate array indexing and slicing, providing flexibility and convenience in accessing and manipulating data stored within NumPy arrays. They are listed below:
print(e[0]) # Access element at index 0print(f[0][1]) # Access element at row 0 and column 1print(f[:1]) # Access all elements at row 0print(a[a >= 3]) # Boolean indexing with a condition to access all elements greater than or equal to 3print(a[::-1]) # Reverse the array
There are built-in functions in NumPy that are used to change the shape of an array, such as:
print(a.reshape(2,3)) # Reshape array from 1D to 2Dprint(g.ravel()) # Flatten an array from 2D to 1D
This cheat sheet covers various NumPy functionalities that are used to create arrays and understand them to further utilize them efficiently in tasks.
Free Resources