本文介绍了从多维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]])您需要执行 void 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数组中查找和删除
发布评论