将Numpy数组索引存储在变量中

时间:2022-06-21 15:42:34

I want to pass an index slice as an argument to a function:

我想将索引切片作为参数传递给函数:

def myfunction(some_object_from_which_an_array_will_be_made, my_index=[1:5:2,::3]):
    my_array = whatever(some_object_from_which_an_array_will_be_made)
    return my_array[my_index]

Obviously this will not work, and obviously in this particular case there might be other ways to do this, but supposing I really want to do stuff this way, how can I use a variable to slice a numpy array?

显然这不起作用,显然在这种特殊情况下可能有其他方法可以做到这一点,但假设我真的想以这种方式做事,我怎样才能使用变量来切割numpy数组呢?

2 个解决方案

#1


8  

np.lib.index_tricks has a number of functions (and classes) that can streamline indexing. np.s_ is one such function. It is actually an instance of a class that has a __get_item__ method, so it uses the [] notation that you want.

np.lib.index_tricks有许多可以简化索引的函数(和类)。 np.s_就是这样一个功能。它实际上是一个具有__get_item__方法的类的实例,因此它使用您想要的[]表示法。

An illustration of its use:

它的用法说明:

In [249]: np.s_[1:5:2,::3]
Out[249]: (slice(1, 5, 2), slice(None, None, 3))

In [250]: np.arange(2*10*4).reshape(2,10,4)[_]
Out[250]: 
array([[[40, 41, 42, 43],
        [52, 53, 54, 55],
        [64, 65, 66, 67],
        [76, 77, 78, 79]]])

In [251]: np.arange(2*10*4).reshape(2,10,4)[1:5:2,::3]
Out[251]: 
array([[[40, 41, 42, 43],
        [52, 53, 54, 55],
        [64, 65, 66, 67],
        [76, 77, 78, 79]]])

Notice that it constructs the same tuple of slices that ajcr did. _ is the temporary variable that IPython uses for the last result.

请注意,它构造了ajcr所做的相同的切片元组。 _是IPython用于最后结果的临时变量。

To pass such a tuple to a function, try:

要将这样的元组传递给函数,请尝试:

def myfunction(some_object_from_which_an_array_will_be_made, my_index=np.s_[:,:]):
    my_array = whatever(some_object_from_which_an_array_will_be_made)
    return my_array[my_index]
I = np.s_[1:5:2,::3]
myfunction(obj, my_index=I)

#2


2  

One way is to build a slice object (or a tuple of slice objects) and pass it in to the function to use as the index.

一种方法是构建切片对象(或切片对象的元组)并将其传递给函数以用作索引。

For example, the index notation

例如,索引表示法

my_array[1:5:2, ::3]

is equivalent to

相当于

my_array[slice(1,5,2), slice(None,None,3)]

So your function could become:

所以你的功能可能变成:

def myfunction(some_object, my_index=(slice(1,5,2), slice(None,None,3))):
    my_array = whatever(some_object)
    return my_array[my_index]

#1


8  

np.lib.index_tricks has a number of functions (and classes) that can streamline indexing. np.s_ is one such function. It is actually an instance of a class that has a __get_item__ method, so it uses the [] notation that you want.

np.lib.index_tricks有许多可以简化索引的函数(和类)。 np.s_就是这样一个功能。它实际上是一个具有__get_item__方法的类的实例,因此它使用您想要的[]表示法。

An illustration of its use:

它的用法说明:

In [249]: np.s_[1:5:2,::3]
Out[249]: (slice(1, 5, 2), slice(None, None, 3))

In [250]: np.arange(2*10*4).reshape(2,10,4)[_]
Out[250]: 
array([[[40, 41, 42, 43],
        [52, 53, 54, 55],
        [64, 65, 66, 67],
        [76, 77, 78, 79]]])

In [251]: np.arange(2*10*4).reshape(2,10,4)[1:5:2,::3]
Out[251]: 
array([[[40, 41, 42, 43],
        [52, 53, 54, 55],
        [64, 65, 66, 67],
        [76, 77, 78, 79]]])

Notice that it constructs the same tuple of slices that ajcr did. _ is the temporary variable that IPython uses for the last result.

请注意,它构造了ajcr所做的相同的切片元组。 _是IPython用于最后结果的临时变量。

To pass such a tuple to a function, try:

要将这样的元组传递给函数,请尝试:

def myfunction(some_object_from_which_an_array_will_be_made, my_index=np.s_[:,:]):
    my_array = whatever(some_object_from_which_an_array_will_be_made)
    return my_array[my_index]
I = np.s_[1:5:2,::3]
myfunction(obj, my_index=I)

#2


2  

One way is to build a slice object (or a tuple of slice objects) and pass it in to the function to use as the index.

一种方法是构建切片对象(或切片对象的元组)并将其传递给函数以用作索引。

For example, the index notation

例如,索引表示法

my_array[1:5:2, ::3]

is equivalent to

相当于

my_array[slice(1,5,2), slice(None,None,3)]

So your function could become:

所以你的功能可能变成:

def myfunction(some_object, my_index=(slice(1,5,2), slice(None,None,3))):
    my_array = whatever(some_object)
    return my_array[my_index]