Slicing of a NumPy 2d array, or how do I extract an mxm submatrix from an nxn array (n>m)?

后端 未结 7 1469
闹比i
闹比i 2020-11-27 09:30

I want to slice a NumPy nxn array. I want to extract an arbitrary selection of m rows and columns of that array (i.e. without any pattern in the numbers of rows/col

相关标签:
7条回答
  • 2020-11-27 10:02

    With numpy, you can pass a slice for each component of the index - so, your x[0:2,0:2] example above works.

    If you just want to evenly skip columns or rows, you can pass slices with three components (i.e. start, stop, step).

    Again, for your example above:

    >>> x[1:4:2, 1:4:2]
    array([[ 5,  7],
           [13, 15]])
    

    Which is basically: slice in the first dimension, with start at index 1, stop when index is equal or greater than 4, and add 2 to the index in each pass. The same for the second dimension. Again: this only works for constant steps.

    The syntax you got to do something quite different internally - what x[[1,3]][:,[1,3]] actually does is create a new array including only rows 1 and 3 from the original array (done with the x[[1,3]] part), and then re-slice that - creating a third array - including only columns 1 and 3 of the previous array.

    0 讨论(0)
  • 2020-11-27 10:04

    To answer this question, we have to look at how indexing a multidimensional array works in Numpy. Let's first say you have the array x from your question. The buffer assigned to x will contain 16 ascending integers from 0 to 15. If you access one element, say x[i,j], NumPy has to figure out the memory location of this element relative to the beginning of the buffer. This is done by calculating in effect i*x.shape[1]+j (and multiplying with the size of an int to get an actual memory offset).

    If you extract a subarray by basic slicing like y = x[0:2,0:2], the resulting object will share the underlying buffer with x. But what happens if you acces y[i,j]? NumPy can't use i*y.shape[1]+j to calculate the offset into the array, because the data belonging to y is not consecutive in memory.

    NumPy solves this problem by introducing strides. When calculating the memory offset for accessing x[i,j], what is actually calculated is i*x.strides[0]+j*x.strides[1] (and this already includes the factor for the size of an int):

    x.strides
    (16, 4)
    

    When y is extracted like above, NumPy does not create a new buffer, but it does create a new array object referencing the same buffer (otherwise y would just be equal to x.) The new array object will have a different shape then x and maybe a different starting offset into the buffer, but will share the strides with x (in this case at least):

    y.shape
    (2,2)
    y.strides
    (16, 4)
    

    This way, computing the memory offset for y[i,j] will yield the correct result.

    But what should NumPy do for something like z=x[[1,3]]? The strides mechanism won't allow correct indexing if the original buffer is used for z. NumPy theoretically could add some more sophisticated mechanism than the strides, but this would make element access relatively expensive, somehow defying the whole idea of an array. In addition, a view wouldn't be a really lightweight object anymore.

    This is covered in depth in the NumPy documentation on indexing.

    Oh, and nearly forgot about your actual question: Here is how to make the indexing with multiple lists work as expected:

    x[[[1],[3]],[1,3]]
    

    This is because the index arrays are broadcasted to a common shape. Of course, for this particular example, you can also make do with basic slicing:

    x[1::2, 1::2]
    
    0 讨论(0)
  • 2020-11-27 10:10

    As Sven mentioned, x[[[0],[2]],[1,3]] will give back the 0 and 2 rows that match with the 1 and 3 columns while x[[0,2],[1,3]] will return the values x[0,1] and x[2,3] in an array.

    There is a helpful function for doing the first example I gave, numpy.ix_. You can do the same thing as my first example with x[numpy.ix_([0,2],[1,3])]. This can save you from having to enter in all of those extra brackets.

    0 讨论(0)
  • 2020-11-27 10:17

    I don't think that x[[1,3]][:,[1,3]] is hardly readable. If you want to be more clear on your intent, you can do:

    a[[1,3],:][:,[1,3]]
    

    I am not an expert in slicing but typically, if you try to slice into an array and the values are continuous, you get back a view where the stride value is changed.

    e.g. In your inputs 33 and 34, although you get a 2x2 array, the stride is 4. Thus, when you index the next row, the pointer moves to the correct position in memory.

    Clearly, this mechanism doesn't carry well into the case of an array of indices. Hence, numpy will have to make the copy. After all, many other matrix math function relies on size, stride and continuous memory allocation.

    0 讨论(0)
  • 2020-11-27 10:17

    I'm not sure how efficient this is but you can use range() to slice in both axis

     x=np.arange(16).reshape((4,4))
     x[range(1,3), :][:,range(1,3)] 
    
    0 讨论(0)
  • 2020-11-27 10:18

    If you want to skip every other row and every other column, then you can do it with basic slicing:

    In [49]: x=np.arange(16).reshape((4,4))
    In [50]: x[1:4:2,1:4:2]
    Out[50]: 
    array([[ 5,  7],
           [13, 15]])
    

    This returns a view, not a copy of your array.

    In [51]: y=x[1:4:2,1:4:2]
    
    In [52]: y[0,0]=100
    
    In [53]: x   # <---- Notice x[1,1] has changed
    Out[53]: 
    array([[  0,   1,   2,   3],
           [  4, 100,   6,   7],
           [  8,   9,  10,  11],
           [ 12,  13,  14,  15]])
    

    while z=x[(1,3),:][:,(1,3)] uses advanced indexing and thus returns a copy:

    In [58]: x=np.arange(16).reshape((4,4))
    In [59]: z=x[(1,3),:][:,(1,3)]
    
    In [60]: z
    Out[60]: 
    array([[ 5,  7],
           [13, 15]])
    
    In [61]: z[0,0]=0
    

    Note that x is unchanged:

    In [62]: x
    Out[62]: 
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11],
           [12, 13, 14, 15]])
    

    If you wish to select arbitrary rows and columns, then you can't use basic slicing. You'll have to use advanced indexing, using something like x[rows,:][:,columns], where rows and columns are sequences. This of course is going to give you a copy, not a view, of your original array. This is as one should expect, since a numpy array uses contiguous memory (with constant strides), and there would be no way to generate a view with arbitrary rows and columns (since that would require non-constant strides).

    0 讨论(0)
提交回复
热议问题