gpt4 book ai didi

python - 获取 numpy.ndarray 的索引

转载 作者:太空狗 更新时间:2023-10-29 22:23:22 24 4
gpt4 key购买 nike

我有一个 numpy.ndarray 类型的一维数组,我想知道它的最大条目的索引。找到最大值后,我使用

peakIndex = numpy.where(myArray==max)

找到峰的索引。但是我的脚本吐出的不是索引,而是

peakIndex = (array([1293]),)

我希望我的代码只输出整数 1293。我怎样才能清理输出?

最佳答案

除了使用 numpy.where,您还可以使用 numpy.argmax .

peakIndex = numpy.argmax(myArray)

numpy.argmax 返回单个数字,即第一次出现的最大值的扁平化索引。如果 myArray 是多维的,您可能希望将展平索引转换为索引元组:

peakIndexTuple = numpy.unravel_index(numpy.argmax(myArray), myArray.shape)

关于python - 获取 numpy.ndarray 的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11377028/

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