[SciPy-User] creating a view of an array
Zachary Pincus
zachary.pincus at yale.edu
Sat Jul 30 17:58:15 EDT 2011
Hi,
Indexing like a[:,(0,2)] creates a copy. (The logic isn't smart enough to determine if the index tuple is regular enough that it might be able to represent it as a view...)
import numpy
a = numpy.zeros((1000,1000))
b = a[:,:3:2] # This is what you want
b.base is a # True
c = a[:,(0,2)] # Makes a copy
c.base is a # False
Pretty much any simple slice will just create a view. (Right, experts?)
In addition, you could muck with the numpy.ndarray constructor and pass base, offset, dtype, and strides parameters to make a new view on an old array that does things that normal slicing couldn't. (Like zero-length strides, etc.)
Zach
On Jul 30, 2011, at 5:48 PM, Martin van Leeuwen wrote:
> Hi Mathieu,
>
> You can index using a tuple too, like:
>
> a[:,(0,2)]
>
> That way you can index into any columns.
>
> 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
More information about the SciPy-User
mailing list