NumPy: Transforming Data
✕Filtering and Masking
- Used to select / modify elements from an array based on conditions.
- Boolean indexing creates a mask that can be applied to the array.
- Conditions can be combined using logical operators (and
&, or|,not~). arr = np.array([1, 2, 3, 4, 5])mask = (arr > 2)filtered_arr = arr[mask]print(filtered_arr)# Output: [3, 4, 5]mask_2 = (arr > 2) & (arr < 5)filtered_arr_2 = arr[mask_2]print(filtered_arr_2)# Output: [3, 4]mask_3 = (arr % 2 == 0)filtered_arr_3 = arr[~mask_3]print(filtered_arr_3)# Output: [1, 3, 5]arr[arr <= 2] = -1print(arr)# Output: [-1, -1, 3, 4, 5]
Examples:
where for Conditional Logic
- Returns indices where condition is True. (filter gave value)
- if-else can be implemented using
np.where. - Syntax:
np.where(condition, x, y) arr = np.array([1, 2, 3, 4, 5])result = np.where(arr > 3)print(result)# Output: (array([3, 4]),)cate = np.where(arr > 3, "High", "Low")print(cate)# Output: ["Low", "Low", "Low", "High", "High"]height = np.array([150, 160, 170, 180, 190])new_height = np.where(height <= 160, height + 2, height)print(new_height)# Output: [152, 162, 170, 180, 190]
Examples:
Select
- if-elif-elif-....-else can be implemented using
np.select. - Syntax:
np.select(condlist, choicelist, default=0)-condlist: List of boolean arrays or conditions. -choicelist: List of values to return for each condition. -default: Value to return when no condition is True. arr = np.array([-1, 2, 3, 4, 5])conditions = [(arr > 0) & (arr < 2), (arr >= 2) & (arr < 4), arr >= 4]choices = ["Low", "Medium", "High"]result = np.select(conditions, choices, "Invalid")print(result)# Output: ["Invalid", "Medium", "Medium", "High", "High"]
Examples:
Reshape, Resize, Flatten, Ravel, Transpose
- Changes shape (dimension) of an array without changing data.
- Returns a new array with the specified shape without modifying the original array.
- New shape must be compatible with original shape (same number of elements).
- Example:
arr = np.array([[1, 2], [3, 4], [5, 6]])reshaped_arr = np.reshape(arr, (2, 3))print(reshaped_arr)# Output: [[1, 2, 3], [4, 5, 6]]print(arr)# Original array remains unchanged: [[1, 2], [3, 4], [5, 6]]print(np.reshape(arr, (4, 2)))# Raises errorprint(np.reshape(arr, (-1,2)))# Automatically infers. Output: [[1, 2], [3, 4], [5, 6]]print(np.reshape(arr, (-1,)))# Flattens to 1D. Output: [1, 2, 3, 4, 5, 6]` - Returns a new array with the specified shape, padding or truncating as needed.
- Example:
arr = np.array([[1, 2], [3, 4], [5, 6]])resized_arr = np.resize(arr, (2, 4))print(resized_arr)# Output: [[1, 2, 3, 4], [5, 6, 1, 2]]resized_arr = np.resize(arr, (2, 3))print(resized_arr)# Output: [[1, 2, 3], [4, 5, 6]]resized_arr = np.resize(arr, (2, 2))print(resized_arr)# Output: [[1, 2], [3, 4]] - Returns a copy of the array collapsed into one dimension.
- Example:
arr = np.array([[1, 2], [3, 4], [5, 6]])flattened_arr = arr.flatten()print(flattened_arr)# Output: [1, 2, 3, 4, 5, 6]print(arr)# Original array unchanged: [[1, 2], [3, 4], [5, 6]] - Returns a flattened view of the array.
- Example:
arr = np.array([[1, 2], [3, 4], [5, 6]])raveled_arr = np.ravel(arr)print(raveled_arr)# Output: [1, 2, 3, 4, 5, 6]raveled_arr[0] = 99print(arr)# Original array modified: [[99, 2], [3, 4], [5, 6]] - Swaps rows and columns of the array.
- Example:
arr = np.array([[1, 2], [3, 4], [5, 6]])transposed_arr = np.transpose(arr)# orarr.Tprint(transposed_arr)# Output: [[1, 3, 5], [2, 4, 6]]print(arr)# Original array unchanged: [[1, 2], [3, 4], [5, 6]]
Reshape
Resize
Flatten
Ravel
Transpose
Merging Arrays: Concatenate, hstack, vstack
- Used to combine multiple arrays into one.
- Combine multiple numpy arrays along rows or columns as specified in axis.
- Example:
arr1 = np.array([[1, 2], [3, 4]])arr2 = np.array([[5, 6], [7, 8]])concatenated_arr = np.concatenate((arr1, arr2), axis=0)print(concatenated_arr)# Output: [[1, 2], [3, 4], [5, 6], [7, 8]]concatenated_arr = np.concatenate((arr1, arr2), axis=1)print(concatenated_arr)# Output: [[1, 2, 5, 6], [3, 4, 7, 8]] - Stacks arrays in sequence horizontally (column-wise).
- Example:
arr1 = np.array([[1, 2], [3, 4]])arr2 = np.array([[5, 6], [7, 8]])hstacked_arr = np.hstack((arr1, arr2))print(hstacked_arr)# Output: [[1, 2, 5, 6], [3, 4, 7, 8]] - Stacks arrays in sequence vertically (row-wise).
- Example:
arr1 = np.array([[1, 2], [3, 4]])arr2 = np.array([[5, 6], [7, 8]])vstacked_arr = np.vstack((arr1, arr2))print(vstacked_arr)# Output: [[1, 2], [3, 4], [5, 6], [7, 8]]
Concatenate
hstack
vstack
Copying Arrays
- Assigning one array to another variable creates a reference, not a copy.
- If you modify the new variable, it will affect the original array.
- To create an actual copy, use
np.copy()or thecopymethod. arr = np.array([1, 2, 3])arr_ref = arrarr_ref[0] = 99print(arr)# Output: [99, 2, 3] (original array modified)arr_copy = np.copy(arr)# orarr.copy()arr_copy[0] = 1print(arr)# Output: [99, 2, 3] (original array unchanged)print(arr_copy)# Output: [1, 2, 3] (copy modified)
Examples:
Linear Algebra Operations
- NumPy provides functions for performing linear algebra operations on arrays.
Common Linear Algebra Operations in NumPy:
| Operation | Example |
|---|---|
| Matrix Multiplication (dot product) | np.dot(a2, a2) / a2 @ a2 |
| Determinant | np.linalg.det(a2) |
| Inverse | np.linalg.inv(a2) |
| Solve Linear Equations | np.linalg.solve(a2, b) |
Examples of linear algebra operations in NumPy
