Slicing arrays in Numpy / Scipy

前端 未结 3 787
栀梦
栀梦 2020-12-03 13:54

I have an array like:

a = array([[1,2,3],[3,4,5],[4,5,6]])

What\'s the most efficient way to slice out a 1x2 array out of this that has onl

相关标签:
3条回答
  • 2020-12-03 14:27

    Two dimensional numpy arrays are indexed using a[i,j] (not a[i][j]), but you can use the same slicing notation with numpy arrays and matrices as you can with ordinary matrices in python (just put them in a single []):

    >>> from numpy import array
    >>> a = array([[1,2,3],[3,4,5],[4,5,6]])
    >>> a[:,1:]
    array([[2, 3],
           [4, 5],
           [5, 6]])
    
    0 讨论(0)
  • 2020-12-03 14:34

    To quote documentation, the basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step (when k > 0).

    Now if i is not given, it defaults to 0 if k > 0. Otherwise i defaults to n - 1 for k < 0 (where n is the length of the array).

    If j is not given, it defaults to n (length of array).

    That's for a one dimensional array.

    Now a two dimensional array is a different beast. The slicing syntax for that is a[rowrange, columnrange].

    So if you want all the rows, but just the last two columns, like in your case, you do:

    a[0:3, 1:3]
    

    Here, "[0:3]" means all the rows from 0 to 3. and "[1:3]" means all columns from column 1 to column 3.

    Now as you may be wondering, even though you have only 3 columns and the numbering starts from 1, it must return 3 columns right? i.e: column 1, column 2, column 3

    That is the tricky part of this syntax. The first column is actually column 0. So when you say "[1:3]", you are actually saying give me column 1 and column 2. Which are the last two columns you want. (There actually is no column 3.)

    Now if you don't know how long your matrix is or if you want all the rows, you can just leave that part empty. i.e.

    a[:, 1:3]
    

    Same goes for columns also. i.e if you wanted say, all the columns but just the first row, you would write

    a[0:1,:]
    

    Now, how the above answer a[:,1:] works is because when you say "[1:]" for columns, it means give me everything except for column 0, and till the end of all the columns. i.e empty means 'till the end'.

    By now you must realize that anything on either side of the comma is all a subset of the one dimensional case I first mentioned above. i.e if you want to specify your rows using step sizes you can write

    a[::2,1]
    

    Which in your case would return

    array([[2, 3],
           [5, 6]])
    

    i.e. a[::2,1] elucidates as: give me every other row, starting with the top most, and give me only the 2nd column.

    This took me some time to figure out. So pasting it here, just in case it helps someone.

    0 讨论(0)
  • 2020-12-03 14:38

    Is this what you're looking for?

    a[:,1:]
    
    0 讨论(0)
提交回复
热议问题