简体   繁体   English

如何使用一维的子索引生成多维2D numpy索引

[英]How to generate multi-dimensional 2D numpy index using a sub-index for one dimension

I want to use numpy.ix_ to generate an multi-dimensional index for a 2D space of values. 我想使用numpy.ix_为值的2D空间生成多维索引。 However, I need to use a subindex to look up the indices for one dimension. 但是,我需要使用子索引来查找一个维度的索引。 For example, 例如,

    assert subindex.shape == (ny, nx)

    data = np.random.random(size=(ny,nx))

    # Generator returning the index tuples 
    def get_idx(ny,nx,subindex):
      for y in range(ny):
        for x in range(nx):
           yi = y             # This is easy
           xi = subindex[y,x] # Get the second index value from the subindex

           yield (yi,xi)

    # Generator returning the data values
    def get_data_vals(ny,nx,data,subindex):
      for y in range(ny):
        for x in range(nx):
           yi = y             # This is easy
           xi = subindex[y,x] # Get the second index value from the subindex

           yield data[y,subindex[y,x]]

So instead of the for loops above, I'd like to use a multi-dimensional index to index data Using numpy.ix_ , I guess I would have something like: 因此,除了上面的for循环外,我想使用numpy.ix_使用numpy.ix_data numpy.ix_索引,我想我会有类似的东西:

    idx = numpy.ix_([np.arange(ny), ?])
    data[idx]

but I don't know what the second dimension argument should be. 但我不知道第二维参数应该是什么。 I'm guessing it should be something involving numpy.choose ? 我猜应该是涉及numpy.choose东西?

It sounds like you need to do two things: 听起来您需要做两件事:

  • Find all indices into the data array and 找到数据数组中的所有索引,然后
  • Translate the column indices according to some other array, subindex. 根据其他数组,即子索引转换列索引。

The code below therefore generates indices for all array positions (using np.indices ), and reshapes it to (..., 2) -- a 2-D list of coordinates representing each position in the array. 因此,下面的代码为所有数组位置生成索引(使用np.indices ),并将其np.indices(..., 2) -表示数组中每个位置的二维坐标列表。 For each coordinate, (i, j) , we then translate the column coordinate j using the subindex array provided, and then use that translated index as the new column index. 然后,对于每个坐标(i, j) ,我们使用提供的子索引数组转换列坐标j ,然后将该转换后的索引用作新的列索引。

With numpy, it is not necessary to do that in a for-loop--we can simply pass in all the indices at once: 使用numpy时,不必在for循环中执行此操作-我们只需一次传递所有索引即可:

i, j = np.indices(data.shape).reshape((-1, 2)).T
data[i, subindex[i, j]]

What you actually seem to want is: 您实际上想要的是:

y_idx = np.arange(ny)[:,np.newaxis]
data[y_idx, subindex]

BTW, you could achieve the same thing with y_idx = np.arange(ny).reshape((-1, 1)) . 顺便说一句,您可以使用y_idx = np.arange(ny).reshape((-1, 1))实现相同的操作。

Let's look at a small example: 让我们看一个小例子:

import numpy as np

ny, nx = 3, 5
data = np.random.rand(ny, nx)
subindex = np.random.randint(nx, size=(ny, nx))

Now 现在

np.arange(ny)
# array([0, 1, 2])

are just the indices for the "y-axis", the first dimension of data . 只是data的第一维“ y轴”的索引。 And

y_idx = np.arange(ny)[:,np.newaxis]
# array([[0],
#        [1],
#        [2]])

adds a new axis to this array (after the existing axis) and effectively transposes it. 向该数组添加一个新轴 (在现有轴之后)并有效地对其进行转置。 When you now use this array in an indexing expression together with the subindex array, the former gets broadcasted to the shape of the latter. 现在,当您在索引表达式中将此数组与subindex数组一起使用时,前者将广播为后者的形状。 So y_idx becomes effectively: 因此y_idx变得有效了:

# array([[0, 0, 0, 0, 0],
#        [1, 1, 1, 1, 1],
#        [2, 2, 2, 2, 2]])

And now for each pair of y_idx and subindex you look up an element in the data array. 现在,对于每对y_idx和子subindex您都会在data数组中查找一个元素。

Here you can find out more about "fancy indexing" 在这里您可以找到有关“花式索引”的更多信息

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

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