Using Python Range Objects To Index Into Numpy Arrays
I've seen it once or twice before, but I can't seem to find any official docs on it: Using python range objects as indices in numpy. import numpy as np a = np.arange(9).reshape(3,3
Solution 1:
Not a proper answer, but too long for comment.
In fact, it seems to work with about any indexable object:
import numpy as np
classMyIndex:
def__init__(self, n):
self.n = n
def__getitem__(self, i):
if i < 0or i >= self.n:
raise IndexError
return i
def__len__(self):
return self.n
a = np.array([1, 2, 3])
print(a[MyIndex(2)])
# [1 2]
I think the relevant lines in NumPy's code are below this comment in core/src/multiarray/mapping.c
:
/*
* Some other type of short sequence - assume we should unpack it like a
* tuple, and then decide whether that was actually necessary.
*/
But I'm not entirely sure. For some reason, this hangs if you remove the if i < 0 or i >= self.n: raise IndexError
, even though there is a __len__
, so at some point it seems to be iterating through the given object until IndexError
is raised.
Solution 2:
Just to wrap this up (thanks to @WarrenWeckesser in the comments): This behavior is actually documented. One only has to realize that range
objects are python sequences in the strict sense.
So this is just a case of fancy indexing. Be warned, though, that it is very slow:
>>>a = np.arange(100000)>>>timeit(lambda: a[range(100000)], number=1000)
12.969507368048653
>>>timeit(lambda: a[list(range(100000))], number=1000)
7.990526253008284
>>>timeit(lambda: a[np.arange(100000)], number=1000)
0.22483703796751797
Post a Comment for "Using Python Range Objects To Index Into Numpy Arrays"