Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a slice object for Numpy array?

I've tried to find a neat solution to this, but I'm slicing several 2D arrays of the same shape in the same manner. I've tidied it up as much as I can by defining a list containing the 'x,y' center e.g. cpix = [161, 134] What I'd like to do is instead of having to write out the slice three times like so:

a1 = array1[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50]  a2 = array2[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50]  a3 = array3[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50] 

is just have something predefined (like maybe a mask?) so I can just do a

a1 = array1[predefined_2dslice]  a2 = array2[predefined_2dslice]  a3 = array3[predefined_2dslice]  

Is this something that numpy supports?

like image 715
FriskyGrub Avatar asked Aug 12 '16 11:08

FriskyGrub


People also ask

Can you slice NumPy arrays?

One-Dimensional SlicingThe first item of the array can be sliced by specifying a slice that starts at index 0 and ends at index 1 (one item before the 'to' index). Running the example returns a subarray with the first element. We can also use negative indexes in slices.

How do I slice a row in NumPy array?

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 .

What is NumPy slice?

Slicing in python means taking elements from one given index to another given index. We pass slice instead of index like this: [start:end] . We can also define the step, like this: [start:end:step] .

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.


2 Answers

Yes you can use numpy.s_:

Example:

>>> a = np.arange(10).reshape(2, 5) >>>  >>> m = np.s_[0:2, 3:4] >>>  >>> a[m] array([[3],        [8]]) 

And in this case:

my_slice = np.s_[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50]  a1 = array1[my_slice]  a2 = array2[my_slice]  a3 = array3[my_slice] 

You can also use numpy.r_ in order to translates slice objects to concatenation along the first axis.

like image 175
Mazdak Avatar answered Sep 22 '22 01:09

Mazdak


You can index a multidimensional array by using a tuple of slice objects.

window = slice(col_start, col_stop), slice(row_start, row_stop) a1 = array1[window] a2 = array2[window]  

This is not specific to numpy and is simply how subscription/slicing syntax works in python.

class mock_array:     def __getitem__(self, key):         print(key) m = mock_array() m[1:3, 7:9] # prints tuple(slice(1, 3, None), slice(7, 9, None)) 
like image 42
benjimin Avatar answered Sep 22 '22 01:09

benjimin