如何按第一列的降序排列如下数组:
dl1 = numpy.array([[ 0.02598003,1.],
[ 0.00730082,2.],
[ 0.05471569,3.],
[ 0.02599167,4.],
[ 0.0544947 ,5.],
[ 0.00753346,6.]])
网上其他地方说直接dl1.sort()会默认按第一列排序,但好像不行
黄舟2017-05-18 10:55:30
>>> a=np.array([[ 0.02598003,1.],
[ 0.00730082,2.],
[ 0.05471569,3.],
[ 0.02599167,4.],
[ 0.0544947 ,5.],
[ 0.00753346,6.]])
>>> a.sort(0)
>>> a
array([[ 0.00730082, 1. ],
[ 0.00753346, 2. ],
[ 0.02598003, 3. ],
[ 0.02599167, 4. ],
[ 0.0544947 , 5. ],
[ 0.05471569, 6. ]])
>>>
np.sort 是把各维分别排序的
如果你是要二维组的联合排序,要用np.argsort
方法
>>> a=np.array([[ 0.02598003,1.],
[ 0.00730082,2.],
[ 0.05471569,3.],
[ 0.02599167,4.],
[ 0.0544947 ,5.],
[ 0.00753346,6.]])
>>> a[a.argsort(0)[:,0]]
array([[ 0.00730082, 2. ],
[ 0.00753346, 6. ],
[ 0.02598003, 1. ],
[ 0.02599167, 4. ],
[ 0.0544947 , 5. ],
[ 0.05471569, 3. ]])
>>>
如果数据很多的话,用python内部的 sorted
会降低效率
为情所困2017-05-18 10:55:30
dl1.sort(axis=0)
ndarray.sort
的关键字参数axis
就是用来按照某列排序
axis : int, optional
Axis along which to sort. Default is -1, which means sort along the last axis.