简体   繁体   English

numpy ndarray 索引 - 从元组中检索索引

[英]numpy ndarray indexing - retrieving indexes from tuple

I have asked a similar question before, but I'm still not completely sure how numpy organises its indices.我之前问过一个类似的问题,但我仍然不完全确定 numpy 如何组织其索引。

I am working with many 3D arrays, all of which are the same size.我正在处理许多 3D 数组,所有数组的大小都相同。 due to later operations (view as window with scipy and others) I need to slice the arrays which I am doing with a series of operations looking like this:由于以后的操作(使用 scipy 和其他人查看窗口),我需要对我正在执行的一系列操作进行切片,如下所示:

imFrag.append(Padded[:100, :100, :100)

which splits the arrays into 8 pieces.它将数组分成 8 个部分。 I am trying to obtain the original indices for each of the slices.我正在尝试获取每个切片的原始索引。 I can do this for the whole 3d array using:我可以使用以下方法对整个 3d 数组执行此操作:

np.where(Mat  == Mat) 

which gives me a tuple containing x, y, and z components.这给了我一个包含 x、y 和 z 分量的元组。 is there a way I can do this?有没有办法做到这一点?

Thanks!谢谢!

Consider storing slice objects instead of the fragements themselves考虑存储切片对象而不是碎片本身

your_slice = np.s_[:100, :100, :100]

To get the image just只是为了得到图像

Padded[your_slice]

To get the indices you used to create the slice获取用于创建切片的索引

your_slice[0].start
your_slice[0].step
your_slice[0].stop

for whichever dimension you care about对于您关心的任何维度

Lets try something small and 2d:让我们尝试一些小的和 2d 的东西:

In [102]: Mat = np.arange(16).reshape(4,4)    
In [103]: sub = Mat[2:,:2]

In [104]: Mat
Out[104]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

In [105]: sub
Out[105]: 
array([[ 8,  9],
       [12, 13]])

Are you wondering how to tell were sub occurred in Mat ?你想知道如何判断sub发生在Mat吗?

In general practice the best thing is to hang on to the indexing tuple在一般实践中,最好的办法是坚持索引元组

In [106]: ind=(slice(2,None),slice(None,2))

In [107]: Mat[ind]
Out[107]: 
array([[ 8,  9],
       [12, 13]])

In [108]: Mat[ind] += sub   # duplicate the sub values

In [109]: Mat
Out[109]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [16, 18, 10, 11],
       [24, 26, 14, 15]])

( np.s_ is a nice way of constructing such a tuple if you prefer the colon syntax). (如果您更喜欢冒号语法, np.s_是构建此类元组的好方法)。

I think I could deduce this slicing from information in Mat and sub , eg我想我可以从Matsub信息推断出这种切片,例如

In [120]: Mat.__array_interface__['data'][0],Mat.shape,Mat.strides,Mat.itemsize
Out[120]: (169022104, (4, 4), (16, 4), 4)

In [121]: sub.__array_interface__['data'][0],sub.shape,sub.strides
Out[121]: (169022136, (2, 2), (16, 4))

but it requires some understanding of how data is stored and accessed.但它需要对数据的存储和访问方式有一定的了解。 For simple slices like this is shouldn't be too hard.对于像这样的简单切片不应该太难。 For more general ones, such as ones with steps and transposes, it would be harder.对于更一般的,例如带有步骤和转置的,会更难。

But I never had need to do this.但我从来不需要这样做。 Hanging on to the original slicing tuple is easier.保留原始切片元组更容易。 And if you do advanced indexing (which creates a copy rather than a view) hanging on to the indexing or masking is the only way.如果您进行高级索引(创建副本而不是视图),则坚持索引或屏蔽是唯一的方法。

===================== ======================

following on your comment:关注你的评论:

In [140]: I,J=np.where(sub==sub) 

In [141]: ind
Out[141]: (slice(2, None, None), slice(None, 2, None))

In [142]: Mat[2+I,0+J]    # 0 inplace of None for J
Out[142]: array([16, 18, 24, 26])

So yes you can use indices from sub to find corresponding elements in Mat .所以是的,您可以使用sub索引来查找Mat相应元素。 The use of where(sub==sub) to get all the indices bugs me a bit.使用where(sub==sub)获取所有索引让我有点烦恼。 meshgrid and mgrid will work but they require generating ranges. meshgridmgrid可以工作,但它们需要生成范围。 I can't off hand think of a function that takes a shape and gives corresponding I,J .我无法立即想到一个具有形状并给出相应I,J的函数。

I,J=np.array(list(np.ndindex(sub.shape))).T

will do, but isn't pretty.会做,但不漂亮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM