[SciPy-User] creating a view of an array

Joe Kington jkington at wisc.edu
Sat Jul 30 18:02:33 EDT 2011


On Sat, Jul 30, 2011 at 1:48 PM, Martin van Leeuwen <
vanleeuwen.martin at gmail.com> wrote:

> Hi Mathieu,
>
> You can index using a tuple too, like:
>
> a[:,(0,2)]
>
> That way you can index into any columns.
>

Just to clarify:

Using a list or tuple for indexing is "fancy" indexing and returns a copy,
not a view.

To illustrate the difference:

import numpy as np

original = np.zeros((5,3))

normal_indexing = original[:,:2]
fancy_indexing = original[:,(0,2)]

normal_indexing[0] = 5
fancy_indexing[0] = 600

print original


So, modifying the "fancy_indexing" version doesn't modify the original (and
is a copy of the original, using more memory). However, modifying the
"normal_indexing" version (which is a view of the same memory as the
original) _does_ modify the original.

I believe the OP was specifically wanting a view.  (And I don't think it's
directly possible, though you can write a simple wrapper class to do it.)

Cheers,
-Joe



>
> Martin
>
> 2011/7/30 mathieu lacage <mathieu.lacage at alcmeon.com>:
> > hi,
> >
> > Let's say I have a big large array:
> >
> > a = numpy.empty((10000*1000,10))
> >
> > and I want to create a view of that array to be able to process a subset
> of
> > its data without making a copy.
> >
> > One column:
> > b = a[:,1]
> >
> > Two adjacent columns:
> > b = a[:,1:2]
> >
> > Can I do the same (no memory allocation) for two columns that are not
> > adjacent ? i.e. if I want to create an array that is a view for the 1st
> and
> > 3rd columns.
> >
> >
> > Mathieu
> >
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User at scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
> >
> >
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20110730/1bea5723/attachment.html>


More information about the SciPy-User mailing list