Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Numpy Multidimensional Array Slicing Without Using the [slice,slice] Syntax?

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

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.

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.

EDIT:

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

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]])

like image 511
calben Avatar asked Mar 20 '14 14:03

calben


People also ask

How do you slice a 2D NP array?

Slice Two-dimensional Numpy Arrays To slice elements from two-dimensional arrays, you need to specify both a row index and a column index as [row_index, column_index] . For example, you can use the index [1,2] to query the element at the second row, third column in precip_2002_2013 .

How can we create numpy array explain indexing and slicing?

Indexing can be done in numpy by using an array as an index. In case of slice, a view or shallow copy of the array is returned but in index array a copy of the original array is returned. Numpy arrays can be indexed with other arrays or any other sequence with the exception of tuples.

How does numpy array slicing work?

Slicing an array You can slice a numpy array is a similar way to slicing a list - except you can do it in more than one dimension. As with indexing, the array you get back when you index or slice a numpy array is a view of the original array. It is the same data, just accessed in a different order.

How do you slice a 2D list in Python?

As shown in the above syntax, to slice a Python list, you have to append square brackets in front of the list name. Inside square brackets you have to specify the index of the item where you want to start slicing your list and the index + 1 for the item where you want to end slicing.


1 Answers

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.

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:

In [1]: np.index_exp[10:4, ::-1, ...]
Out[1]: (slice(10, 4, None), slice(None, None, -1), Ellipsis)
like image 157
Joe Kington Avatar answered Oct 11 '22 08:10

Joe Kington