找到并从多维数组numpy的删除 [英] find and delete from more-dimensional numpy array
本文介绍了找到并从多维数组numpy的删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个numpy的阵列:
I have two numpy-arrays:
p_a_colors=np.array([[0,0,0],
[0,2,0],
[119,103,82],
[122,122,122],
[122,122,122],
[3,2,4]])
p_rem = np.array([[119,103,82],
[122,122,122]])
我想从在p_rem p_a_colors删除所有列,所以我得到:
I want to delete all the columns from p_a_colors that are in p_rem, so I get:
p_r_colors=np.array([[0,0,0],
[0,2,0],
[3,2,4]])
我想,事情应该像
I think, something should work like
p_r_colors= np.delete(p_a_colors, np.where(np.all(p_a_colors==p_rem, axis=0)),0)
但我只是不明白轴或[:]权
but I just don't get the axis or [:] right.
我知道,
p_r_colors=copy.deepcopy(p_a_colors)
for i in range(len(p_rem)):
p_r_colors= np.delete(p_r_colors, np.where(np.all(p_r_colors==p_rem[i], axis=-1)),0)
会的工作,但我试图避免(Python)的循环,因为我也想表现的权利。
would work, but I am trying to avoid (python)loops, because I also want the performance right.
推荐答案
这是我会怎么做:
dtype = np.dtype((np.void, (p_a_colors.shape[1] *
p_a_colors.dtype.itemsize)))
mask = np.in1d(p_a_colors.view(dtype), p_rem.view(dtype))
p_r_colors = p_a_colors[~mask]
>>> p_r_colors
array([[0, 0, 0],
[0, 2, 0],
[3, 2, 4]])
您需要做的空白DTYPE的事情,这样numpy的行比作一个整体。之后,使用内置的组例程似乎是显而易见的路要走。
You need to do the void dtype thing so that numpy compares rows as a whole. After that using the built-in set routines seems like the obvious way to go.
这篇关于找到并从多维数组numpy的删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文