[Numpy-discussion] Array views

Christopher Barker Chris.Barker at noaa.gov
Sat Mar 26 14:36:18 EDT 2011

On 3/26/11 10:12 AM, Pauli Virtanen wrote:
> On Sat, 26 Mar 2011 13:10:42 -0400, Hugo Gagnon wrote:
> [clip]
>> a1 = b[:,0]
>> a2 = b[:,1]
>> ...
>> and it works but that doesn't help me for my problem. Is there a way to
>> reformulate the first code snippet above but with shallow copying?
> No. You need an 2-D array to "own" the data. The second way is the
> approach to use if you want to share the data.

exactly -- but to clarify, it's not just about ownership, it's about 
layout of the data in memory. the data in a numpy array needs to be laid 
out in memory as one block, with consitent strides from one element to 
the next, one row to the next, etc.

When you create an array from scratch (like your 2-d array here), you 
get one big block of memory. If you create each row separately, they 
each have their own block of memory that are unrelated -- there is no 
way to put those together into one block with consistent strides.

So you need to create that big block first (the 2-d array), then you can 
reference parts of it for each row.

See my previous note for a bit more discussion.

Oh, and maybe the little presentation and sample code I gave to the 
Seattle Python Interest group will help:



Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov

More information about the NumPy-Discussion mailing list