从多维numpy数组中查找和删除

编程入门 行业动态 更新时间:2024-10-28 04:23:07
本文介绍了从多维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数组中查找和删除

本文发布于:2023-06-09 06:28:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/596846.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多维   组中   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!