Means of asymmetric arrays in numpy
<p>I have an asymmetric 2d array in numpy, as in some arrays are longer than others, such as: <code>[[1, 2], [1, 2, 3], ...]</code></p>

<p>But numpy doesn't seem to like this:</p>

<pre><code>import numpy as np

foo = np.array([[1], [1, 2]])
foo.mean(axis=1)
</code></pre>

<p>Traceback:</p>

<pre><code>Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/home/tom/.virtualenvs/nlp/lib/python3.5/site-packages/numpy/core/_methods.py", line 56, in _mean
    rcount = _count_reduce_items(arr, axis)
  File "/home/tom/.virtualenvs/nlp/lib/python3.5/site-packages/numpy/core/_methods.py", line 50, in _count_reduce_items
    items *= arr.shape[ax]
IndexError: tuple index out of range
</code></pre>

<p>Is there a nice way to do this or should I just do the maths myself?</p>

<p>We could use an almost vectorized approach based upon <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.ufunc.reduceat.html" rel="nofollow"><code>np.add.reduceat</code></a> that takes care of the irregular length <em>subarrays</em>, for which we are calculating the average values. <code>np.add.reduceat</code> sums up elements in those intervals of irregular lengths after getting a <code>1D</code> flattened version of the input array with <code>np.concatenate</code>. Finally, we need to divide the summations by the lengths of those subarrays to get the average values.</p>

<p>Thus, the implementation would look something like this -</p>

<pre><code>lens = np.array(map(len,foo)) # Thanks to @Kasramvd on this!
vals = np.concatenate(foo)
shift_idx = np.append(0,lens[:-1].cumsum())
out = np.add.reduceat(vals,shift_idx)/lens.astype(float)
</code></pre>

python numpy