Hello! We'll explore a concept called Boolean Indexing in NumPy
today. It lets us access array elements based on conditions instead of explicit indices.
To illustrate, let's create a NumPy
array, data
:
Python1import numpy as np 2 3data = np.array([12, 43, 36, 32, 51, 18, 79, 7]) 4print("Data: ", data) # Data: [12 43 36 32 51 18 79 7]
Now, suppose we want to extract elements greater than 30. We form a Boolean array checking this condition for data
:
Python1bool_array = data > 30 2print("Boolean Array: ", bool_array) # Boolean Array: [False True True True True False True False]
To extract the elements that satisfy our condition from data
, we use the bool_array
as an index:
Python1filtered_data = data[bool_array] 2print("Filtered Data: ", filtered_data) # Filtered Data: [43 36 32 51 79]
Now filtered_data
only holds values from data
that are greater than 30.
We can also use Boolean conditions to combine multiple criteria using logical operators like &
for AND and |
for OR.
Consider an array of prices per unit for different products in a retail store:
Python1prices = np.array([15, 30, 45, 10, 20, 35, 50]) 2print("Prices: ", prices) # Prices: [15 30 45 10 20 35 50]
Suppose we want to find prices that are greater than 20 and less than 40. We can combine conditions using the &
operator:
Python1filtered_prices = prices[(prices > 20) & (prices < 40)] 2print("Filtered Prices (20 < price < 40): ", filtered_prices) 3# Filtered Prices (20 < price < 40): [30 35]
Now, let's consider finding prices that are either less than 15 or greater than 45 using the |
operator:
Python1filtered_prices_or = prices[(prices < 15) | (prices > 45)] 2print("Filtered Prices (price < 15 OR price > 45): ", filtered_prices_or) 3# Filtered Prices (price < 15 OR price > 45): [10 50]
Using these logical operators, you can create complex filtering conditions to extract exactly the data you need.
Next, let's learn about Fancy Indexing in NumPy
, a tool for accessing multiple non-adjacent array items. We pass an array of indices to select these items.
Consider an array of seven numbers:
Python1data = np.array([11, 22, 33, 44, 55, 66, 77]) 2print("Data: ", data) # Data: [11 22 33 44 55 66 77]
We fetch the 1st, 3rd, and 5th elements together:
Python1fancy_indexes = np.array([0, 2, 4]) 2fancy_data = data[fancy_indexes] 3print("Data from Fancy Indexing: ", fancy_data) # Data from Fancy Indexing: [11 33 55]
Let's consider a practical use of Fancy Indexing. Given an array representing people's ages, we want to fetch the ages of person 2, person 5, and person 7:
Python1ages = np.array([15, 22, 27, 35, 41, 56, 63, 74, 81]) 2print("Initial Ages Array: ", ages) # Initial Ages Array: [15 22 27 35 41 56 63 74 81] 3 4indexes = np.array([1, 4, 6]) # Indices of values of interest 5fetched_ages = ages[indexes] 6print("Fetched Ages: ", fetched_ages) # Fetched Ages: [22 41 63]
We've covered Boolean Indexing and Fancy Indexing in NumPy
in today's session, moving from the basics to a more powerful, combined application of the methods.
Now it's your turn to put these techniques into practice. The following exercises will allow you to refine your skills and solidify the concepts learned. Happy coding!