如何在Python使用具有多个条件的 NumPy where() ?
在Python,NumPy 有许多库函数来创建数组,其中之一是根据另一个数组的满足条件创建数组。所述numpy.where()函数返回在输入阵列,其中所述给定条件成立的元素的索引。
句法:
numpy.where(condition[, x, y])
Parameters:
- condition : When True, yield x, otherwise yield y.
- x, y : Values from which to choose. x, y and condition need to be broadcastable to some shape.
Returns: [ndarray or tuple of ndarrays] If both x and y are specified, the output array contains elements of x where condition is True, and elements from y elsewhere.
如果给出唯一条件,则返回元组 condition.nonzero(),条件为 True 的索引。在上面的语法中,我们可以看到 where()函数可以接受两个参数,其中一个是必需的,另一个是可选的。如果条件的值为真,则将根据索引创建一个数组。
示例 1:
Numpy where() 使用逻辑 OR 具有多个条件。
Python3
# Import NumPy library
import numpy as np
# Create an array using the list
np_arr1 = np.array([23, 11, 45, 43, 60, 18,
33, 71, 52, 38])
print("The values of the input array :\n", np_arr1)
# Create another array based on the
# multiple conditions and one array
new_arr1 = np.where((np_arr1))
# Print the new array
print("The filtered values of the array :\n", new_arr1)
# Create an array using range values
np_arr2 = np.arange(40, 50)
# Create another array based on the
# multiple conditions and two arrays
new_arr2 = np.where((np_arr1), np_arr1, np_arr2)
# Print the new array
print("The filtered values of the array :\n", new_arr2)
Python3
# Import NumPy library
import numpy as np
# Create two arrays of random values
np_arr1 = np.random.rand(10)*100
np_arr2 = np.random.rand(10)*100
# Print the array values
print("\nThe values of the first array :\n", np_arr1)
print("\nThe values of the second array :\n", np_arr2)
# Create a new array based on the conditions
new_arr = np.where((np_arr1), np_arr1, np_arr2)
# Print the new array
print("\nThe filtered values of both arrays :\n", new_arr)
Python3
# Import NumPy library
import numpy as np
# Create two multidimensional arrays of
# integer values
np_arr1 = np.array([[6, 13, 22, 7, 12],
[7, 11, 16, 32, 9]])
np_arr2 = np.array([[44, 20, 8, 35, 10],
[98, 23, 42, 6, 13]])
# Print the array values
print("\nThe values of the first array :\n", np_arr1)
print("\nThe values of the second array :\n", np_arr2)
# Create a new array from two arrays based on
# the conditions
new_arr = np.where(((np_arr1 % 2 == 0) & (np_arr2 % 2 == 1)),
np_arr1, np_arr2)
# Print the new array
print("\nThe filtered values of both arrays :\n", new_arr)
输出:
示例 2:
Numpy where() 使用逻辑 AND 具有多个条件。
蟒蛇3
# Import NumPy library
import numpy as np
# Create two arrays of random values
np_arr1 = np.random.rand(10)*100
np_arr2 = np.random.rand(10)*100
# Print the array values
print("\nThe values of the first array :\n", np_arr1)
print("\nThe values of the second array :\n", np_arr2)
# Create a new array based on the conditions
new_arr = np.where((np_arr1), np_arr1, np_arr2)
# Print the new array
print("\nThe filtered values of both arrays :\n", new_arr)
输出:
示例 3:
Numpy where() 在多维数组中具有多个条件。
蟒蛇3
# Import NumPy library
import numpy as np
# Create two multidimensional arrays of
# integer values
np_arr1 = np.array([[6, 13, 22, 7, 12],
[7, 11, 16, 32, 9]])
np_arr2 = np.array([[44, 20, 8, 35, 10],
[98, 23, 42, 6, 13]])
# Print the array values
print("\nThe values of the first array :\n", np_arr1)
print("\nThe values of the second array :\n", np_arr2)
# Create a new array from two arrays based on
# the conditions
new_arr = np.where(((np_arr1 % 2 == 0) & (np_arr2 % 2 == 1)),
np_arr1, np_arr2)
# Print the new array
print("\nThe filtered values of both arrays :\n", new_arr)
输出:
结论:
NumPy 中的where()函数用于从具有多个条件的现有数组创建一个新数组。