Python 在Numpy数组中,如何查找值的所有坐标

Python 在Numpy数组中,如何查找值的所有坐标,python,arrays,numpy,multidimensional-array,Python,Arrays,Numpy,Multidimensional Array,如果要查找所有坐标,如何查找3D数组中最大值的坐标 到目前为止,这是我的代码,但它不起作用,我不明白为什么 s = set() elements = np.isnan(table) numbers = table[~elements] biggest = float(np.amax(numbers)) a = table.tolist() for x in a: coordnates = np.argwhere(table == x) if x == biggest:

如果要查找所有坐标,如何查找3D数组中最大值的坐标

到目前为止,这是我的代码,但它不起作用,我不明白为什么

s = set()
elements = np.isnan(table)
numbers = table[~elements]
biggest = float(np.amax(numbers))
a = table.tolist()
for x in a:
    coordnates = np.argwhere(table == x)
    if x == biggest:
        s.add((tuple(coordinates[0]))
print(s)
例如:

table = np.array([[[ 1, 2, 3],
        [ 8, 4, 11]],

        [[ 1, 4, 4],
        [ 8, 5, 9]],

        [[ 3, 8, 6],
        [ 11, 9, 8]],

        [[ 3, 7, 6],
        [ 9, 3, 7]]])

应返回
s={(0,1,2)、(2,1,0)}
组合
np.argwhere
np.max
(如@AshwiniChaudhary在注释中指出的)可用于查找坐标:

>>> np.argwhere(table == np.max(table))
array([[0, 1, 2],
       [2, 1, 0]], dtype=int64)
In [197]: np.transpose(np.where(table==np.max(table)))
Out[197]: 
array([[0, 1, 2],
       [2, 1, 0]], dtype=int32)
要获得集合,可以使用集合理解(需要将子数组转换为元组,以便将它们存储在集合中):

transpose
将这个由3个数组组成的元组转换为一个包含2组坐标的数组:

>>> np.argwhere(table == np.max(table))
array([[0, 1, 2],
       [2, 1, 0]], dtype=int64)
In [197]: np.transpose(np.where(table==np.max(table)))
Out[197]: 
array([[0, 1, 2],
       [2, 1, 0]], dtype=int32)
此操作非常常见,已包装在函数调用中(查看其文档)


不使用Numpy有什么原因吗?我尝试使用Numpy,只是没有找到一个特定的函数来返回所有坐标,是吗?
np.argwhere(table==table.max())
返回
array([[0,1,2],[2,1,0]])
。使用
Numpy.nanmax
忽略
Nan
s:
np.argwhere(table==np.nanmax(table))
In [199]: np.argwhere(table==np.max(table))
Out[199]: 
array([[0, 1, 2],
       [2, 1, 0]], dtype=int32)