通过一列对Python矩阵进行排序 [英] Python Matrix sorting via one column

查看:966
本文介绍了通过一列对Python矩阵进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 n x 2 整数矩阵.第一列是系列0,1,-1,2,-2,但是这些顺序是从它们的组成矩阵编译而成的.第二列是另一个列表的索引列表.

I have a n x 2 matrix of integers. The first column is a series 0,1,-1,2,-2, however these are in the order that they were compiled in from their constituent matrices. The second column is a list of indices from another list.

我想通过第二列对矩阵进行排序.这等同于在Excel中选择两列数据,然后通过B列(数据在A列和B列中)进行排序.请记住,每行第一列中的相邻数据应与相应的第二列对应物保持一致.我使用以下方法查看了解决方案:

I would like to sort the matrix via this second column. This would be equivalent to selecting two columns of data in Excel, and sorting via Column B (where the data is in columns A and B). Keep in mind, the adjacent data in the first column of each row should be kept with its respective second column counterpart. I have looked at solutions using the following:

data[np.argsort(data[:, 0])]

但这似乎不起作用.有问题的矩阵如下所示:

But this does not seem to work. The matrix in question looks like this:

matrix([[1, 1],
        [1, 3],
        [1, 7],
        ..., 
        [2, 1021],
        [2, 1040],
        [2, 1052]])

推荐答案

您可以使用

numpy.lexsort(keys,axis = -1)

numpy.lexsort(keys, axis=-1)

使用一系列键执行间接排序.

Perform an indirect sort using a sequence of keys.

给出多个排序键,这些键可以解释为 电子表格中,lexsort返回一个整数索引数组,该整数索引 描述多列的排序顺序.

Given multiple sorting keys, which can be interpreted as columns in a spreadsheet, lexsort returns an array of integer indices that describes the sort order by multiple columns.


In [13]: data = np.matrix(np.arange(10)[::-1].reshape(-1,2))

In [14]: data
Out[14]: 
matrix([[9, 8],
        [7, 6],
        [5, 4],
        [3, 2],
        [1, 0]])

In [15]: temp = data.view(np.ndarray)

In [16]: np.lexsort((temp[:, 1], ))
Out[16]: array([4, 3, 2, 1, 0])

In [17]: temp[np.lexsort((temp[:, 1], ))]
Out[17]: 
array([[1, 0],
       [3, 2],
       [5, 4],
       [7, 6],
       [9, 8]])

请注意,如果您将多个键传递给np.lexsort,则 last 键是主键.倒数第二个键是第二个键,依此类推.

Note if you pass more than one key to np.lexsort, the last key is the primary key. The next to last key is the second key, and so on.

使用上面显示的np.lexsort要求使用临时数组,因为np.lexsort在numpy矩阵上不起作用.自从 temp = data.view(np.ndarray)创建一个视图,而不是data的副本,它不需要太多的额外内存.但是,

Using np.lexsort as I show above requires the use of a temporary array because np.lexsort does not work on numpy matrices. Since temp = data.view(np.ndarray) creates a view, rather than a copy of data, it does not require much extra memory. However,

temp[np.lexsort((temp[:, 1], ))]

是一个新的数组,它确实需要更多的内存.

is a new array, which does require more memory.

还有一种方法可对列就地进行排序.想法是将数组视为具有两列的结构化数组.与普通ndarray不同,结构化数组具有sort方法,该方法允许您将列指定为键:

There is also a way to sort by columns in-place. The idea is to view the array as a structured array with two columns. Unlike plain ndarrays, structured arrays have a sort method which allows you to specify columns as keys:

In [65]: data.dtype
Out[65]: dtype('int32')

In [66]: temp2 = data.ravel().view('int32, int32')

In [67]: temp2.sort(order = ['f1', 'f0'])

请注意,由于temp2data view ,因此不需要分配新内存和复制数组.另外,排序temp2会同时修改data:

Notice that since temp2 is a view of data, it does not require allocating new memory and copying the array. Also, sorting temp2 modifies data at the same time:

In [69]: data
Out[69]: 
matrix([[1, 0],
        [3, 2],
        [5, 4],
        [7, 6],
        [9, 8]])

这篇关于通过一列对Python矩阵进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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