按多个轴对 2D numpy 数组进行排序 [英] Sorting a 2D numpy array by multiple axes

查看:37
本文介绍了按多个轴对 2D numpy 数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形状为 (N,2) 的二维 numpy 数组,它包含 N 个点(x 和 y 坐标).例如:

I have a 2D numpy array of shape (N,2) which is holding N points (x and y coordinates). For example:

array([[3, 2],
       [6, 2],
       [3, 6],
       [3, 4],
       [5, 3]])

我想对其进行排序,以便我的点按 x 坐标排序,然后在 x 坐标相同的情况下按 y 排序.所以上面的数组应该是这样的:

I'd like to sort it such that my points are ordered by x-coordinate, and then by y in cases where the x coordinate is the same. So the array above should look like this:

array([[3, 2],
       [3, 4],
       [3, 6],
       [5, 3],
       [6, 2]])

如果这是一个普通的 Python 列表,我会简单地定义一个比较器来做我想做的事,但据我所知,numpy 的排序函数不接受用户定义的比较器.有什么想法吗?

If this was a normal Python list, I would simply define a comparator to do what I want, but as far as I can tell, numpy's sort function doesn't accept user-defined comparators. Any ideas?

感谢您的想法!我建立了一个包含 1000000 个随机整数点的快速测试用例,并对我可以运行的测试用例进行了基准测试(抱歉,目前无法升级 numpy).

Thanks for the ideas! I set up a quick test case with 1000000 random integer points, and benchmarked the ones that I could run (sorry, can't upgrade numpy at the moment).

Mine:   4.078 secs 
mtrw:   7.046 secs
unutbu: 0.453 secs

推荐答案

使用 词法排序:

import numpy as np    
a = np.array([(3, 2), (6, 2), (3, 6), (3, 4), (5, 3)])

ind = np.lexsort((a[:,1],a[:,0]))    

a[ind]
# array([[3, 2],
#       [3, 4],
#       [3, 6],
#       [5, 3],
#       [6, 2]])

<小时>如果 aC_CONTIGUOUS

a.ravel() 返回一个视图.如果这是真的,@ars 的方法,通过使用 ravel 而不是 flatten 稍作修改, 产生一种很好的方式来排序 a in-place:


a.ravel() returns a view if a is C_CONTIGUOUS. If that is true, @ars's method, slightly modifed by using ravel instead of flatten, yields a nice way to sort a in-place:

a = np.array([(3, 2), (6, 2), (3, 6), (3, 4), (5, 3)])
dt = [('col1', a.dtype),('col2', a.dtype)]
assert a.flags['C_CONTIGUOUS']
b = a.ravel().view(dt)
b.sort(order=['col1','col2'])

由于 ba 的一个视图,所以对 b 排序也对 a 排序:

Since b is a view of a, sorting b sorts a as well:

print(a)
# [[3 2]
#  [3 4]
#  [3 6]
#  [5 3]
#  [6 2]]

这篇关于按多个轴对 2D numpy 数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆