1
\$\begingroup\$

I have a seemingly simple issue. Consider this list:

a = [[12.0, 5.0, 63.0], [0.1, 2.0, 7.1, 3.0, 2.3, 5.0, 8.4]]

I want to find the mean (using numpy) of all the elements in its sublists combined. In this case the result should be:

10.79

obtained as:

np.mean([12.0, 5.0, 63.0, 0.1, 2.0, 7.1, 3.0, 2.3, 5.0, 8.4])

The solution I've found is to flatten the list first and then obtain the mean, as:

np.mean([item for sublist in a for item in sublist])

but this seems unnecessarily complicated. I would've assumed that numpy.mean() could handle this case without the need to modify the list first. I tried using the argument axis to no avail.

Am I missing some functionality here?

\$\endgroup\$
5
  • 1
    \$\begingroup\$ How is that complicated? It's literally doing exactly what you specified in your problem statement, nothing more. It's also literally the simplest list comprehension you can write over a nested list. \$\endgroup\$ Jul 12 '16 at 18:50
  • \$\begingroup\$ "Complicated" in the sense that I expected numpy to automatically handle such a case via some argument. Not "complicated" as in "the code is hard to read/comprehend". \$\endgroup\$
    – Gabriel
    Jul 12 '16 at 18:51
  • \$\begingroup\$ If you use the numpy array data structures I'm pretty sure it automatically flattens the data for you without needing to pass an argument. \$\endgroup\$ Jul 12 '16 at 18:54
  • 1
    \$\begingroup\$ So just cast to np.array \$\endgroup\$ Jul 12 '16 at 18:56
  • 1
    \$\begingroup\$ @machineyearning: that won't work because the lists are different sizes. OP needs to flatten first. \$\endgroup\$ Jul 12 '16 at 19:38
4
\$\begingroup\$

Assuming your nesting is only one level deep, the concatenation can be done very easily using np.hstack. This will treat the inner lists as vectors, then concatenate them end-to-end as a 1D numpy array. You can then take the mean of the resulting array. So this will do what you want:

>>> np.hstack(a).mean()
10.79

Or equivalently (but more verbose in my opinion):

>>> np.mean(np.hstack(a))
10.79
\$\endgroup\$
2
  • \$\begingroup\$ Excellent, I hadn't though of using np.hstack(). Thank you! \$\endgroup\$
    – Gabriel
    Jul 12 '16 at 19:49
  • \$\begingroup\$ But look at what hstack does: np.concatenate([atleast_1d(_m) for _m in tup],axis=0). It does a list comprehension on your list. Actually that isn't needed here. This will do: np.mean(np.concatenate(a)). The key is understanding what this does compared to np.array(a). Most cases when you apply a numpy function to a list, it first turns that list into an array (or maybe a list of arrays). \$\endgroup\$
    – hpaulj
    Jul 17 '16 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.