Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的排序列平均值_Python_Sorting - Fatal编程技术网

Python中的排序列平均值

Python中的排序列平均值,python,sorting,Python,Sorting,大约一年前,我用R编写了相同的函数。现在我想用Python编写相同的函数: 我创建了矩阵: 7 3 2 3 1 6 4 3 1 接下来,我对所有列进行升序排序: 3 1 1 4 3 2 7 3 6 现在我想按平均值对列进行升序排序。输出如下所示: 1 1 3 3 2 4 3 6 7 我的所有代码: import numpy as np a = np.matrix([[7,3,2],[3,1,6],[4,3,1]]) a.sort(axis=0) b = a.mean(0) p

大约一年前,我用R编写了相同的函数。现在我想用Python编写相同的函数: 我创建了矩阵:

7 3 2
3 1 6 
4 3 1 
接下来,我对所有列进行升序排序:

3 1 1 
4 3 2
7 3 6 
现在我想按平均值对列进行升序排序。输出如下所示:

1 1 3 
3 2 4 
3 6 7 
我的所有代码:

import numpy as np
a = np.matrix([[7,3,2],[3,1,6],[4,3,1]])
a.sort(axis=0)
b = a.mean(0)
print(a)
诀窍是将数组转换为数组而不是矩阵

这也可以写成:

>>> a[:,a.A.mean(0).argsort()]
matrix([[1, 1, 3],
        [3, 2, 4],
        [3, 6, 7]])
代码将是:

import numpy as np
a = np.matrix([[7,3,2],[3,1,6],[4,3,1]])
a.sort(axis=0)
print(a[:,np.argsort(np.array(a).mean(0))])
import numpy as np
a = np.matrix([[7,3,2],[3,1,6],[4,3,1]])
a.sort(axis=0)
print(a[:,np.argsort(np.array(a).mean(0))])