๐ NumPy Quickstart Guide with a Rainman Spin ๐#
โDefinitely. Definitely Python. Definitely NumPy.โ
What is NumPy?#
NumPy is Raymond Babbittโs cheat sheet for scientific computing: fast, accurate, and reliable.
Definition
NumPy is a Python library that provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on them.
Provides multi-dimensional arrays and math functions.
Used indirectly in Pandas, Matplotlib, SciPy, and many other libraries.
Why Use NumPy?#
Definition
A NumPy array is a grid-like data structure for storing numerical data efficiently. Unlike Python lists, all elements must have the same data type.
Memory Efficient: Uses less memory than Python lists.
Fast Operations: Optimized with C for lightning-speed calculations.
Advanced Math: Functions like linear algebra, Fourier transforms, and random sampling.
How to Import NumPy#
import numpy as np
Definition
Standard alias: Using np
is the standard alias for NumPy. It ensures consistency across projects and avoids naming conflicts.
Creating and Accessing Arrays#
Definition
An array is a data structure in NumPy that stores elements in a grid-like format. Arrays can be one-dimensional, two-dimensional (matrices), or higher-dimensional (tensors).
Note
python always starts with 0 as the first index
a = np.array([1, 2, 3, 4, 5, 6]) # 1D array
print(a[0]) # Access the first element
1
Definition
Mutability: Arrays are mutable, allowing modification of their elements.
a[0] = 10
print(a) # [10, 2, 3, 4, 5, 6]
[10 2 3 4 5 6]
Reshaping Arrays#
a = np.arange(6) # Create an array [0, 1, 2, 3, 4, 5]
b = a.reshape(3, 2) # Reshape into 3x2 matrix
print(b)
[[0 1]
[2 3]
[4 5]]
Definition
Reshaping transforms the shape of an array while keeping the total number of elements constant.
Dynamic Reshaping: Rearrange data without altering values.
Array Attributes#
Tip
NumPy provides attributes like .ndim
, .shape
, .size
, and .dtype
to describe arrays.
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.ndim) # Number of dimensions
print(a.shape) # Shape (rows, columns)
print(a.size) # Total elements
print(a.dtype) # Data type
2
(2, 3)
6
int64
Mathematical Operations#
NumPy performs operations element-wise by default.
Definition
Element-wise operations apply mathematical operations to each element of an array independently.
a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])
# Addition
print(a + b) # [6, 8, 10, 12]
[ 6 8 10 12]
# Multiplication
print(a * b) # [5, 12, 21, 32]
[ 5 12 21 32]
# Sum of all elements
print(a.sum()) # 10
10
Broadcasting#
a = np.array([1, 2, 3])
print(a + 5) # Adds 5 to every element [6, 7, 8]
[6 7 8]
Definition
Broadcasting allows NumPy to perform operations on arrays with different shapes by stretching the smaller array to match the shape of the larger one.
Finding Min and Max#
data = np.array([[3, 7, 1], [4, 5, 9]])
print(np.max(data)) # Global maximum
9
print(np.min(data, axis=0)) # Min along columns
[3 5 1]
print(np.argmax(data)) # Index of max value
5
Definition
Min/Max functions (np.max
, np.min
) return the largest/smallest values in an array. Axis-based operations apply along rows or columns.
Sorting and Unique Elements#
data = np.array([3, 1, 4, 1, 5])
# Sort
print(np.sort(data)) # [1, 1, 3, 4, 5]
# Unique elements
unique, counts = np.unique(data, return_counts=True)
print(unique) # [1, 3, 4, 5]
print(counts) # [2, 1, 1, 1]
[1 1 3 4 5]
[1 3 4 5]
[2 1 1 1]
Note
Sorting organizes elements in ascending order. np.unique
finds unique elements and their counts in an array.
Preallocating Memory#
zeros = np.zeros((3, 3)) # 3x3 matrix of zeros
ones = np.ones((2, 4)) # 2x4 matrix of ones
filled = np.full((3, 3), 42) # 3x3 matrix filled with 42
Note
Preallocating memory improves performance by creating arrays with fixed sizes and default values before computation.
Linear Algebra#
Definition
Linear algebra operations, such as matrix multiplication and eigenvalue decomposition, are essential for scientific and engineering applications.
matrix = np.array([[1, 2], [3, 4]])
# Matrix Multiplication
result = np.dot(matrix, matrix)
# Eigenvalues and Eigenvectors
eigvals, eigvecs = np.linalg.eig(matrix)
Random Sampling#
Definition
Random sampling generates random values for simulations, model initialization, or probabilistic computations.
rng = np.random.default_rng()
# Random floats
print(rng.random(5)) # 5 random numbers between 0 and 1
# Random integers
print(rng.integers(10, size=(2, 3))) # 2x3 matrix of random integers
[0.59980728 0.51198625 0.69236516 0.02226763 0.58973737]
[[3 9 1]
[3 2 6]]
Saving and Loading Data#
Definition
Save and load methods enable storage of NumPy arrays in binary or text formats for reuse in other projects.
# Save and load arrays in binary format
np.save("array.npy", a)
loaded = np.load("array.npy")
# Save and load arrays in text format
np.savetxt("array.csv", a, delimiter=",")
loaded_text = np.loadtxt("array.csv", delimiter=",")
Advanced Mathematical Operations#
Definition
NumPy provides a wide range of mathematical tools for advanced computations, including exponentials, logarithms, and trigonometric functions.
x = np.array([1, 2, 3])
# Exponential and logarithms
print(np.exp(x)) # [e^1, e^2, e^3]
print(np.log(x)) # [log(1), log(2), log(3)]
[ 2.71828183 7.3890561 20.08553692]
[0. 0.69314718 1.09861229]
# Trigonometric functions
angles = np.array([0, np.pi / 2, np.pi])
print(np.sin(angles)) # Sine values
[0.0000000e+00 1.0000000e+00 1.2246468e-16]
Summary of Methods#
NumPy provides tools for:
Array creation (
np.array
,np.zeros
,np.ones
).Array reshaping (
reshape
,ravel
).Element-wise operations (
+
,*
,sum
, etc.).Statistical analysis (
mean
,std
,min
,max
).Linear algebra (
np.linalg
methods likedet
andeig
).Random sampling (
np.random
).Sorting and uniqueness (
np.sort
,np.unique
).
โYeah, definitely NumPy. Definitely the best.โ ๐