我有以下下面的numpy矩阵,我想根据第三列值升序排序

[[  3.05706500e+06   4.98000000e+01  -2.62500070e+01  -9.38135544e+01]
 [  3.05706600e+06   4.98000000e+01  -3.00000056e+01  -9.38135544e+01]
 [  3.05706700e+06   4.98000000e+01  -3.37500042e+01  -9.38135544e+01]
 [  3.05706800e+06   4.98000000e+01  -3.75000028e+01  -9.38135544e+01]]

这是我真正想要的矩阵。
[[  3.05706800e+06   4.98000000e+01  -3.75000028e+01  -9.38135544e+01]
 [  3.05706700e+06   4.98000000e+01  -3.37500042e+01  -9.38135544e+01]
 [  3.05706600e+06   4.98000000e+01  -3.00000056e+01  -9.38135544e+01]
 [  3.05706500e+06   4.98000000e+01  -2.62500070e+01  -9.38135544e+01]]

我如何仅使用numpy做到这一点?任何帮助,将不胜感激。谢谢!

最佳答案

给定你的数组

>>> arr
array([[  3.05706500e+06,   4.98000000e+01,  -2.62500070e+01,
         -9.38135544e+01],
       [  3.05706600e+06,   4.98000000e+01,  -3.00000056e+01,
         -9.38135544e+01],
       [  3.05706700e+06,   4.98000000e+01,  -3.37500042e+01,
         -9.38135544e+01],
       [  3.05706800e+06,   4.98000000e+01,  -3.75000028e+01,
         -9.38135544e+01]])

您可以简单地将numpy.sort与axis = 0一起按需要对其进行排序
>>> numpy.sort(arr,axis=0)
array([[  3.05706500e+06,   4.98000000e+01,  -3.75000028e+01,
         -9.38135544e+01],
       [  3.05706600e+06,   4.98000000e+01,  -3.37500042e+01,
         -9.38135544e+01],
       [  3.05706700e+06,   4.98000000e+01,  -3.00000056e+01,
         -9.38135544e+01],
       [  3.05706800e+06,   4.98000000e+01,  -2.62500070e+01,
         -9.38135544e+01]])
>>>

我相信我以前的答案是错误的,因为我误解了这个问题。这是正确的答案
>>> arr[arr[:,2].argsort()]
array([[  3.05706800e+06,   4.98000000e+01,  -3.75000028e+01,
         -9.38135544e+01],
       [  3.05706700e+06,   4.98000000e+01,  -3.37500042e+01,
         -9.38135544e+01],
       [  3.05706600e+06,   4.98000000e+01,  -3.00000056e+01,
         -9.38135544e+01],
       [  3.05706500e+06,   4.98000000e+01,  -2.62500070e+01,
         -9.38135544e+01]])
>>>

关于python - 以升序对numpy矩阵行值进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10078470/

10-16 17:00