从Numpy中的多个片中选择

时间:2022-01-20 21:26:24

Say we have

说我们有

a = np.ones((3,3,3))

and

slices = [(0, 1, slice(None)), (0, slice(None), 0),  (slice(None), 1, 0)]

Is there a simple way to select/change values of a from slices?

是否有一种简单的方法从片中选择/更改a的值?

For example, I want to assign 0 to a under slices such that a becomes

例如,我想把0赋给a,使a变成

array([[[0., 1., 1.],
        [0., 0., 0.],
        [0., 1., 1.]],

       [[1., 1., 1.],
        [0., 1., 1.],
        [1., 1., 1.]],

       [[1., 1., 1.],
        [0., 1., 1.],
        [1., 1., 1.]]])

An iterative way is to do

一个迭代的方法就是去做

for t in slices:
    a[t] = 0

Is there a better way to make use of the indices like np.r_ like what's used by JoshAdel in Assign value to multiple slices in numpy?

有没有更好的方法来利用像np这样的指标。比如JoshAdel在给numpy中的多个片赋值时用了什么?

I am hoping to achieve something like a[SLICES] = 0 and change all part of a from each slice in slices to 0.

我希望实现类似于[切片]= 0的方法,并将每个切片的所有部分更改为0。

1 个解决方案

#1


2  

Do you know what r_ does? It converts the slices into ranges, and then concatenates the whole mess together.

你知道r_是做什么的吗?它将切片转换为范围,然后将整个混乱连接在一起。

I don't know if you can use r_ or something similar to construct the required indices. But:

我不知道你是否可以使用r_或类似的东西来构造所需的索引。但是:

In [168]: idx = np.where(a==0)
In [169]: idx
Out[169]: 
(array([0, 0, 0, 0, 0, 1, 2]),
 array([0, 1, 1, 1, 2, 1, 1]),
 array([0, 0, 1, 2, 0, 0, 0]))

this is gives us an idea of the required indexing arrays (minus some likely duplicates).

这使我们了解了所需的索引数组(减去一些可能的重复)。


It might be possible to concatenate these 3 ogrid lists into a composite:

可以将这3个ogrid列表合并成一个组合:

In [181]: np.ogrid[0:1,1:2,:3]
Out[181]: [array([[[0]]]), array([[[1]]]), array([[[0, 1, 2]]])]

In [182]: np.ogrid[0:1,:3,0:1]
Out[182]: 
[array([[[0]]]), array([[[0],
         [1],
         [2]]]), array([[[0]]])]

In [183]: np.ogrid[:3,1:2,0:1]
Out[183]: 
[array([[[0]],

        [[1]],

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

Individually they select the 0s in a.

它们分别在a中选择0。

It may be easiest to convert them into their raveled equivalents, and join the resulting 1d arrays.

将它们转换为它们的raveled对等物可能是最简单的,并将其连接到结果的1d数组中。

In [188]: np.ravel_multi_index(Out[181],(3,3,3))
Out[188]: array([[[3, 4, 5]]])
etc
In [195]: np.hstack([Out[188].ravel(), Out[189].ravel(), Out[190].ravel()])
Out[195]: array([ 3,  4,  5,  0,  3,  6,  3, 12, 21])
In [197]: a.flat[_]
Out[197]: array([0., 0., 0., 0., 0., 0., 0., 0., 0.])

In [199]: np.unravel_index(Out[195],(3,3,3))
Out[199]: 
(array([0, 0, 0, 0, 0, 0, 0, 1, 2]),
 array([1, 1, 1, 0, 1, 2, 1, 1, 1]),
 array([0, 1, 2, 0, 0, 0, 0, 0, 0]))

Out[169] and Out[199] have the same values, except for duplicates.

Out[169]和Out[199]具有相同的值,但重复的值除外。

This is a generalization of the problem of joining several 1d slices. Indexing and then concatenating takes about as much time as concatenating the indices first.

这是对加入多个1d片的问题的概括。索引和然后连接所需的时间与首先连接索引所需的时间相同。

#1


2  

Do you know what r_ does? It converts the slices into ranges, and then concatenates the whole mess together.

你知道r_是做什么的吗?它将切片转换为范围,然后将整个混乱连接在一起。

I don't know if you can use r_ or something similar to construct the required indices. But:

我不知道你是否可以使用r_或类似的东西来构造所需的索引。但是:

In [168]: idx = np.where(a==0)
In [169]: idx
Out[169]: 
(array([0, 0, 0, 0, 0, 1, 2]),
 array([0, 1, 1, 1, 2, 1, 1]),
 array([0, 0, 1, 2, 0, 0, 0]))

this is gives us an idea of the required indexing arrays (minus some likely duplicates).

这使我们了解了所需的索引数组(减去一些可能的重复)。


It might be possible to concatenate these 3 ogrid lists into a composite:

可以将这3个ogrid列表合并成一个组合:

In [181]: np.ogrid[0:1,1:2,:3]
Out[181]: [array([[[0]]]), array([[[1]]]), array([[[0, 1, 2]]])]

In [182]: np.ogrid[0:1,:3,0:1]
Out[182]: 
[array([[[0]]]), array([[[0],
         [1],
         [2]]]), array([[[0]]])]

In [183]: np.ogrid[:3,1:2,0:1]
Out[183]: 
[array([[[0]],

        [[1]],

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

Individually they select the 0s in a.

它们分别在a中选择0。

It may be easiest to convert them into their raveled equivalents, and join the resulting 1d arrays.

将它们转换为它们的raveled对等物可能是最简单的,并将其连接到结果的1d数组中。

In [188]: np.ravel_multi_index(Out[181],(3,3,3))
Out[188]: array([[[3, 4, 5]]])
etc
In [195]: np.hstack([Out[188].ravel(), Out[189].ravel(), Out[190].ravel()])
Out[195]: array([ 3,  4,  5,  0,  3,  6,  3, 12, 21])
In [197]: a.flat[_]
Out[197]: array([0., 0., 0., 0., 0., 0., 0., 0., 0.])

In [199]: np.unravel_index(Out[195],(3,3,3))
Out[199]: 
(array([0, 0, 0, 0, 0, 0, 0, 1, 2]),
 array([1, 1, 1, 0, 1, 2, 1, 1, 1]),
 array([0, 1, 2, 0, 0, 0, 0, 0, 0]))

Out[169] and Out[199] have the same values, except for duplicates.

Out[169]和Out[199]具有相同的值,但重复的值除外。

This is a generalization of the problem of joining several 1d slices. Indexing and then concatenating takes about as much time as concatenating the indices first.

这是对加入多个1d片的问题的概括。索引和然后连接所需的时间与首先连接索引所需的时间相同。