如何对矩阵中的列进行绝对排序(numpy)

How to do an absolute sorting of columns in a matrix (numpy)

所以我有一个矩阵:

    a = np.array([[7,-1,0,5],
                  [2,5.2,4,2],
                  [3,-2,1,4]])

我想按绝对值升序列排序。我使用了 np.sort(abs(a)) 和 sorted(a,key=abs),sorted 可能是正确的但不知道如何将它用于列。我想得到

    a = np.array([[2,-1,0,2],
                  [3,-2,1,4],
                  [7,5.2,4,5]])
                 

尝试 argsort on axis=0 then take_along_axis 将订单应用于 a:

import numpy as np

a = np.array([[7, -1, 0, 5],
              [2, 5.2, 4, 2],
              [3, -2, 1, 4]])

s = np.argsort(abs(a), axis=0)
a = np.take_along_axis(a, s, axis=0)
print(a)

a:

[[ 2.  -1.   0.   2. ]
 [ 3.  -2.   1.   4. ]
 [ 7.   5.2  4.   5. ]]