Datatiede

How to use python NumPy where() function with multiple conditions

How to use python NumPy where() function with multiple conditions
NumPy library has many functions to create the array in python. where() function is one of them to create an array from another NumPy array based on one or more conditions. Some operations can be done at the time of array creation based on the condition by using this function. It can be used without any conditional expression also. How this function can be used with multiple conditions in python is shown in this tutorial.

Syntax:

numpy.where(condition,[x,y])

where the () function can take two arguments. The first argument is mandatory, and the second argument is optional. If the value of the first argument (condition) is true, then the output will contain the array elements from the array, x otherwise from the array, y. This function will return the index values of the input array if no optional argument is used.

Use of where() function:

Different types of Boolean operators can be used to define the condition of this function. The uses of where a () function with multiple conditions are shown in this part of the tutorial.

Example -1: Use of multiple conditions with logical OR

The following example shows the use of the where() function with and without the optional argument. Here, the logical OR has used to define the condition. The first where() function has applied in a one-dimensional array that will return the array of indices of the input array where the condition will return True. The second where() function has applied in two one-dimensional arrays will retrieve the values from the first array when the condition will return True. Otherwise, it will retrieve the values from the second array.

# Import NumPy library
import numpy as np
# Create an array using the list
np_array1 = np.array([23, 11, 45, 43, 60, 18, 33, 71, 52, 38])
print("The values of the input array :\n", np_array1)
# Create another array based on the multiple conditions and one array
new_array1 = np.where((np_array1 50))
# Print the new array
print("The filtered values of the array :\n", new_array1)
# Create an array using range values
np_array2 = np.arange(40, 50)
# Create another array based on the multiple conditions and two arrays
new_array2 = np.where((np_array1 60), np_array1, np_array2)
# Print the new array
print("The filtered values of the array :\n", new_array2)

Output:

The following output will appear after executing the above script. Here, the condition has returned True for the values 23,11,18,33, and 38 of the first array. The condition has returned False for the values 45, 43, 60, 71, and 52. So, 42, 43, 44, and 48 have been added from the second array for the values 45, 43, 60, and 52. Here, 71 is out of range.

Example -2: Use of multiple conditions with logical AND

The following example shows how the () function can be used with the multiple conditions defined by logical and applied in two one-dimensional arrays. Here, two one-dimensional NumPy arrays have been created by using the rand() function. These arrays have been used in the where() function with the multiple conditions to create the new array based on the conditions. The condition will return True when the first array's value is less than 40 and the value of the second array is greater than 60. The new array has printed later.

# Import NumPy library
import numpy as np
# Create two arrays of random values
np_array1 = np.random.rand(10)*100
np_array2 = np.random.rand(10)*100
# Print the array values
print("\nThe values of the first array :\n", np_array1)
print("\nThe values of the second array :\n", np_array2)
# Create a new array based on the conditions
new_array = np.where((np_array1 60), np_array1, np_array2)
# Print the new array
print("\nThe filtered values of both arrays :\n", new_array)

Output:

The following output will appear after executing the above script. The condition has returned False for all elements. So, the returned array contains the values from the second array only.

Example-3: Use of multiple conditions in the multi-dimensional array

The following example shows how where the () function can be used with the multiple conditions defined by logical AND that will be applied in two multi-dimensional arrays. Here, two multi-dimensional arrays have been created by using lists. Next, these functions have applied in where() function to create the new array based on the condition. The condition used in the function will return True where the value of the first array is even and the value of the second array is odd; otherwise, the condition will return False.

# Import NumPy library
import numpy as np
# Create two multidimensional arrays of integer values
np_array1 = np.array([[5, 12, 21, 6, 11], [6, 10, 15, 31, 8]])
np_array2 = np.array([[43, 19, 7, 34, 9], [99, 22, 41, 5, 12]])
# Print the array values
print("\nThe values of the first array :\n", np_array1)
print("\nThe values of the second array :\n", np_array2)
# Create a new array from two arrays based on the conditions
new_array = np.where(((np_array1 % 2 == 0) & (np_array2 % 2 == 1)), np_array1, np_array2)
# Print the new array
print("\nThe filtered values of both arrays :\n", new_array)

Output:

The following output will appear after executing the above script. In the output, 43, 12, 7, 34, 9, 22, 41, 5, and 12 have added in the new array from the second array because the condition is False for these values. The first 12 value in the new array has added from the first array because the condition is True for this value only.

Conclusion:

where the () function of the NumPy library is useful for filtering the values from two arrays. Creating a new array by filtering the data from two arrays based on multiple conditions defined by logical OR and logical AND has been explained in this tutorial. I hope the readers will be able to use this function in their script properly after practicing the examples of this tutorial.

How to Change Mouse and Touchpad Settings Using Xinput in Linux
Most Linux distributions ship with “libinput” library by default to handle input events on a system. It can process input events on both Wayland and X...
Remap your mouse buttons differently for different software with X-Mouse Button Control
Maybe you need a tool that could make your mouse's control change with every application that you use. If this is the case, you can try out an applica...
Microsoft Sculpt Touch Wireless Mouse Review
I recently read about the Microsoft Sculpt Touch wireless mouse and decided to buy it. After using it for a while, I decided to share my experience wi...