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 ~).
  • Examples:
    1. arr = np.array([1, 2, 3, 4, 5]) mask = (arr > 2) filtered_arr = arr[mask] print(filtered_arr) # Output: [3, 4, 5]
    2. mask_2 = (arr > 2) & (arr < 5) filtered_arr_2 = arr[mask_2] print(filtered_arr_2) # Output: [3, 4]
    3. mask_3 = (arr % 2 == 0) filtered_arr_3 = arr[~mask_3] print(filtered_arr_3) # Output: [1, 3, 5]
    4. arr[arr <= 2] = -1 print(arr) # Output: [-1, -1, 3, 4, 5]

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)
  • Examples:
    1. arr = np.array([1, 2, 3, 4, 5]) result = np.where(arr > 3) print(result) # Output: (array([3, 4]),)
    2. cate = np.where(arr > 3, "High", "Low") print(cate) # Output: ["Low", "Low", "Low", "High", "High"]
    3. 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]

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.
  • Examples:
    1. 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"]

Reshape, Resize, Flatten, Ravel, Transpose

  • Changes shape (dimension) of an array without changing data.
  • Reshape
    1. Returns a new array with the specified shape without modifying the original array.
    2. New shape must be compatible with original shape (same number of elements).
    3. 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 error print(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]`
    Resize
    1. Returns a new array with the specified shape, padding or truncating as needed.
    2. 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]]
    Flatten
    1. Returns a copy of the array collapsed into one dimension.
    2. 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]]
    Ravel
    1. Returns a flattened view of the array.
    2. 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]]
    Transpose
    1. Swaps rows and columns of the array.
    2. Example: arr = np.array([[1, 2], [3, 4], [5, 6]])transposed_arr = np.transpose(arr) # or arr.Tprint(transposed_arr) # Output: [[1, 3, 5], [2, 4, 6]] print(arr) # Original array unchanged: [[1, 2], [3, 4], [5, 6]]

Merging Arrays: Concatenate, hstack, vstack

  • Used to combine multiple arrays into one.
  • Concatenate
    1. Combine multiple numpy arrays along rows or columns as specified in axis.
    2. 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]]
    hstack
    1. Stacks arrays in sequence horizontally (column-wise).
    2. 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]]
    vstack
    1. Stacks arrays in sequence vertically (row-wise).
    2. 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]]

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 the copy method.
  • Examples:
    1. arr = np.array([1, 2, 3]) arr_ref = arr arr_ref[0] = 99 print(arr) # Output: [99, 2, 3] (original array modified)
    2. arr_copy = np.copy(arr) # or arr.copy() arr_copy[0] = 1 print(arr) # Output: [99, 2, 3] (original array unchanged) print(arr_copy) # Output: [1, 2, 3] (copy modified)

Linear Algebra Operations

  • NumPy provides functions for performing linear algebra operations on arrays.
Common Linear Algebra Operations in NumPy:
OperationExample
Matrix Multiplication (dot product)np.dot(a2, a2) / a2 @ a2
Determinantnp.linalg.det(a2)
Inversenp.linalg.inv(a2)
Solve Linear Equationsnp.linalg.solve(a2, b)
Examples of linear algebra operations in NumPy