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

Robert Kern robert.kern@gmail....
Fri Oct 29 15:30:51 CDT 2010


On Fri, Oct 29, 2010 at 15:15, Jacob Biesinger <jake.biesinger@gmail.com> wrote:

> What I can't do is create a view with arbitrary indices:
>
> scores = scipy.ones((10,1))
> subset = scores[[1,5,7]]   # not a reference!
> subset[0] = 3
> subset /= subset.sum()  # does not update scores!
>
> 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.

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.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the SciPy-User mailing list