[SciPy-User] Scipy views and slicing: Can I get a view-slice from only certain elements of an array?

Francesc Alted faltet at pytables.org
Sat Oct 30 06:00:05 EDT 2010


A Friday 29 October 2010 22:51:39 Jacob Biesinger escrigué:
> One last thought-- It would be nice to be able to use array.array's
> as indices to scipy.array's.  In other words:
> 
> indices = array('l', [1,5,7])
> scores[indices]
> ---------------------------------------------------------------------
> ------ IndexError                                Traceback (most
> recent call last)
> 
> This works fine but is a bit too much overhead for the memory savings
> I get from array's:
> scores[list(indices)]

Why not using a numpy.array object instead of array.array?  Indexing 
with them is much faster than using plain lists:

>>> a = np.arange(1000)
>>> b = np.arange(1e8)
>>> timeit b[a]
100000 loops, best of 3: 10.4 µs per loop
>>> l = a.tolist()
>>> timeit b[l]
10000 loops, best of 3: 66.5 µs per loop

NumPy arrays helps saving space too:

>>> sys.getsizeof(l)
8072
>>> a.size*a.itemsize
8000   # 72 bytes less, not a lot but better than nothing

And, if you don't need to address arrays larger than 2**31, then you can 
save more space yet if you choose the int32 type for indexing:

>>> a4 = a.astype('i4')
>>> a4.size*a4.itemsize
4000
>>> timeit b[a4]
100000 loops, best of 3: 10.2 µs per loop  # similar performance than i8

Hope this helps,

-- 
Francesc Alted



More information about the SciPy-User mailing list