简体   繁体   English

Python(Numpy)数组排序

[英]Python (Numpy) array sorting

I've got this array, named v, of dtype('float64'): 我有一个名为v的dtype('float64')数组:

array([[  9.33350000e+05,   8.75886500e+06,   3.45765000e+02],
       [  4.33350000e+05,   8.75886500e+06,   6.19200000e+00],
       [  1.33360000e+05,   8.75886500e+06,   6.76650000e+02]])

... which I've acquired from a file by using the np.loadtxt command. ...我通过使用np.loadtxt命令从文件中获取的。 I would like to sort it after the values of the first column, without mixing up the structure that keeps the numbers listed on the same line together. 我想在第一列的值之后对其进行排序,而不会混淆将数字列在同一行上的结构。 Using v.sort(axis=0) gives me: 使用v.sort(axis = 0)给我:

array([[  1.33360000e+05,   8.75886500e+06,   6.19200000e+00],
       [  4.33350000e+05,   8.75886500e+06,   3.45765000e+02],
       [  9.33350000e+05,   8.75886500e+06,   6.76650000e+02]])

... ie places the smallest number of the third column in the first line, etc. I would rather want something like this... ...即将第三列中最小数量的第一列放在第一行等等。我宁愿想要这样的东西......

array([[  1.33360000e+05,   8.75886500e+06,   6.76650000e+02],
       [  4.33350000e+05,   8.75886500e+06,   6.19200000e+00],
       [  9.33350000e+05,   8.75886500e+06,   3.45765000e+02]])

... where the elements of each line hasn't been moved relatively to each other. ......每条线的元素没有相对移动。

Try 尝试

v[v[:,0].argsort()]

(with v being the array). v是数组)。 v[:,0] is the first column, and .argsort() returns the indices that would sort the first column. v[:,0]是第一列, .argsort()返回将对第一列进行排序的索引。 You then apply this ordering to the whole array using advanced indexing. 然后使用高级索引将此排序应用于整个数组。 Note that you get a sorte copy of the array. 请注意,您将获得该阵列的sorte副本。

The only way I know of to sort the array in place is to use a record dtype: 我知道对数组进行排序的唯一方法是使用记录dtype:

v.dtype = [("x", float), ("y", float), ("z", float)]
v.shape = v.size
v.sort(order="x")

Alternatively 另外

Try 尝试

import numpy as np

order = v[:, 0].argsort()
sorted = np.take(v, order, 0)

'order' has the order of the first row. 'order'具有第一行的顺序。 and then 'np.take' take the columns their corresponding order. 然后'np.take'将列按相应的顺序排列。

See the help of 'np.take' as 请参阅'np.take'的帮助

help(np.take)

take(a, indices, axis=None, out=None, mode='raise') Take elements from an array along an axis. take(a,indices,axis = None,out = None,mode ='raise')沿轴取一个数组中的元素。

 This function does the same thing as "fancy" indexing (indexing arrays using arrays); however, it can be easier to use if you need elements along a given axis. Parameters ---------- a : array_like The source array. indices : array_like The indices of the values to extract. axis : int, optional The axis over which to select values. By default, the flattened input array is used. out : ndarray, optional If provided, the result will be placed in this array. It should be of the appropriate shape and dtype. mode : {'raise', 'wrap', 'clip'}, optional Specifies how out-of-bounds indices will behave. * 'raise' -- raise an error (default) * 'wrap' -- wrap around * 'clip' -- clip to the range 'clip' mode means that all indices that are too large are 

replaced by the index that addresses the last element along that axis. 由追踪该轴的最后一个元素的索引替换。 Note that this disables indexing with negative numbers. 请注意,这会禁用带负数的索引。

 Returns ------- subarray : ndarray The returned array has the same type as `a`. See Also -------- ndarray.take : equivalent method Examples -------- >>> a = [4, 3, 5, 7, 6, 8] >>> indices = [0, 1, 4] >>> np.take(a, indices) array([4, 3, 6]) In this example if `a` is an ndarray, "fancy" indexing can be used. >>> a = np.array(a) >>> a[indices] array([4, 3, 6]) 

如果你有v[:,0]有一些相同值的实例,你想对第1,2列等进行二次排序,那么你将需要使用numpy.lexsort()numpy.sort(v, order=('col1', 'col2', etc..)但是对于order= case, v将需要是一个结构化数组。

An example application of numpy.lexsort() to sort the rows of an array and deals with ties in the first column. numpy.lexsort()一个示例应用程序,用于对数组的行进行排序并处理第一列中的关系。 Note that lexsort effectively sorts columns and starts with the last column, so you need to reverse the rows of a then take the transpose before the lexsort , and finally transpose the result (you'd have thought this should be easier, but hey!): 需要注意的是lexsort有效排序列,并与最后一列开始,所以你需要扭转的行a对前再取转lexsort ,最后转置的结果(你会认为这应该是比较容易的,但哎!) :

In [1]: import numpy as np

In [2]: a = np.array([[1,2,3,4],[1,0,4,1],[0,4,1,1]])

In [3]: a[np.lexsort(np.flip(a, axis=1).T).T]
Out[3]: 
array([[0, 4, 1, 1],
       [1, 0, 4, 1],
       [1, 2, 3, 4]])

In [4]: a
Out[4]: 
array([[1, 2, 3, 4],
       [1, 0, 4, 1],
       [0, 4, 1, 1]])

Thanks go to @Paul for the suggestion to use lexsort . 感谢@Paul提出使用lexsort的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM