# very simple iteration question.

10 messages
Open this post in threaded view
|

## very simple iteration question.

 Hi.  This is a very basic question, sorry if it's irritating.  If i didn't find the answer written already somewhere on the site, please point me to it.  That'd be great. OK: how do i iterate over an axis other than 0? I have a 3D array of data[year, week, location].  I want to iterate over each year at each location and run a series of stats on the columns (on the 52 weeks in a particular year at a particular location).  'for years in data:' will get the first one, but then how do i not iterate over the 1 axis and iterate over the 2 axis instead? thanks, alex. _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

## Re: very simple iteration question.

 for i in range(52):    week_data = data[:,i,:] OR for week_data in data.transpose(1,0,2):    ...   Nadav -----הודעה מקורית----- מאת: [hidden email] בשם a g נשלח: ד 30-אפריל-08 11:11 אל: [hidden email] נושא: [Numpy-discussion] very simple iteration question.   Hi.  This is a very basic question, sorry if it's irritating.  If i didn't find the answer written already somewhere on the site, please point me to it.  That'd be great. OK: how do i iterate over an axis other than 0? I have a 3D array of data[year, week, location].  I want to iterate over each year at each location and run a series of stats on the columns (on the 52 weeks in a particular year at a particular location).  'for years in data:' will get the first one, but then how do i not iterate over the 1 axis and iterate over the 2 axis instead? thanks, alex. _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion_______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

## Re: very simple iteration question.

 In reply to this post by a g-3 2008/4/30 a g <[hidden email]>: > Hi.  This is a very basic question, sorry if it's irritating.  If i >  didn't find the answer written already somewhere on the site, please >  point me to it.  That'd be great. > >  OK: how do i iterate over an axis other than 0? > >  I have a 3D array of data[year, week, location].  I want to iterate >  over each year at each location and run a series of stats on the >  columns (on the 52 weeks in a particular year at a particular location). >   'for years in data:' will get the first one, but then how do i not >  iterate over the 1 axis and iterate over the 2 axis instead? Well, there's always for i in xrange(A.shape[1]):     do_something(A[:,i,...]) But that's kind of ugly in this day and age. I would be tempted by for a in np.rollaxis(A,1):     do_something(a) It's worth mentioning that traversing an array along an axis not the first usually results in subarrays that are not contiguous in memory. While numpy makes working with these convenient, they may not be very efficient for cache reasons (for example, modern processors load from memory - an extremely slow operation - in blocks that may be as large as 64 bytes; if you only use eight of these before moving on, your code will use much more memory bandwidth than it needs to). If this is a concern, you can usually transparently rearrange your array's memory layout to avoid it. Anne _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

## Re: very simple iteration question.

 In reply to this post by a g-3 Hi Alex, a g wrote: > Hi.  This is a very basic question, sorry if it's irritating.  If i > didn't find the answer written already somewhere on the site, please > point me to it.  That'd be great. You should look at any of the documents below and read up on array slicing. It is perhaps the most important and pervasive concept of Numpy and should be understood by all users.      Numpy Tutorial: http://www.scipy.org/Tentative_NumPy_Tutorial     Numpy for MATLAB users: http://www.scipy.org/NumPy_for_Matlab_Users     Guide to Numpy > OK: how do i iterate over an axis other than 0? > > I have a 3D array of data[year, week, location].  I want to iterate > over each year at each location and run a series of stats on the > columns (on the 52 weeks in a particular year at a particular location). >  'for years in data:' will get the first one, but then how do i not > iterate over the 1 axis and iterate over the 2 axis instead? It is not clear to me whether you want to slice or iterate over an array. Assuming you are fixing the year and location, the following code iterates over data for fixed year and location. for week in xrange(0, 52):       data[year, week, loc] Slicing is more efficient and you should use it if you can. Fixing the year and location, the following computes the mean and standard deviation across all weeks. All of the statements below yield scalars.      data[year, :, loc].mean() -- takes the mean of the data across weeks      data[year, :, loc].std() -- takes the standard deviation of the data across weeks You should download IPython and type help(numpy.array) to see one set of functions you can call on the result of a slice (sum, min, etc.). Although I don't know what statistics you are computing for sure, the following code might be useful since it computes a statistic across all weeks for each year and location value.      data.mean(axis=1) It yields a num_years by num_locations array mu where mu[y, l] is the average data value across all weeks for year y and loc l. I hope this helps. Damian _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

## Re: very simple iteration question.

 In reply to this post by a g-3 a g wrote: > OK: how do i iterate over an axis other than 0? This ties in nicely with some of the discussion about interating over matrices. It ahs been suggested that it would be nice to have iterators for matrices, so you could do: for row in M.rows:    ... and for column in M.cols:    ... If so, then wouldn't it make sense to have built in iterators for nd-arrays as well? something like: for subarray in A.iter(axis=i):     ... where axis would default to 0. This is certainly cleaner than: for j in range(A.shape[i]):      subarray = A[:,:,j,:] Wait! I have no idea how to spell that generically -- i.e. the ith index, where i is a variable. There must be a way to build a slice object dynamically, but I don't know it. How often would i be a variable, rather than hard coded? I have no idea. -Chris -- Christopher Barker, Ph.D. Oceanographer 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 [hidden email] _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

## Re: very simple iteration question.

 2008/4/30 Christopher Barker <[hidden email]>: > a g wrote: >  > OK: how do i iterate over an axis other than 0? > >  This ties in nicely with some of the discussion about interating over >  matrices. It ahs been suggested that it would be nice to have iterators >  for matrices, so you could do: > >  for row in M.rows: >    ... > >  and > >  for column in M.cols: >    ... > > >  If so, then wouldn't it make sense to have built in iterators for >  nd-arrays as well? something like: > >  for subarray in A.iter(axis=i): >     ... > >  where axis would default to 0. > >  This is certainly cleaner than: > >  for j in range(A.shape[i]): >      subarray = A[:,:,j,:] > >  Wait! I have no idea how to spell that generically -- i.e. the ith >  index, where i is a variable. There must be a way to build a slice >  object dynamically, but I don't know it. Slices can be built without too much trouble using slice(), but it's much easier to just write for subarray in np.rollaxis(A,i):     ... rollaxis() just pulls the specified axis to the front. (It doesn't do what I thought it would, which is a cyclic permutation of the axes). Anne _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

## Re: very simple iteration question.

 Anne Archibald wrote: >> it's much easier to just write > > for subarray in np.rollaxis(A,i): >     ... cool, thanks! So the answer to the OPs question: > OK: how do i iterate over an axis other than 0? > > I have a 3D array of data[year, week, location].  I want to iterate > over each year at each location  ... for loc in np.rollaxis(data, 2):      for year in np.rollaxis(data, 0): # rollaxis not required here, but                                        # for symmetry's sake...      .... I think I still like the idea of an iterator (or maybe making rollaxis a method?), but this works pretty well. -Chris -- Christopher Barker, Ph.D. Oceanographer 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 [hidden email] _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

## Re: very simple iteration question.

 On Wed, Apr 30, 2008 at 11:57:44AM -0700, Christopher Barker wrote: > I think I still like the idea of an iterator (or maybe making rollaxis a > method?), but this works pretty well. Generally, in object oriented programming, you expect a method like rollaxis to modify an object inplace. At least that would be my expectation. Gaël _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion