numpy apply_over_axes forcing keepdims=True?
<p>I have the following code </p>

<pre><code>import numpy as np
import sys

def barycenter( arr, axis=0 ) :
    bc = np.mean( arr, axis, keepdims=False )
    print( "src shape:", arr.shape, ", **** trg shape:", bc.shape, "****" )
    sys.stdout.flush()
    return bc

a = np.array([[[0.1, 0.2, 0.3], [0.2, 0.3, 0.4]],
              [[0.4, 0.4, 0.4], [0.7, 0.6, 0.8]]], np.float)

e = barycenter( a, 2 )
print( "direct application =", e, "**** (trg shape =", e.shape, ") ****\n" )
f = np.apply_over_axes( barycenter, a, 2 )
print( "application through apply_over_axes =", f, "**** (trg shape =", f.shape, ") ****\n" )
</code></pre>

<p>which produces the following output</p>

<pre><code>src shape: (2, 2, 3) , **** trg shape: (2, 2) ****
direct application = [[ 0.2  0.3]
 [ 0.4  0.7]] **** (trg shape = (2, 2) ) ****

src shape: (2, 2, 3) , **** trg shape: (2, 2) ****
application through apply_over_axes = [[[ 0.2]
  [ 0.3]]

 [[ 0.4]
  [ 0.7]]] **** (trg shape = (2, 2, 1) ) ****
</code></pre>

<p>So the return value of the function <code>barycenter</code> is different from what is obtained with <code>apply_over_axes( barycenter, ...</code>.</p>

<p>Why is that so?</p>

<p>The result follows directly from the doc:</p>

<blockquote>
  <p>func is called as res = func(a, axis), where axis is the first element
  of axes. The result res of the function call must have either the same
  dimensions as a or one less dimension. If res has one less dimension
  than a, a dimension is inserted before axis. The call to func is then
  repeated for each axis in axes, with res as the first argument.</p>
</blockquote>

<p>Your func reduces the dimension by 1, so apply_over_axes inserts a dimension.</p>

python numpy