Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用unravel_索引查找最大数的索引_Python_Numpy - Fatal编程技术网

Python 使用unravel_索引查找最大数的索引

Python 使用unravel_索引查找最大数的索引,python,numpy,Python,Numpy,我试图理解numpy函数 unravel_index 通过应用它来查找n维数组中最大数的索引。我总是可以通过以下呼叫找到最大的号码: np.where(a == a.max())[0][0] 我试图以以下方式使用解列索引: a.unravel_index([a.max()],a.shape) 但这是行不通的。如何使用展开索引查找最大数的索引?将平面索引转换为维度索引。因此,您需要将一个平面索引传递给np。如果将多维数组转换(查看)为一维数组/向量,则平面索引是数组单元格的索引 对于您的情况

我试图理解numpy函数

unravel_index
通过应用它来查找n维数组中最大数的索引。我总是可以通过以下呼叫找到最大的号码:

np.where(a == a.max())[0][0]
我试图以以下方式使用
解列索引

a.unravel_index([a.max()],a.shape)
但这是行不通的。如何使用
展开索引
查找最大数的索引?

将平面索引转换为维度索引。因此,您需要将一个平面索引传递给
np。如果将多维数组转换(查看)为一维数组/向量,则平面索引是数组单元格的索引

对于您的情况,您可能希望使用获取平面索引,然后使用
np.unlavel_index
获取维度索引(如果需要):

要返回最大值,现在可以使用这两种方法对数组
a
进行索引:

print(a.flat[flat_idx])
print(a[dim_idx])
使用
np.where
[0][0]
索引第一个元素将仅适用于一维数组
如果在没有索引第一个元素的情况下调用它,它将返回与
np.unlavel\u index
非常相同的结果。但是当
np.unravel\u index
返回整数的
tuple
时,
np.where
将返回整数数组的
tuple


这使得索引和查找索引的速度大大降低
np.其中(a==a.max())
np.解开索引(a.argmax(),a.shape)
花费的时间大约是的4倍。(针对形状为(100100)和(10001000)的数组进行测试)。

IIUC,您需要解开索引,而不是最大值本身。因此,
a.unlavel\u索引(np.where(a==a.max())[0][0],a.shape)
可能是您想要的。数组的形状是什么?您想要平面索引还是2D、3D等索引?
print(a.flat[flat_idx])
print(a[dim_idx])