Python 如何在NumPy中将N-D切片转换为索引?
给定任意N元组切片(也称为N-D切片),如何将其转换为N-D数组的对应索引,表示为1D数组的元组(沿每个轴的索引)?例如,如果我们有Python 如何在NumPy中将N-D切片转换为索引?,python,arrays,numpy,indexing,slice,Python,Arrays,Numpy,Indexing,Slice,给定任意N元组切片(也称为N-D切片),如何将其转换为N-D数组的对应索引,表示为1D数组的元组(沿每个轴的索引)?例如,如果我们有np.nd\u slice\u to\u索引next code: import numpy as np print(np.nd_slice_to_indexes(np.s_[1 : 3])) print(np.nd_slice_to_indexes(np.s_[1 : 3, 5 : 11 : 2])) 应该打印 (array([1, 2]),) (array([1
np.nd\u slice\u to\u索引
next code:
import numpy as np
print(np.nd_slice_to_indexes(np.s_[1 : 3]))
print(np.nd_slice_to_indexes(np.s_[1 : 3, 5 : 11 : 2]))
应该打印
(array([1, 2]),)
(array([1, 1, 1, 2, 2, 2]), array([5, 7, 9, 5, 7, 9]))
NumPy通常将N-D数组的索引表示为相同长度的1-D数组的N元组(元组中k-th
数组的每个元素表示沿k-th维的下一个索引)。例如,np.nonzero
在代码中返回这样的N元组
print(np.nonzero([[0, 1, 1], [1, 1, 0]])) # Non-zero elements in 2D array.
# (array([0, 0, 1, 1], dtype=int64), array([1, 2, 0, 1], dtype=int64))
同样的行为应该像下面的Pythonic函数一样实现,但要以更高效(性能)的方式实现:
由于@hpaulj的使用,有效地解决了任务
查看
np.ogrid
和np.mgrid
@hpaulj谢谢!作品添加了您关于np.mgrid
作为的建议。@hpaulj虽然查看np.mgrid
的代码,但可能是因为它的实现性能不太好。可能效率不高,没有进行计时,不确定np.mgrid
是否真的实现得很快。
import numpy as np
def nd_slice_to_indexes(nd_slice):
assert type(nd_slice) in [tuple, slice], type(nd_slice)
if type(nd_slice) is not tuple:
nd_slice = (nd_slice,)
def iter_slices(slices):
if len(slices) == 0:
yield ()
else:
for i in range(slices[0].start, slices[0].stop, slices[0].step or 1):
for r in iter_slices(slices[1:]):
yield (i,) + r
*res, = np.vstack(list(iter_slices(nd_slice))).T
return tuple(res)
print(nd_slice_to_indexes(np.s_[1 : 3]))
print(nd_slice_to_indexes(np.s_[1 : 3, 5 : 11 : 2]))
print(nd_slice_to_indexes(np.s_[1 : 3, 5 : 11 : 2, 8 : 14 : 3]))
# (array([1, 2]),)
# (array([1, 1, 1, 2, 2, 2]), array([5, 7, 9, 5, 7, 9]))
# (array([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]), array([5, 5, 7, 7, 9, 9, 5, 5, 7, 7, 9, 9]), array([ 8, 11, 8, 11, 8, 11, 8, 11, 8, 11, 8, 11]))
import numpy as np
def nd_slice_to_indexes(nd_slice):
grid = np.mgrid[{tuple: nd_slice, slice: (nd_slice,)}[type(nd_slice)]]
return tuple(grid[i].ravel() for i in range(grid.shape[0]))
print(nd_slice_to_indexes(np.s_[1 : 3]))
print(nd_slice_to_indexes(np.s_[1 : 3, 5 : 11 : 2]))
print(nd_slice_to_indexes(np.s_[1 : 3, 5 : 11 : 2, 8 : 14 : 3]))
# (array([1, 2]),)
# (array([1, 1, 1, 2, 2, 2]), array([5, 7, 9, 5, 7, 9]))
# (array([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]), array([5, 5, 7, 7, 9, 9, 5, 5, 7, 7, 9, 9]), array([ 8, 11, 8, 11, 8, 11, 8, 11, 8, 11, 8, 11]))