NumPy: Transforming Data

1. Filtering and Masking

    1.1 🎯 What is Filtering?
    1. Filtering means selecting only the values that match a condition. Example: From [1, 2, 3, 4, 5], select only values greater than 2. Result: [3 4 5]
    1.2 🎭 What is a Mask?
    1. A mask is a list of True and False values. NumPy uses the mask to decide which values to keep. arr = np.array([1, 3, 4, 5])mask = arr > 2 print(mask)Output: [False True True True] Then we can apply the mask: filtered_arr = arr[mask] print(filtered_arr)Output: [3 4 5] 📌 Simple idea: True means keep the value. False means ignore the value. 📌 This is the same idea as Python filter() — but instead of checking one item at a time, NumPy checks the whole array at once.
NumPy Filtering and Masking
NumPy Filtering and Masking
    1.3 🔗 Combining Conditions
    1. We can combine conditions using: & — Both conditions must be true | — At least one condition must be true ~ — Reverse the condition ⚠️ In NumPy, use &, |, and ~ instead of Python and, or, and not.
    1.4 💻 Examples
    1. import numpy as nparr = np.array([1, 2, 3, 4, 5])Example 1: Values greater than 2 mask = arr > 2 filtered_arr = arr[mask]print(filtered_arr)Output: [3 4 5] Example 2: Values greater than 2 and less than 5 mask = (arr > 2) & (arr < 5) filtered_arr = arr[mask]print(filtered_arr)Output: [3 4] Example 3: Select odd numbers mask = arr % 2 == 0 odd_numbers = arr[~mask]print(odd_numbers)Output: [1 3 5] Example 4: Modify values using condition arr[arr <= 2] = -1print(arr)Output: [-1 -1 3 4 5] 📌 Simple idea: Filtering selects values. Masking can also help modify values.

2. np.where() for Conditional Logic

    2.1 🤔 What is np.where()?
    1. np.where() helps us apply if-else logic on NumPy arrays. It can be used in two ways: 1. Find indices where a condition is true 2. Return different values based on a condition
    2.2 Syntax
    1. np.where(condition, value_if_true, value_if_false) Interpretation: 👉 If condition is True, use first value. 👉 Otherwise, use second value.
    2.3 💻 Examples
    1. import numpy as nparr = np.array([1, 2, 3, 4, 5])Example 1: Find index positions result = np.where(arr > 3)print(result)Output: (array([3, 4]),) 📌 This means values greater than 3 are found at index 3 and index 4. Example 2: Create categories category = np.where(arr > 3, "High", "Low")print(category)Output: ['Low' 'Low' 'Low' 'High' 'High'] Example 3: Modify values conditionally 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] 📌 Simple idea: np.where() is like applying if-else to every value in an array.

3. np.select() for Multiple Conditions

    3.1 🧭 What is np.select()?
    1. np.select() is used when we have multiple conditions. It works like an if → elif → elif → else chain.
    3.2 Syntax
    1. np.select(condition_list, choice_list, default=value)condition_list — List of conditions choice_list — Output for each condition default — Output when no condition matches
    3.3 💻 Example
    1. import numpy as nparr = 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, default="Invalid")print(result)Output: ['Invalid' 'Medium' 'Medium' 'High' 'High'] 📌 Simple idea: Use np.where() for simple if-else. Use np.select() for multiple conditions.
NumPy np.select() vs np.where()
NumPy: np.select() vs np.where()

4. Reshape, Resize, Flatten, Ravel, and Transpose

    4.1 🔄 Why Transform Array Shape?
    1. Sometimes we need to change the shape of data. Example: [1 2 3 4 5 6] can be changed into: [[1 2 3] [4 5 6]] Shape transformation helps us prepare data for analysis, visualization, and Machine Learning.
4.2 Quick Comparison
FunctionWhat It Does
reshape()Changes shape if total elements match
resize()Returns an array with the given shape and may repeat or remove values
flatten()Converts to 1D copy
ravel()Converts to 1D view when possible
transpose() / .TSwaps rows and columns
    4.3 reshape()
    1. reshape() changes the shape of an array without changing the data. The new shape must have the same total number of elements. Example import numpy as nparr = 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]] Original array remains unchanged: print(arr)Output: [[1 2] [3 4] [5 6]] Invalid reshape print(np.reshape(arr, (4, 2))) This gives an error because the original array has 6 elements, but shape (4, 2) needs 8 elements. Using -1 NumPy can automatically calculate one missing dimension using -1. print(np.reshape(arr, (-1, 2)))Output: [[1 2] [3 4] [5 6]] Flatten using reshape: print(np.reshape(arr, (-1,)))Output: [1 2 3 4 5 6] 📌 Simple idea: reshape() changes the layout only when the number of elements fits.
NumPy reshape() illustration
NumPy reshape() illustration
    4.4 resize()
    1. np.resize() returns a new array with the requested shape. If the new size is larger, NumPy repeats values. If the new size is smaller, NumPy removes extra values. Example import numpy as nparr = np.array([[1, 2], [3, 4], [5, 6]]) Resize to larger shape: resized_arr = np.resize(arr, (2, 4))print(resized_arr)Output: [[1 2 3 4] [5 6 1 2]] Resize to same number of elements: resized_arr = np.resize(arr, (2, 3))print(resized_arr)Output: [[1 2 3] [4 5 6]] Resize to smaller shape: resized_arr = np.resize(arr, (2, 2))print(resized_arr)Output: [[1 2] [3 4]] 📌 Simple idea: resize() can repeat or remove values to fit the new shape. 📌 Think back to the egg tray — reshape() rearranges the same eggs into a different tray shape. resize() can also add or remove eggs to fit the new tray.
NumPy resize() illustration
NumPy resize() illustration
    4.5 flatten()
    1. flatten() converts a multi-dimensional array into a 1D array. It returns a copy. Example import numpy as nparr = np.array([[1, 2], [3, 4], [5, 6]])flattened_arr = arr.flatten()print(flattened_arr)Output: [1 2 3 4 5 6] Original array remains unchanged: print(arr)Output: [[1 2] [3 4] [5 6]] 📌 Simple idea: flatten() gives a separate 1D copy.
    4.6 ravel()
    1. ravel() also converts an array into 1D. It usually returns a view, meaning changes may affect the original array. Example import numpy as nparr = np.array([[1, 2], [3, 4], [5, 6]])raveled_arr = np.ravel(arr)print(raveled_arr)Output: [1 2 3 4 5 6] Modify raveled array: raveled_arr[0] = 99print(arr)Output: [[99 2] [ 3 4] [ 5 6]] 📌 ravel() can be connected to the original array.
NumPy flatten() vs ravel()
NumPy: flatten() vs ravel()
    4.7 transpose()
    1. transpose() swaps rows and columns. Example import numpy as nparr = np.array([[1, 2], [3, 4], [5, 6]])transposed_arr = np.transpose(arr)print(transposed_arr)Output: [[1 3 5] [2 4 6]] Shortcut: print(arr.T) 📌 Simple idea: Transpose changes rows into columns and columns into rows.
NumPy transpose() illustration
NumPy transpose() illustration

5. Merging Arrays

    5.1 🧩 Why Merge Arrays?
    1. Merging means combining multiple arrays into one. We can combine arrays: 👉 Vertically, by adding rows 👉 Horizontally, by adding columns
5.2 Quick Comparison
FunctionMeaning
np.concatenate()Combines arrays using selected axis
np.vstack()Stacks arrays vertically, row-wise
np.hstack()Stacks arrays horizontally, column-wise
    5.3 np.concatenate()
    1. np.concatenate() combines arrays along a selected axis. Example import numpy as nparr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]])Combine row-wise result = np.concatenate((arr1, arr2), axis=0)print(result)Output: [[1 2] [3 4] [5 6] [7 8]] Combine column-wise result = np.concatenate((arr1, arr2), axis=1)print(result)Output: [[1 2 5 6] [3 4 7 8]] 📌 Simple idea: axis=0 adds rows. axis=1 adds columns.
    5.4 np.vstack()
    1. np.vstack() stacks arrays vertically. It adds one array below another. import numpy as nparr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]])result = np.vstack((arr1, arr2))print(result)Output: [[1 2] [3 4] [5 6] [7 8]] 📌 vstack = stack boxes on top of each other.
    5.5 np.hstack()
    1. np.hstack() stacks arrays horizontally. It adds one array beside another. import numpy as nparr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]])result = np.hstack((arr1, arr2))print(result)Output: [[1 2 5 6] [3 4 7 8]] 📌 hstack = place boxes side by side.
NumPy hstack() vs vstack() for Merging
NumPy: hstack() vs vstack() for Merging

6. Copying Arrays

    6.1 📋 Assignment vs Copy
    1. Assigning one array to another variable does not create a new copy. It creates another name for the same array.
    6.2 Reference Example
    1. import numpy as nparr = np.array([1, 2, 3])arr_ref = arrarr_ref[0] = 99print(arr)Output: [99 2 3] 📌 Why did this happen? arr_ref and arr point to the same array.
    6.3 Copy Example
    1. To create an actual copy, use np.copy() or arr.copy(). Example import numpy as nparr = np.array([1, 2, 3])arr_copy = np.copy(arr)arr_copy[0] = 99print(arr) print(arr_copy)Output: [1 2 3] [99 2 3] 📌 Simple idea: Use .copy() when you want to change the new array without changing the original.

7. Linear Algebra Operations

    7.1 🧮 What is Linear Algebra in NumPy?
    1. NumPy provides functions for matrix operations. These are useful in: 👉 Machine Learning 👉 Data Science 👉 Computer graphics 👉 Statistics 📌 Determinant and inverse come from linear algebra — you do not need to master the math behind them right now to use these functions correctly.
7.2 Common Linear Algebra Operations
OperationNumPy Example
Matrix multiplicationnp.dot(a, b) or a @ b
Determinantnp.linalg.det(a)
Inversenp.linalg.inv(a)
Solve equationsnp.linalg.solve(a, b)
    7.3 Matrix Multiplication
    1. import numpy as npa = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6], [7, 8]])result = a @ bprint(result)Output: [[19 22] [43 50]]
    7.4 Determinant
    1. import numpy as npa = np.array([[1, 2], [3, 4]])det = np.linalg.det(a)print(det)Output: -2.0
    7.5 Inverse
    1. import numpy as npa = np.array([[1, 2], [3, 4]])inverse = np.linalg.inv(a)print(inverse)Output: [[-2. 1. ] [ 1.5 -0.5]]
    7.6 Solving Linear Equations
    1. Suppose we have: x + y = 52x + y = 8 We can write it as: a = [[1, 1], [2, 1]]b = [5, 8] Then solve: import numpy as npa = np.array([[1, 1], [2, 1]]) b = np.array([5, 8])solution = np.linalg.solve(a, b)print(solution)Output: [3. 2.] This means: x = 3 y = 2 📌 NumPy can solve equations quickly using matrices.
Advertisement
Ad PlaceholderSlot: 7421026683

Practice QuestionsNot started

  1. Data Cleaning

    Question 1 of 5

      Write a function clean_outlier that takes a 1D NumPy array as input. The function should:
      1. Find the first quartile (Q1) and the third quartile (Q3).
      2. Calculate the interquartile range (IQR = Q3 - Q1).
      3. Calculate the lower bound (Q1 - 1.5 * IQR).
      4. Calculate the upper bound (Q3 + 1.5 * IQR).
      5. Replace any values below the lower bound or above the upper bound with the median of the array.
      6. Return the cleaned NumPy array.
      Write a function clean_outlier_std that takes a 1D NumPy array as input. The function should:
      1. Calculate the mean and standard deviation of the array.
      2. Calculate the lower bound (mean - 2 * std).
      3. Calculate the upper bound (mean + 2 * std).
      4. Replace any values below the lower bound or above the upper bound with the median of the array.
      5. Return the cleaned NumPy array.
      Context: You have sensor data recorded over time. The sensor records invalid values as -1. Write a function clean_invalid that:
      1. Takes a 1D NumPy array as input.
      2. Find the indices of all invalid values (-1) using np.where().
      3. For each invalid value, replace it with the mean of its surrounding (the previous and next value).
      4. Return the cleaned NumPy array.
  2. Conditional: Using where and select

    Question 2 of 5

    • Write a function increment_salary() that takes a 1D NumPy array as input and returns the updated salary array.Salary Hike Rules: - Salary below 1000: Increase by 20%. - Salary from 1000 to 5000: Increase by 10%. - Salary above 5000: Increase by 5%. 📌 Use np.select() to apply the conditions and calculate the increased salaries.
    • Write a function categorize_age() that takes a 1D NumPy array as input and categorizes each age into one of the following groups: Child, Teen, Adult, Senior. Age Categories: - Child: Age less than 13 - Teen: Age from 13 to 19. - Adult: Age from 20 to 59. - Senior: Age 60 and above. 📌 Use np.select() to apply the conditions and identify age categories.
    • Create a NumPy array temp_data with the values: [15, 22, 30, 5, 18, 25]. Use np.where() to create a new array temp_category that categorizes the temperatures as follows: - "Low" for temperatures below average - "High" for temperatures above average. Expected output: ["Low", "Low", "High", "Low", "Low", "High"].
    • Create two NumPy arrays: player_names as ["Player1", "Player2", "Player3", "Player4"] player_scores as [85, 92, 78, 90] Find the names of players whose scores are above 88 and store the result in top_players.
  3. Transforming Arrays

    Question 3 of 5

    • Create a 4 x 4 NumPy array with random integer values from 0 to 255. Convert it into a 3 x 3 array using np.resize.
    • Create a 6 x 6 NumPy array with random integer values from 0 to 255. Convert it into a 4 x 9 array using np.reshape.
    • Create a 4 x 8 NumPy array with values generated from normal distribution (mean = 40, sd = 10). Transpose the array and print the result.
    • Create a 4 x 4 NumPy array with random integer values from 0 to 20. Convert it into a 1D array using each of the following methods: - np.reshape() - np.resize() - .flatten() - np.ravel()
  4. Merging Arrays

    Question 4 of 5

    • Create a 4 x 3 NumPy array with random integer values from 0 to 60. Create another 2 x 3 NumPy array with random integer values from 10 to 80. Merge the two arrays vertically using: - np.concatenate() - np.vstack()
    • Create a 3 x 4 NumPy array with random integer values from 60 to 80. Create another 3 x 2 NumPy array with random integer values from 20 to 90. Merge them horizontally using: - np.concatenate() - np.hstack()
    • Ram and Shyam are conducting a pollution survey in KTM and Pokhara cities. They recorded CO₂ and temperature readings for three days as follows: KTM Readings: [[0.5, 22], [0.3, 20], [0.7, 25]] Pokhara Readings: [[0.2, 18], [0.4, 21], [0.6, 23]] Each row represents a day, the first column represents CO₂ levels, and the second column represents temperature. Tasks: 1. Combine the arrays vertically, then find the maximum and minimum values of CO₂ and temperature considering both cities. 2. Combine the arrays horizontally, then display the CO₂ and temperature readings for second day.
    • Create two NumPy arrays as follows: arr_1 as [['Nike', 'S'], ['Nike', 'L'], ['Adidas', 'M']]. arr_2 as [[150, 1], [200, 1.2], [180, 0.9]]. Create a new array tshirt_info by combining the features from these arrays horizontally.
  5. Matrix Operations

    Question 5 of 5

    • Solve the following system of equations using NumPy: 2x + 3y + 5z = 10 4x + y + 2z = 8 x + 2y + z = 5 Method - 1 - Store the coefficients as matrix_a - Store the constants as matrix_b - Assign mat_a_inv as the inverse of matrix_a - Calculate the solution using np.dot(mat_a_inv, matrix_b) - Print the values of x, y, and z. - Calculate and display determinant of matrix_a
    • Solve same system of equations - Using np.linalg.solve() method - Print the values of x, y, and z.
Advertisement
Ad PlaceholderSlot: 5413242224