在NumPy中,可以使用numpy.argsort()
函数按照指定轴的值对2D NumPy数组进行排序。要按照和最大的列对数组进行排序,可以按照以下步骤进行操作:
import numpy as np
arr = np.array([[5, 2, 9], [1, 7, 3], [4, 6, 8]])
column_sums = np.sum(arr, axis=0)
numpy.argsort()
函数获取按和最大的列的排序索引:sorted_indices = np.argsort(column_sums)[::-1]
sorted_arr = arr[:, sorted_indices]
完整的代码示例如下:
import numpy as np
arr = np.array([[5, 2, 9], [1, 7, 3], [4, 6, 8]])
column_sums = np.sum(arr, axis=0)
sorted_indices = np.argsort(column_sums)[::-1]
sorted_arr = arr[:, sorted_indices]
print(sorted_arr)
输出结果为:
[[9 2 5]
[3 7 1]
[8 6 4]]
这里的sorted_arr
就是按照和最大的列进行排序后的2D NumPy数组。
对于腾讯云相关产品和产品介绍链接地址,由于要求不能提及具体的云计算品牌商,无法给出相关链接。但可以根据需要在腾讯云官方网站上搜索相关产品和服务,以获取更多信息。
领取专属 10元无门槛券
手把手带您无忧上云