Python 按秩排序
我想对我的2D数组进行排序,以匹配另一个数组的秩 我尝试了排序和排序功能,但它不起作用。我不确定我是否格式化错误,或者最好的方式是什么Python 按秩排序,python,numpy,sorting,Python,Numpy,Sorting,我想对我的2D数组进行排序,以匹配另一个数组的秩 我尝试了排序和排序功能,但它不起作用。我不确定我是否格式化错误,或者最好的方式是什么 array = (32,96) ranked = [57, 23, 68, 58, 25, 91, 70, 83, 77, 75, 89, 34, 49, 79, 66, 54, 67, 44, 63, 52, 46, 20, 64, 10, 80, 33, 30, 29, 28, 26, 17, 27, 50, 51, 92, 8
array = (32,96)
ranked = [57, 23, 68, 58, 25, 91, 70, 83, 77, 75, 89, 34, 49, 79, 66, 54, 67,
44, 63, 52, 46, 20, 64, 10, 80, 33, 30, 29, 28, 26, 17, 27, 50, 51,
92, 86, 69, 47, 0, 7, 3, 85, 18, 11, 13, 53, 8, 78, 82, 81, 14,
74, 59, 32, 42, 39, 1, 31, 36, 19, 24, 5, 38, 9, 73, 71, 76, 87,
41, 55, 94, 93, 84, 16, 90, 62, 48, 43, 72, 95, 65, 45, 61, 22, 21,
15, 37, 88, 2, 40, 56, 6, 12, 60, 4, 35]
sortedarray = sorted(array,ranked)
对于上面的代码,我得到一个错误:
TypeError: 'list' object is not callable
ranked有96个值,对应于原始数组中的第二个维度。要对数组的第二个维度进行排序,您可以首先创建一个形状为32、96的空sortedarray,然后按照ranked指定的顺序填充新数组。以下是对数组使用随机值的代码:
array[:,ranked]不能完成这项工作吗?我试过了,但它似乎没有对array的其他维度进行排序。如果可以,请向我们展示一个最小的样本和预期的输出。
array = np.random.random((32,96))
ranked = [57, 23, 68, 58, 25, 91, 70, 83, 77, 75, 89, 34, 49, 79, 66, 54, 67,
44, 63, 52, 46, 20, 64, 10, 80, 33, 30, 29, 28, 26, 17, 27, 50, 51,
92, 86, 69, 47, 0, 7, 3, 85, 18, 11, 13, 53, 8, 78, 82, 81, 14,
74, 59, 32, 42, 39, 1, 31, 36, 19, 24, 5, 38, 9, 73, 71, 76, 87,
41, 55, 94, 93, 84, 16, 90, 62, 48, 43, 72, 95, 65, 45, 61, 22, 21,
15, 37, 88, 2, 40, 56, 6, 12, 60, 4, 35]
sortedarray = np.zeros(np.shape(array))
sortedarray[:, ranked] = array