numpy数组过滤lambda&np.where&np.argwhere
lambda函数
匿名函数
python 使用 lambda 来创建匿名函数。
- lambda只是一个表达式,函数体比def简单很多。
- lambda的主体是一个表达式,而不是一个代码块。仅仅能在lambda表达式中封装有限的逻辑进去。
- lambda函数拥有自己的命名空间,且不能访问自有参数列表之外或全局命名空间里的参数。
- 虽然lambda函数看起来只能写一行,却不等同于C或C++的内联函数,后者的目的是调用小函数时不占用栈内存从而增加运行效率。
以下是示例代码:
x_min, x_max, y_min, y_max, z_min, z_max = list_roi_xyz
list_pc_values = points.tolist()
points_roi = np.array(list(filter(lambda point: \
(point[0] > x_min) and (point[0] < x_max) and \
(point[1] > y_min) and (point[1] < y_max) and \
(point[2] > z_min) and (point[2] < z_max), list_pc_values)))
# list_xy_values = points_roi[:,:2].tolist()
# list_xy_values = list(map(lambda xy: [int((xy[0]-x_min)/x_grid), \
# int((xy[1]-y_min)/y_grid)], list_xy_values))
numpy.where()
numpy.where()
- numpy.where() 函数返回输入数组中满足给定条件的元素的索引。
where(condition, [x, y], /)
Return elements chosen from `x` or `y` depending on `condition`.
.. note::
When only `condition` is provided, this function is a shorthand for
``np.asarray(condition).nonzero()``. Using `nonzero` directly should be
preferred, as it behaves correctly for subclasses. The rest of this
documentation covers only the case where all three arguments are
provided.
Parameters
----------
condition : array_like, bool
Where True, yield `x`, otherwise yield `y`.
x, y : array_like
Values from which to choose. `x`, `y` and `condition` need to be
broadcastable to some shape.
Returns
-------
out : ndarray
An array with elements from `x` where `condition` is True, and elements
from `y` elsewhere.
See Also
--------
choose
nonzero : The function that is called when x and y are omitted
Notes
-----
If all the arrays are 1-D, `where` is equivalent to::
[xv if c else yv
for c, xv, yv in zip(condition, x, y)]
Examples
--------
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.where(a < 5, a, 10*a)
array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
This can be used on multidimensional arrays too:
>>> np.where([[True, False], [True, True]],
... [[1, 2], [3, 4]],
... [[9, 8], [7, 6]])
array([[1, 8],
[3, 4]])
The shapes of x, y, and the condition are broadcast together:
>>> x, y = np.ogrid[:3, :4]
>>> np.where(x < y, x, 10 + y) # both x and 10+y are broadcast
array([[10, 0, 0, 0],
[10, 11, 1, 1],
[10, 11, 12, 2]])
>>> a = np.array([[0, 1, 2],
... [0, 2, 4],
... [0, 3, 6]])
>>> np.where(a < 4, a, -1) # -1 is broadcast
array([[ 0, 1, 2],
[ 0, 2, -1],
[ 0, 3, -1]])
实例
import numpy as np
x = np.arange(9.).reshape(3, 3)
print ('我们的数组是:')
print (x)
print ( '大于 3 的元素的索引:')
y = np.where(x > 3)
print (y)
print ('使用这些索引来获取满足条件的元素:')
print (x[y])
输出结果为:
我们的数组是:
[[0. 1. 2.]
[3. 4. 5.]
[6. 7. 8.]]
大于 3 的元素的索引:
(array([1, 1, 2, 2, 2]), array([1, 2, 0, 1, 2]))
使用这些索引来获取满足条件的元素:
[4. 5. 6. 7. 8.]
idx = np.where((points[:, 0]>x_min) & (points[:, 0]<x_max) &
(points[:, 1]>y_min) & (points[:, 1]<y_max) &
(points[:, 2]>z_min) & (points[:, 2]<z_max))
points_roi = points[idx[0]]
numpy.argwhere(a)
Find the indices of array elements that are non-zero, grouped by element.
Parameters
----------
a : array_like
Input data.
Returns
-------
index_array : (N, a.ndim) ndarray
Indices of elements that are non-zero. Indices are grouped by element.
This array will have shape ``(N, a.ndim)`` where ``N`` is the number of
non-zero items.
See Also
--------
where, nonzero
Notes
-----
``np.argwhere(a)`` is almost the same as ``np.transpose(np.nonzero(a))``,
but produces a result of the correct shape for a 0D array.
The output of ``argwhere`` is not suitable for indexing arrays.
For this purpose use ``nonzero(a)`` instead.
Examples
--------
>>> x = np.arange(6).reshape(2,3)
>>> x
array([[0, 1, 2],
[3, 4, 5]])
>>> np.argwhere(x>1)
array([[0, 2],
[1, 0],
[1, 1],
[1, 2]])
idx = np.argwhere((points[:, 0]>x_min) & (points[:, 0]<x_max) &
(points[:, 1]>y_min) & (points[:, 1]<y_max) &
(points[:, 2]>z_min) & (points[:, 2]<z_max))
points_roi = points[idx.flatten()]
for循环
points_roi = []
for p in range(points.shape[0]):
if points[p,0]>x_min and points[p,0]<x_max and \
points[p,1]>y_min and points[p,1]<y_max and \
points[p,2]>z_min and points[p,2]<z_max:
points_roi.append(points[p,:])
points_roi = np.array(points_roi)