A-T*_*Tab 5 python indexing numpy multidimensional-array reshape
numpy.reshape在Python中使用时,有没有办法跟踪索引的变化?
例如,如果将具有该形状的numpy数组(m,n,l,k)重塑为具有形状的数组(m*n,k*l);有没有一种方法可以获取[x,y,w,z]当前[X,Y]索引的初始索引(),反之亦然?
是的,有,它叫做raveling和unraveling索引。例如,您有两个数组:
import numpy as np
arr1 = np.arange(10000).reshape(20, 10, 50)
arr2 = arr.reshape(20, 500)
Run Code Online (Sandbox Code Playgroud)
表示您想索引(10, 52)(相当于arr2[10, 52])元素,但在中arr1:
>>> np.unravel_index(np.ravel_multi_index((10, 52), arr2.shape), arr1.shape)
(10, 1, 2)
Run Code Online (Sandbox Code Playgroud)
或另一个方向:
>>> np.unravel_index(np.ravel_multi_index((10, 1, 2), arr1.shape), arr2.shape)
(10, 52)
Run Code Online (Sandbox Code Playgroud)