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

Jacob Biesinger jake.biesinger at gmail.com
Fri Oct 29 16:51:39 EDT 2010


>> Is there a way to do this?
>
> Not automatically, no. But if you keep that index list around, it's
> pretty straightforward to do the update manually.
>
> indices = [1, 5, 7]
> subset = scores[indices]
> subset[0] = 3
> subset /= subset.sum()
> scores[indices] = subset
>
> It doesn't save you any memory, though.

Right-- as I mentioned, this is my current strategy.  It's just a bit
slow since there are millions of sets of indices.

> numpy arrays must be described by a starting memory location and
> uniform strides. It can't jump around arbitrarily in memory. Well, it
> could, but it would be slow for typical cases.

I thought I was close with the list of array slices.  For some
operations (like taking the summation), I think this would work okay,
but it would likely take more memory than a list of integer indices.

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)]



More information about the SciPy-User mailing list