At the core of NumPy are arrays. An array is a grid of values, all of the same type, and is indexed by a tuple of non - negative integers. The number of dimensions is the rank of the array, and the shape of an array is a tuple of integers giving the size of the array along each dimension.
import numpy as np
# Create a 1 - D array
a = np.array([1, 2, 3])
print("1 - D array:", a)
# Create a 2 - D array
b = np.array([[1, 2, 3], [4, 5, 6]])
print("2 - D array:", b)
NumPy supports a wide range of data types, such as integers (int8
, int16
, etc.), floating - point numbers (float32
, float64
), and booleans.
# Create an array with a specific data type
c = np.array([1, 2, 3], dtype=np.float32)
print("Array with float32 data type:", c)
The shape of an array can be accessed using the shape
attribute. You can also reshape an array using the reshape
method.
d = np.array([1, 2, 3, 4, 5, 6])
print("Original shape:", d.shape)
e = d.reshape(2, 3)
print("Reshaped array:", e)
print("New shape:", e.shape)
A NumPy cheat sheet PDF allows you to quickly look up functions and operations. For example, if you want to know how to create an array of zeros, you can find the relevant code snippet in the cheat sheet.
# Create an array of zeros
zeros_array = np.zeros((2, 3))
print("Array of zeros:", zeros_array)
You can use the cheat sheet to learn new NumPy concepts. For instance, if you are not familiar with matrix multiplication in NumPy, the cheat sheet can show you how to use the dot
function.
f = np.array([[1, 2], [3, 4]])
g = np.array([[5, 6], [7, 8]])
result = np.dot(f, g)
print("Matrix multiplication result:", result)
The cheat sheet can also help you optimize your code. It may show you more efficient ways to perform operations, such as using vectorized operations instead of loops.
# Vectorized addition
h = np.array([1, 2, 3])
i = np.array([4, 5, 6])
vectorized_sum = h + i
print("Vectorized sum:", vectorized_sum)
arange
: Create an array with evenly spaced values within a given interval.j = np.arange(0, 10, 2)
print("Array created with arange:", j)
linspace
: Create an array with evenly spaced numbers over a specified interval.k = np.linspace(0, 10, 5)
print("Array created with linspace:", k)
l = np.array([1, 2, 3])
m = l * 2
print("Element - wise multiplication:", m)
n = np.array([1, 2, 3, 4, 5])
print("Sum:", np.sum(n))
print("Mean:", np.mean(n))
print("Standard deviation:", np.std(n))
Vectorized operations are much faster than traditional Python loops. Whenever possible, use NumPy’s built - in functions for operations on arrays.
# Bad practice: Using a loop for addition
p = [1, 2, 3]
q = [4, 5, 6]
result_loop = []
for i in range(len(p)):
result_loop.append(p[i] + q[i])
print("Result using loop:", result_loop)
# Good practice: Using vectorization
r = np.array([1, 2, 3])
s = np.array([4, 5, 6])
result_vectorized = r + s
print("Result using vectorization:", result_vectorized)
Be mindful of memory usage, especially when working with large arrays. You can use data types with appropriate precision to reduce memory consumption.
# Using int8 instead of int64 for small integer values
t = np.array([1, 2, 3], dtype=np.int8)
print("Array with int8 data type to save memory:", t)
A NumPy cheat sheet PDF is an invaluable resource for both beginners and experienced Python users. It provides a quick and easy way to access NumPy functions, learn new concepts, and optimize your code. By understanding the fundamental concepts, using the cheat sheet effectively, following common practices, and adhering to best practices, you can become more proficient in using NumPy for scientific computing tasks.
This blog provides a comprehensive overview of NumPy cheat sheet PDF, covering all the essential aspects to help you make the most of this useful tool.