简体   繁体   中英

Slicing a numpy array vs selecting a single element

I have an array:

arr = np.array([[1,2,3], [4,5,6], [7,8,9]])

If I slice the array arr[:, :1] my result is array([[1], [4], [7]]) .

If I slice the array arr[:, 0] my result is array([1, 4, 7]) .

Why is there a difference?

:1 is a slice (which happens to be of length 1) so numpy returns a list for every line (first dim) in your array. This is why you have a 2-dimensional array as a result: you asked for a sub array with : .

0 on the other hand is an index so numpy shrinks the result by 1 dimension resulting of a list of values (array of dimension 1 more precisely).

https://docs.scipy.org/doc/numpy-1.14.2/user/basics.indexing.html#single-element-indexing

claims that single element indexing behaves like the usual Python list indexing. Slicing also follows this pattern

In [175]: arr = np.arange(6).reshape(2,3)
In [176]: arr
Out[176]: 
array([[0, 1, 2],
       [3, 4, 5]])

single element indexing - returns a 1d array

In [177]: arr[1,:]
Out[177]: array([3, 4, 5])

slice indexing returns a 2d array

In [178]: arr[:1,:]
Out[178]: array([[0, 1, 2]])

with an equivalent list

In [179]: alist = arr.tolist()

single element indexing returns a list:

In [180]: alist[1]
Out[180]: [3, 4, 5]

slice indexing returns a nested list:

In [181]: alist[:1]
Out[181]: [[0, 1, 2]]

Same pattern holds when I index a 1d array, or list of numbers.

numpy generalizes this to columns, and higher dimensions, but tries to keep the pattern the same.

In [187]: arr[:,0]
Out[187]: array([0, 3])
In [188]: arr[:,:1]
Out[188]: 
array([[0],
       [3]])
In [189]: [x[0] for x in alist]
Out[189]: [0, 3]
In [190]: [x[:1] for x in alist]
Out[190]: [[0], [3]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM