如何根据条件过滤二维 NumPy 数组?
在本文中,我们将看到如何在 NumPy 二维数组中按给定条件应用过滤器。我们必须获得所需元素的输出,即无论我们想从现有数组还是新数组中过滤元素。
在这里,我们将在 numpy 中创建一个二维数组。
Python3
import numpy as np
# 2-D Array also called as arrays
# with rank 2
np_2d_arr = np.array([[1, 2, 3], [4, 5, 6]])
# View the 2-D Array A2
print(np_2d_arr)
Python3
import numpy as np
arr = np.asarray([[1, 'one'], [2, 'two'], [3, 'three'],
[4, 'four'], [5, 'five']])
fltr = np.asarray(['two', 'four'])
arr[np.in1d(arr[:, 1], fltr)]
Python3
import numpy as np
a = np.arange(12).reshape((3, 4))
print(a[:, np.all(a < 10, axis = 0)])
Python3
import numpy as np
a = np.arange(12).reshape((3, 4))
print(a[:, np.any(a < 2, axis = 0)])
输出:
[[1 2 3]
[4 5 6]]
现在让我们看一些在 NumPy 二维数组中通过给定条件应用过滤器的示例。
示例 1:使用 np.asarray() 方法
在这个例子中,我们使用 np.asarray() 方法,解释如下:
Syntax : numpy.asarray(arr, dtype=None, order=None)
Parameters :
- arr : [array_like] Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.
- dtype : [data-type, optional] By default, the data-type is inferred from the input data.
- order : Whether to use row-major (C-style) or column-major (Fortran-style) memory representation. Defaults to ‘C’.
Return : [ndarray] Array interpretation of arr. No copy is performed if the input is already ndarray with matching dtype and order. If arr is a subclass of ndarray, a base class ndarray is returned.
在这里,我们首先创建一个 numpy 数组和一个过滤器,其值要被过滤。为了过滤,我们在 numpy.in1d() 方法中使用了这个 fltr 并将其作为它的值存储在原始数组中,如果条件满足则返回 True。
蟒蛇3
import numpy as np
arr = np.asarray([[1, 'one'], [2, 'two'], [3, 'three'],
[4, 'four'], [5, 'five']])
fltr = np.asarray(['two', 'four'])
arr[np.in1d(arr[:, 1], fltr)]
输出:
array([['2', 'two'],
['4', 'four']], dtype='
示例 2:使用 numpy.all() 方法
在这个例子中,我们使用 np.all() 方法,解释如下:
The numpy.all() function tests whether all array elements along the mentioned axis evaluate to True.
Syntax: numpy.all(array, axis = None, out = None, keepdims = class numpy._globals._NoValue at 0x40ba726c)
Parameters :
- Array :[array_like]Input array or object whose elements, we need to test.
- axis : [int or tuple of ints, optional]Axis along which array elements are evaluated.
- out : [ndarray, optional]Output array with same dimensions as Input array, placed with result
- keepdmis : [boolean, optional]If this is set to True.
Return : A new Boolean array as per ‘out’ parameter
在这里,我们首先使用 np.arrange() 和 reshape() 方法创建一个 numpy 数组。为了过滤,我们在要过滤的索引位置使用了条件。如果所有值都满足条件,则 np.all() 方法返回 True。此返回值与原始数组映射以提供过滤后的值。
蟒蛇3
import numpy as np
a = np.arange(12).reshape((3, 4))
print(a[:, np.all(a < 10, axis = 0)])
输出:
[[0 1]
[4 5]
[8 9]]
示例 3:使用 numpy.any() 方法
在这个例子中,我们使用 np.any() 方法,解释如下:
The numpy.any() function tests whether any array elements along the mentioned axis evaluate to True.
Syntax : numpy.any(a, axis = None, out = None, keepdims = class numpy._globals._NoValue at 0x40ba726c)
Parameters :
- array :[array_like]Input array or object whose elements, we need to test.
- axis : [int or tuple of ints, optional]Axis along which array elements are evaluated.
- out : [ndarray, optional]Output array with same dimensions as Input array, placed with result
- keepdmis : [boolean, optional]If this is set to True.
Return : A new Boolean array as per ‘out’ parameter
在这里,我们首先使用 np.arrange() 和 reshape() 方法创建一个 numpy 数组。为了过滤,我们在要过滤的索引位置使用了条件。如果任何值满足条件,则 np.any() 方法返回 true。此返回值与原始数组映射以提供过滤后的值。
蟒蛇3
import numpy as np
a = np.arange(12).reshape((3, 4))
print(a[:, np.any(a < 2, axis = 0)])
输出:
[[0 1]
[4 5]
[8 9]]