sum over all but one axis

Vincent Schut schut at
Thu Oct 26 04:39:34 CDT 2006

Vincent Schut wrote:
> Hi list,
> probably I'm just missing something obvious, but I'm looking for a 
> function/construcion to sum (or better, apply any numpy function) over 
> all but one axis. Like when I have a nD array A, it will give me a 1D 
> array with the sums of A[i, ...], but the axis that is retained in the 
> result of course should be an argument to that function.
> I thought 'apply_along_axis' would do the trick, but it does not. If I 
> apply that to a 3d array, it results in a 2d array instead of a 1d array.
> Of course I can easily loop over that axis, but if possible I'd like to 
> prevent array loops...
> Vincent.
Replying to self... the 'solution' I just found is this:
1: create a list 'axes' with the numbers of all axes except the one I 
want to retain
2: use numpy.apply_over_axes(function, array, axes).flatten()

If anyone knows a better / more elegant solution or any related comments 
of course I'd still like to hear that. :-)


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo

More information about the Numpy-discussion mailing list