gpt4 book ai didi

python - Numpy 三列之间的最大参数项数,输出为数组

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:28 24 4
gpt4 key购买 nike

我想制定一个数组,它是 3 列之间的最大值(项目编号不是值)。

例如

In:  arr=([(1,2,3,4), (4,5,16,0), (7,8,9,2)]) # maximum of columns 0, 1, 2, 3
Out: array([2,2,1,0]) # As: 7 > 4 > 1, 8 > 5 > 2, 16 > 9 > 3, and 4 > 2 > 0

当前(非工作解决方案):

np.argmax([arr['f0'], arr['f1'], arr['f2']])

最佳答案

您可以在numpy.argmax中指定axis键,它独立地对numpy数组的指定轴进行操作。在您的情况下,您希望通过查找每列最大值的索引来单独操作每列,因此请指定 axis=0。以下是在 IPython 中给定数据的示例运行:

In [10]: import numpy as np

In [11]: arr=np.array([(1,2,3), (4,5,16), (7,8,9)])

In [12]: np.argmax(arr, axis=0)
Out[12]: array([2, 2, 1])

上面的示例是您编辑帖子之前的示例。使用编辑中的新数据,以下是运行示例:

In [13]: arr=np.array([(1,2,3,4), (4,5,16,0), (7,8,9,2)])

In [14]: np.argmax(arr, axis=0)
Out[14]: array([2, 2, 1, 0])

有关numpy.argmax的更多信息可以在这里找到:http://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html

关于python - Numpy 三列之间的最大参数项数,输出为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31395746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com