In [50]: rects=np.random.randint(0,100,[10,4])
...: def crit_(rect):
...: x,y,w,h=rect
...: return x>10 and y>10 and w>10 and h>10
...:
In [51]: filter(crit_,rects)
Out[51]: <filter at 0x7f55a1a43ac8>
In [52]: list(_)
Out[52]:
[array([43, 57, 85, 93]),
array([30, 22, 19, 55]),
array([12, 96, 48, 40]),
array([48, 13, 37, 46]),
array([49, 15, 35, 32]),
array([81, 81, 96, 57])]
vstack
работает, но выдает предупреждение:
In [53]: np.vstack(filter(crit_,rects))
/usr/local/bin/ipython3:1: FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future.
#!/usr/bin/python3
Out[53]:
array([[43, 57, 85, 93],
[30, 22, 19, 55],
[12, 96, 48, 40],
[48, 13, 37, 46],
[49, 15, 35, 32],
[81, 81, 96, 57]])
In [55]: np.vstack(list(filter(crit_,rects)))
Out[55]:
array([[43, 57, 85, 93],
[30, 22, 19, 55],
[12, 96, 48, 40],
[48, 13, 37, 46],
[49, 15, 35, 32],
[81, 81, 96, 57]])
без фильтра / итерации:
In [60]: rects[(rects>10).all(axis=1)]
Out[60]:
array([[43, 57, 85, 93],
[30, 22, 19, 55],
[12, 96, 48, 40],
[48, 13, 37, 46],
[49, 15, 35, 32],
[81, 81, 96, 57]])