如何将 numpy 2d 数组裁剪为非零值? [英] How to crop a numpy 2d array to non-zero values?
本文介绍了如何将 numpy 2d 数组裁剪为非零值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个像这样的二维布尔 numpy 数组:
Let's say i have a 2d boolean numpy array like this:
import numpy as np
a = np.array([
[0,0,0,0,0,0],
[0,1,0,1,0,0],
[0,1,1,0,0,0],
[0,0,0,0,0,0],
], dtype=bool)
通常如何将其裁剪为包含所有 True 值的最小框(矩形、内核)?
How can i in general crop it to the smallest box (rectangle, kernel) that includes all True values?
所以在上面的例子中:
b = np.array([
[1,0,1],
[1,1,0],
], dtype=bool)
推荐答案
这是一个使用切片和 argmax
来获取边界的方案 -
Here's one with slicing and argmax
to get the bounds -
def smallestbox(a):
r = a.any(1)
if r.any():
m,n = a.shape
c = a.any(0)
out = a[r.argmax():m-r[::-1].argmax(), c.argmax():n-c[::-1].argmax()]
else:
out = np.empty((0,0),dtype=bool)
return out
样品运行 -
In [142]: a
Out[142]:
array([[False, False, False, False, False, False],
[False, True, False, True, False, False],
[False, True, True, False, False, False],
[False, False, False, False, False, False]])
In [143]: smallestbox(a)
Out[143]:
array([[ True, False, True],
[ True, True, False]])
In [144]: a[:] = 0
In [145]: smallestbox(a)
Out[145]: array([], shape=(0, 0), dtype=bool)
In [146]: a[2,2] = 1
In [147]: smallestbox(a)
Out[147]: array([[ True]])
基准测试
其他方法 -
def argwhere_app(a): # @Jörn Hees's soln
coords = np.argwhere(a)
x_min, y_min = coords.min(axis=0)
x_max, y_max = coords.max(axis=0)
return a[x_min:x_max+1, y_min:y_max+1]
不同程度稀疏性的时间(大约 10%、50% 和 90%)-
Timings for varying degrees of sparsity (approx. 10%, 50% & 90%) -
In [370]: np.random.seed(0)
...: a = np.random.rand(5000,5000)>0.1
In [371]: %timeit argwhere_app(a)
...: %timeit smallestbox(a)
1 loop, best of 3: 310 ms per loop
100 loops, best of 3: 3.19 ms per loop
In [372]: np.random.seed(0)
...: a = np.random.rand(5000,5000)>0.5
In [373]: %timeit argwhere_app(a)
...: %timeit smallestbox(a)
1 loop, best of 3: 324 ms per loop
100 loops, best of 3: 3.21 ms per loop
In [374]: np.random.seed(0)
...: a = np.random.rand(5000,5000)>0.9
In [375]: %timeit argwhere_app(a)
...: %timeit smallestbox(a)
10 loops, best of 3: 106 ms per loop
100 loops, best of 3: 3.19 ms per loop
这篇关于如何将 numpy 2d 数组裁剪为非零值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文