根据值的唯一性删除 numpy 数组的行
假设我有一个像这样的二维数组,
numpy.array(
[[0,1,1.2,3],
[1,5,3.2,4],
[3,4,2.8,4],
[2,6,2.3,5]])
我希望形成一个数组,根据最后一列值的唯一性消除整行,根据第三列值选择要保留的行。 例如,在这种情况下,我只想保留最后一列为 4 的行,并选择具有第三列次要值的行,结果如下:
array([0,1,1.2,3],
[3,4,2.8,4],
[2,6,2.3,5])
从而消除行 [1,5,3.2 ,4]
哪一个是最好的方法?
let's say I have a bi-dimensional array like that
numpy.array(
[[0,1,1.2,3],
[1,5,3.2,4],
[3,4,2.8,4],
[2,6,2.3,5]])
I want to have an array formed eliminating whole rows based on uniqueness of values of last column, selecting the row to keep based on value of third column.
e.g. in this case i would like to keep only one of the rows with 4 as last column, and choose the one which has the minor value of third column, having something like that as a result:
array([0,1,1.2,3],
[3,4,2.8,4],
[2,6,2.3,5])
thus eliminating row [1,5,3.2,4]
which would be the best way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的 numpy 已经过时了,但这应该可行:
我已经从我的声明性解决方案中大大简化了它,这变得非常笨拙。 希望这更容易理解; 我们所做的就是维护一个我们想要保留的值的字典和一个我们想要删除的索引列表。
My numpy is way out of practice, but this should work:
I've greatly simplified it from my declarative solution, which was getting quite unwieldy. Hopefully this is easier to follow; all we do is maintain a dictionary of values that we want to keep and a list of indexes we want to delete.
这可以在 Numpy 中通过组合
lexsort
和unique
来有效实现,如下所示这将返回所需的结果
This can be achieved efficiently in Numpy by combining
lexsort
andunique
as followsThis returns the desired result