Suppose I have a function F(), which is defined for 1dim arguments. If the user passes an n>1 dim array, I want to apply F to each 1dim view. For example, for a 2d array, apply F to each row and return a 2d result. For a 3d array, select each 2d subarray and see above. Return 3d result. Any suggestions on how to code something like this in numpy?
On Sun, Oct 07, 2007 at 06:52:11AM 0400, Neal Becker wrote:
Suppose I have a function F(), which is defined for 1dim arguments. If the user passes an n>1 dim array, I want to apply F to each 1dim view.
For example, for a 2d array, apply F to each row and return a 2d result.
For a 3d array, select each 2d subarray and see above. Return 3d 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 Nd 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. Gaël
On Sun, Oct 07, 2007 at 06:52:11AM 0400, Neal Becker wrote:
Suppose I have a function F(), which is defined for 1dim arguments. If the user passes an n>1 dim array, I want to apply F to each 1dim view.
For example, for a 2d array, apply F to each row and return a 2d result.
For a 3d array, select each 2d subarray and see above. Return 3d result.
Any suggestions on how to code something like this in numpy?
Not the most efficient way, but easy to read and understand: import numpy as N def func(a): return a.shape z = N.zeros((2,2,2,2)) print N.array([func(sub) for sub in z]) Regards Stéfan
participants (3)

Gael Varoquaux

Neal Becker

Stefan van der Walt