如何使用python打印出对象中numpy数组每列的最大no ?



我有下面的numpy数组

[[7, 0, 0, 6],
[5, 6, 6, 1],
[4, 1, 6, 7],
[5, 3, 4, 7]]

我想用np找到每列的最大no。Max,然后在对象中打印结果,使输出如下所示

[7, 6, 6, 7]

如果arr是您的数组,那么您只需要使用max函数,指示所选的轴:

arr.max(axis=0)

输出:

array([7, 6, 6, 7])

如果你想要一个列表而不是numpy数组:

arr.max(axis=0).tolist()

输出:

[7, 6, 6, 7]

您可以遍历转置数组并使用np.max()查找最大值:

import numpy as np
m =np.array([[7, 0, 0, 6],
[5, 6, 6, 1],
[4, 1, 6, 7],
[5, 3, 4, 7]])
out = [np.max(i) for i in m.transpose()]
print(out)

输出:

[7, 6, 6, 7]
import numpy as np
m =np.array([[7, 0, 0, 6],
[5, 6, 6, 1],
[4, 1, 6, 7],
[5, 3, 4, 7]])

#list
max_numbers = [max(x) for x in m]
#array
max_num_array = np.array(max_numbers)

相关内容

最新更新