在不使用[slice,slice]语法的情况下使用Numpy多维数组切片?

时间:2022-08-14 04:18:57

Is there are a way to use Numpy's multidimensional array slicing without using the [slice, slice] syntax?

有没有办法在不使用[slice,slice]语法的情况下使用Numpy的多维数组切片?

I need to be able to use it from normal function calls, but I haven't found a way to use the slice object to do it.

我需要能够从正常的函数调用中使用它,但我还没有找到一种方法来使用切片对象来完成它。

I cannot use the syntax [(slice,slice)] for my program because [] is special syntax outside of regular function calls. The language I am using is Hy, a Lisp for Python, and it does not support this syntax. More importantly, it shouldn't support this syntax. Numpy, however, doesn't seem to support multidimensional slicing without using the [] syntax.

我不能为我的程序使用语法[(slice,slice)],因为[]是常规函数调用之外的特殊语法。我使用的语言是Hy,一个用于Python的Lisp,它不支持这种语法。更重要的是,它不应该支持这种语法。但是,不使用[]语法,Numpy似乎不支持多维切片。

What's tripping me up is that the mix of C and Python in the Numpy source makes it difficult to discern how the [slice,slice] is implemented. It may not even be possible to circumvent this syntax.

令我感到震惊的是Numpy源中C和Python的混合使得很难分辨出[slice,slice]的实现方式。甚至可能无法绕过这种语法。

EDIT:

The answer provided below by @Joe Kington allows one to slice Numpy matrices like so:

@Joe Kington在下面提供的答案允许人们像这样切割Numpy矩阵:

x = np.array([list(range(5)) for x in list(range(5))]) x.getitem(slice(1,4)) array([[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]) x.getitem(tuple([slice(1,4),slice(1,4)])) array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])

x = np.array([list(range(5))for x in list(range(5))])x.getitem(slice(1,4))array([[0,1,2,3,4] ],[0,1,2,3,4],[0,1,2,3,4]])x.getitem(元组([slice(1,4),slice(1,4)]))数组([[1,2,3],[1,2,3],[1,2,3]])

2 个解决方案

#1


7  

From your description, it seems like you're asking what function calls are used to implement slicing and slice assignment.

从您的描述中,您似乎在询问使用哪些函数调用来实现切片和切片分配。

Python uses the "special" methods __getitem__ and __setitem__ to implement and/or allow customization of how slicing works. Any class that implements these can be sliced. There's actually nothing numpy-specific about this.

Python使用“特殊”方法__getitem__和__setitem__来实现和/或允许自定义切片的工作方式。任何实现这些的类都可以切片。实际上没有什么关于这个特定的numpy。

In other words

换一种说法

x = arr[4:10, 9:15, ::-1]
x[0] = 100

is translated into

被翻译成

x = arr.__getitem__((slice(4, 6), slice(9, 10), slice(None, None, -1)))
x.__setitem__(0, 100)

For example:

class Foo(object):
    def __getitem__(self, index):
        print 'Getting', index
    def __setitem__(self, index, val):
        print 'Setting', index, 'to', val

f = Foo()
print 'Getting...'
f[:]
f[4:10, ::-1, ...]

print 'Equivalently:'
f.__getitem__(slice(None))
f.__getitem__((slice(4, 10), slice(None, None, -1), Ellipsis))

print 'Setting...'
f[0] = 1
f[5:10, 100] = 2
f[...] = 100

print 'Equivalently:'
f.__setitem__(0, 1)
f.__setitem__((slice(5,10), 100), 2)
f.__setitem__(Ellipsis, 100)

Also, it can be handy to know about numpy.index_exp (or equivalently, np.s_). It's nothing fancy -- it just translates slicing into the equivalent tuple, etc. It's quite similar to our Foo class above. For example:

此外,了解numpy.index_exp(或等效地,np.s_)也很方便。它没什么特别的 - 它只是将切片转换成等效的元组等等。它与我们上面的Foo类非常相似。例如:

In [1]: np.index_exp[10:4, ::-1, ...]
Out[1]: (slice(10, 4, None), slice(None, None, -1), Ellipsis)

#2


1  

I suspect you are trying to pass the slice through as a parameter?

我怀疑你是试图通过切片作为参数?

def do_slice(sl, mystring):
    return mystring[sl]

sl = slice(0,2)
mystr = "Hello"

print do_slice(sl, mystr)

#1


7  

From your description, it seems like you're asking what function calls are used to implement slicing and slice assignment.

从您的描述中,您似乎在询问使用哪些函数调用来实现切片和切片分配。

Python uses the "special" methods __getitem__ and __setitem__ to implement and/or allow customization of how slicing works. Any class that implements these can be sliced. There's actually nothing numpy-specific about this.

Python使用“特殊”方法__getitem__和__setitem__来实现和/或允许自定义切片的工作方式。任何实现这些的类都可以切片。实际上没有什么关于这个特定的numpy。

In other words

换一种说法

x = arr[4:10, 9:15, ::-1]
x[0] = 100

is translated into

被翻译成

x = arr.__getitem__((slice(4, 6), slice(9, 10), slice(None, None, -1)))
x.__setitem__(0, 100)

For example:

class Foo(object):
    def __getitem__(self, index):
        print 'Getting', index
    def __setitem__(self, index, val):
        print 'Setting', index, 'to', val

f = Foo()
print 'Getting...'
f[:]
f[4:10, ::-1, ...]

print 'Equivalently:'
f.__getitem__(slice(None))
f.__getitem__((slice(4, 10), slice(None, None, -1), Ellipsis))

print 'Setting...'
f[0] = 1
f[5:10, 100] = 2
f[...] = 100

print 'Equivalently:'
f.__setitem__(0, 1)
f.__setitem__((slice(5,10), 100), 2)
f.__setitem__(Ellipsis, 100)

Also, it can be handy to know about numpy.index_exp (or equivalently, np.s_). It's nothing fancy -- it just translates slicing into the equivalent tuple, etc. It's quite similar to our Foo class above. For example:

此外,了解numpy.index_exp(或等效地,np.s_)也很方便。它没什么特别的 - 它只是将切片转换成等效的元组等等。它与我们上面的Foo类非常相似。例如:

In [1]: np.index_exp[10:4, ::-1, ...]
Out[1]: (slice(10, 4, None), slice(None, None, -1), Ellipsis)

#2


1  

I suspect you are trying to pass the slice through as a parameter?

我怀疑你是试图通过切片作为参数?

def do_slice(sl, mystring):
    return mystring[sl]

sl = slice(0,2)
mystr = "Hello"

print do_slice(sl, mystr)