[Numpy-discussion] iterate over multiple arrays

Bruce Southey bsouthey@gmail....
Tue Sep 13 08:27:41 CDT 2011


On 09/13/2011 01:53 AM, David Froger wrote:
> Thank you Olivier and Robert for your replies!
>
> Some remarks about the dictionnary solution:
>
> from numpy import *
>
> def f(arr):
>       return arr + 100.
>
> arrs = {}
> arrs['a'] = array( [1,1,1] )
> arrs['b'] = array( [2,2,2] )
> arrs['c'] = array( [3,3,3] )
> arrs['d'] = array( [4,4,4] )
>
> for key,value in arrs.iteritems():
>      arrs[key] = f(value)
>
> 1. about the memory
> Memory is first allocated with the array functions:
>      arrs['a'] = array( [1,1,1] )
>      arrs['b'] = array( [2,2,2] )
>      arrs['c'] = array( [3,3,3] )
>      arrs['d'] = array( [4,4,4] )
>
> Are there others memory allocations with this assignemnt:
>      arrs[key] = f(value)
> or is the already allocated memory used to store the result of f(value)?
>
> In other words, if I have N arrays of the same shape, each of them costing
> nbytes of memory, does it use N*nbytes memory, or 2*N*bytes?
>
> I think this is well documented on the web and I can find it....
>
> 2. about individual array
> The problem is that now, if one want to use a individual array, one have now to
> use:
>      arrs['a']
> instead of just:
>      a
> So I'm sometime tempted to use locals() instead of arrs...
>
Perhaps not quite what you want but you do have the option of a 
structured or record array instead of a dict if the individual arrays 
are the same dimensions. See for example:
http://www.scipy.org/RecordArrays

 >>> import numpy as np
 >>> img = np.array([(1,2,3,4), (1,2,3,4), (1,2,3,4), (1,2,3,4)], 
[('a',int),('b',int),('c',int), ('d',int)])
 >>> img.dtype.names
('a', 'b', 'c', 'd')
 >>> img['a']
array([1, 1, 1, 1])
 >>> rimg = img.view(np.recarray) #take a view
 >>> rimg.a
array([1, 1, 1, 1])
 >>> rimg.b
array([2, 2, 2, 2])

Other links:
http://docs.scipy.org/doc/numpy/user/basics.rec.html
http://www.scipy.org/Cookbook/Recarray

Bruce




More information about the NumPy-Discussion mailing list