While trying to reproduce various fancy indexings for astropy's FITS sections (a loaded-on-demand array), I found the following interesting behavior:
np.array([1])[..., 0] array(1) np.array([1])[0] 1 np.array([1])[(0,)] 1
The docs say "Ellipsis expand to the number of : objects needed to make a selection tuple of the same length as x.ndim.", so it's not totally clear to me how to explain that difference in the results. Antony
Hi Anthony, I am not sure whether the following section in documentation is relevant to the behavior you were referring to. When an ellipsis (...) is present but has no size (i.e. replaces zero :) the result will still always be an array. A view if no advanced index is present, otherwise a copy. Here, ...replaces zero : Advanced indexing always returns a *copy* of the data (contrast with basic slicing that returns a *view* <http://docs.scipy.org/doc/numpy/glossary.html#term-view>). And I think it is a view that is returned in this case.
a = array([1]) a array([1]) a[:,0] # zero : are present Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: too many indices for array a[...,0]=2 a array([2]) a[0] = 3 a array([3]) a[(0,)] = 4 a array([4]) a[: array([1])
Hope I helped. Cheers, N.Maniteja. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Mo, 2015-01-05 at 14:13 +0530, Maniteja Nandana wrote:
Hi Anthony,
I am not sure whether the following section in documentation is relevant to the behavior you were referring to.
When an ellipsis (...) is present but has no size (i.e. replaces zero :) the result will still always be an array. A view if no advanced index is present, otherwise a copy.
Exactly. There are actually three forms of indexing to distinguish. 1. Indexing with integers (also scalar arrays) matching the number of dimensions. This will return a *scalar*. 2. Slicing, etc. which returns a view. This also occurs as soon there is an ellipsis in there (even if it replaces 0 `:`). You should see it as a feature to get a view if the result might be a scalar otherwise ;)! 3. Advanced indexing which cannot be view based and returns a copy. - Sebastian
Here, ...replaces zero :
Advanced indexing always returns a copy of the data (contrast with basic slicing that returns a view). And I think it is a view that is returned in this case.
a = array([1]) a array([1]) a[:,0] # zero : are present Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: too many indices for array a[...,0]=2 a array([2]) a[0] = 3 a array([3]) a[(0,)] = 4 a array([4]) a[: array([1])
Hope I helped.
Cheers, N.Maniteja. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
I see, thanks! 2015-01-05 2:14 GMT-07:00 Sebastian Berg <sebastian@sipsolutions.net>:
On Mo, 2015-01-05 at 14:13 +0530, Maniteja Nandana wrote:
Hi Anthony,
I am not sure whether the following section in documentation is relevant to the behavior you were referring to.
When an ellipsis (...) is present but has no size (i.e. replaces zero :) the result will still always be an array. A view if no advanced index is present, otherwise a copy.
Exactly. There are actually three forms of indexing to distinguish.
1. Indexing with integers (also scalar arrays) matching the number of dimensions. This will return a *scalar*. 2. Slicing, etc. which returns a view. This also occurs as soon there is an ellipsis in there (even if it replaces 0 `:`). You should see it as a feature to get a view if the result might be a scalar otherwise ;)! 3. Advanced indexing which cannot be view based and returns a copy.
- Sebastian
Here, ...replaces zero :
Advanced indexing always returns a copy of the data (contrast with basic slicing that returns a view). And I think it is a view that is returned in this case.
a = array([1]) a array([1]) a[:,0] # zero : are present Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: too many indices for array a[...,0]=2 a array([2]) a[0] = 3 a array([3]) a[(0,)] = 4 a array([4]) a[: array([1])
Hope I helped.
Cheers, N.Maniteja. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (3)
-
Antony Lee
-
Maniteja Nandana
-
Sebastian Berg