[Numpy-discussion] Iterate over all 1-dim views

Gael Varoquaux gael.varoquaux@normalesup....
Sun Oct 7 10:55:25 CDT 2007

On Sun, Oct 07, 2007 at 06:52:11AM -0400, Neal Becker wrote:
> Suppose I have a function F(), which is defined for 1-dim arguments.  If the
> user passes an n>1 dim array, I want to apply F to each 1-dim view.

> For example, for a 2-d array, apply F to each row and return a 2-d result.

> For a 3-d array, select each 2-d subarray and see above.  Return 3-d result.

> Any suggestions on how to code something like this in numpy?

Code your function so that it works well for 2D arrays (using axis=-1 and
co), then use a decorator on it so that if you pass it an N-d array, it
transforms it in a 2D array, passes it to the decorator, then transforms
the output back to the right shape.

The idea is quite theoretical, and I have never gotten to implement it,
because when I was facing similar problems, it didn't come to my mind,
but I think it can work in a very general way.


More information about the Numpy-discussion mailing list