且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用2d数组索引3d numpy数组

更新时间:2022-11-09 23:32:27

花式索引和广播结果的形状是索引数组的形状.您需要为arr_3d

The shape of the result from fancy index and broadcasting is the shape of the indexing array. You need passing 2d array for each axis of arr_3d

ax_0 = np.arange(arr_3d.shape[0])[:,None]
ax_1 = np.arange(arr_3d.shape[1])[None,:]

arr_3d[ax_0, ax_1, arr_2d]

Out[1127]:
array([[ 3,  6,  8],
       [14, 19, 22]])