NumPy: Array Operations, Ufuncs, and Shape Manipulation
Array Operations
Arithmetic Operations
NumPy supports element-wise arithmetic operations like addition, subtraction, multiplication, and division:
import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) print(a + b) # [5 7 9] print(a - b) # [-3 -3 -3] print(a * b) # [4 10 18] print(a / b) # [0.25 0.4 0.5]
Broadcasting
Broadcasting allows operations between arrays of different shapes by stretching the smaller array to match the larger one:
import numpy as np a = np.array([[1, 2, 3], [4, 5, 6]]) b = np.array([10, 20, 30]) print(a + b) # [[11 22 33] [14 25 36]]
Aggregate Functions
NumPy provides functions to compute aggregates like sum, mean, and standard deviation:
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(np.sum(arr)) # 15 print(np.mean(arr)) # 3.0 print(np.std(arr)) # 1.4142135623730951 print(np.min(arr)) # 1 print(np.max(arr)) # 5
Element-wise Operations and Comparison
Perform operations and comparisons element by element:
import numpy as np a = np.array([1, 2, 3]) b = np.array([2, 2, 2]) print(a ** 2) # [1 4 9] print(a > b) # [False False True] print(np.equal(a, b)) # [False True False]
Dot Product, Matrix Multiplication
NumPy supports dot products and matrix multiplication:
import numpy as np a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6], [7, 8]]) print(np.dot(a, b)) # [[19 22] [43 50]] print(a @ b) # Same as np.dot, [[19 22] [43 50]]
NumPy Universal Functions (ufuncs)
Unary Functions
Unary ufuncs operate on each element of an array:
import numpy as np arr = np.array([1, 4, 9]) print(np.sqrt(arr)) # [1. 2. 3.] print(np.exp(arr)) # [2.71828183 54.59815003 8103.08392758] print(np.log(arr)) # [0. 1.38629436 2.19722458]
Binary Functions
Binary ufuncs operate on two arrays element-wise:
import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) print(np.add(a, b)) # [5 7 9] print(np.subtract(a, b)) # [-3 -3 -3] print(np.multiply(a, b)) # [4 10 18] print(np.divide(a, b)) # [0.25 0.4 0.5]
Performance Benefits
Ufuncs are implemented in C, offering significant performance benefits over Python loops. They enable vectorized operations, reducing execution time and improving efficiency for large arrays.
Shape Manipulation
reshape, transpose, T
Modify array shapes without changing data:
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) print(arr.reshape(2, 3)) # [[1 2 3] [4 5 6]] print(arr.reshape(3, 2)) # [[1 2] [3 4] [5 6]] print(arr.reshape(2, 3).transpose()) # [[1 4] [2 5] [3 6]] print(arr.reshape(2, 3).T) # Same as transpose, [[1 4] [2 5] [3 6]]
resize, swapaxes, expand_dims, squeeze
Additional shape manipulation tools:
import numpy as np arr = np.array([1, 2, 3, 4]) # Resize (modifies in place, may repeat or truncate) arr2 = np.array([1, 2, 3, 4]) arr2.resize(2, 3) print(arr2) # [[1 2 3] [4 0 0]] # Swap axes arr3 = np.array([[1, 2], [3, 4]]) print(np.swapaxes(arr3, 0, 1)) # [[1 3] [2 4]] # Expand dimensions arr4 = np.array([1, 2, 3]) print(np.expand_dims(arr4, axis=0)) # [[1 2 3]] # Squeeze (remove single-dimensional entries) arr5 = np.array([[1], [2], [3]]) print(np.squeeze(arr5)) # [1 2 3]
Flattening Arrays (flatten, ravel)
Convert multi-dimensional arrays to 1D:
import numpy as np arr = np.array([[1, 2], [3, 4]]) print(arr.flatten()) # [1 2 3 4] (returns copy) print(arr.ravel()) # [1 2 3 4] (returns view, faster)