In this article, we will see various examples of numpy.where() function and how it works in python. For instance, like,
- Using numpy.where() with single condition.
- Using numpy.where() with multiple condition
- Use numpy.where() to select indexes of elements that satisfy multiple conditions
- Using numpy.where() without condition expression
In Python’s NumPy module, we can select elements with two different sequences based on conditions on the different array.
Syntax of np.where()
numpy.where(condition[, x, y])
Explanation:
- The condition returns a NumPy array of bool.
- X and Y are the arrays that are optional, which means either both are passed or not passed.
- If it is passed then it returns the elements from x and y based on the condition depending on values in the bool array.
- If x & y arguments are not passed and only condition argument is passed then it returns the indices of the elements that are True in bool numpy array.
Let’s dig in to see some examples.
Using numpy.where() with single condition
Let’s say we have two lists of the same size and a NumPy array.
arr = np.array([11, 12, 13, 14]) high_values = ['High', 'High', 'High', 'High'] low_values = ['Low', 'Low', 'Low', 'Low']
Now, we want to convert this numpy array to the array of the same size, where the values will be included from the list high_values and low_values. For instance, if the value in an array is less than 12, then replace it with the ‘low‘ and if the value in array arr is greater than 12 then replace it with the value ‘high‘.
So ultimately, the array will look like this:
['Low' 'Low' 'High' 'High']
We can also do this with for loops, but this numpy module is designed to carry out tasks like this only.
We will use numpy.where() to see the results.
arr = np.array([11, 12, 13, 14]) high_values = ['High', 'High', 'High', 'High'] low_values = ['Low', 'Low', 'Low', 'Low'] # numpy where() with condition argument result = np.where(arr > 12,['High', 'High', 'High', 'High'],['Low', 'Low', 'Low', 'Low']) print(result)
We have converted the two arrays in a single array by using the where function like evaluating based on conditions of less than 12 and greater than 12.
The first value came out low because the value in the array arr is smaller than 12. Similarly, the last values returned are high because the value in array arr is greater than 12.
Let’s see how it worked.
Numpy.where() contains three arguments.
The first argument is the numpy array which got converted to a bool array.
arr > 12 ==> [False False True True]
Then numpy.where() iterated over the bool array and for every True, it yields corresponding element from list 1 i.e. high_values and for every False, it yields corresponding element from 2nd list i.e. low_values i.e.
[False False True True] ==> [‘Low’, ‘Low’, ‘High’, ‘High’]
This is how we created a new array from the older arrays.
Using numpy.where() with multiple conditions
In the above example, we have used single conditions. Here we will see the example with multiple conditions.
arr = np.array([11, 12, 14, 15, 16, 17]) # pass condition expression only result = np.where((arr > 12) & (arr < 16),['A', 'A', 'A', 'A', 'A', 'A'],['B', 'B', 'B', 'B', 'B', 'B']) print(result)
We executed multiple conditions on the array arr and it returned a bool value. Then numpy.where() iterated over the bool array and for every True it yields corresponding element from the first list and for every False it yields the corresponding element from the 2nd list. Then constructs a new array by the values selected from both the lists based on the result of multiple conditions on numpy array arr i.e.
- Conditional expression returns true for 14 and 15 values, so they are replaced by values in list1.
- Conditional expression returns False for 11,12,16, and 17, so they are replaced by values in list2.
Now, we will pass the different values and see what the array returns.
arr = np.array([11, 12, 14, 15, 16, 17]) # pass condition expression only result = np.where((arr > 12) & (arr < 16),['A', 'B', 'C', 'D', 'E', 'F'],[1, 2, 3, 4, 5, 6])
Use np.where() to select indexes of elements that satisfy multiple conditions
We will take a new NumPy array:
arr = np.array([11, 12, 13, 14, 15, 16, 17, 15, 11, 12, 14, 15, 16, 17])
Now, we will give a condition and find the indexes of elements that satisfy that condition that is an element should be greater than 12 and less than 16. For this, we will use numpy.where(),
arr = np.array([11, 12, 13, 14, 15, 16, 17, 15, 11, 12, 14, 15, 16, 17]) # pass condition expression only result = np.where((arr > 12) & (arr < 16)) print(result)
A tuple containing an array of indexes is returned where the conditions met True in the original array.
How did it work?
Here, the condition expression is evaluated to a bool numpy array, numpy.where() is passed to it. Then where() returned a tuple of arrays for each dimension. As our array was one dimension only, so it contained an element only i.e. a new array containing the indices of elements where the value was True in bool array i.e. indexes of items from original array arr where value is between 12 & 16.
Using np.where() without any condition expression
In this case, we will directly pass the bool array as a conditional expression.
result = np.where([True, False, False],[1, 2, 4],[7, 8, 9]) print(result)
The True value is yielded from the first list and the False value is yielded from the second list when numpy.where() iterates over the bool value.
So, basically, it returns an array of elements from the first list where the condition is True and elements from a second list elsewhere.
In the conclusion, we hope that you understood this article well.