Calculate mean across dimension in a 2D array

PythonArraysMultidimensional ArrayNumpyMean

Python Problem Overview


I have an array a like this:

a = [[40, 10], [50, 11]]

I need to calculate the mean for each dimension separately, the result should be this:

[45, 10.5]

45 being the mean of a[*][0] and 10.5 the mean of a[*][1].

What is the most elegant way of solving this without using a loop?

Python Solutions


Solution 1 - Python

a.mean() takes an axis argument:

In [1]: import numpy as np

In [2]: a = np.array([[40, 10], [50, 11]])

In [3]: a.mean(axis=1)     # to take the mean of each row
Out[3]: array([ 25. ,  30.5])

In [4]: a.mean(axis=0)     # to take the mean of each col
Out[4]: array([ 45. ,  10.5])

Or, as a standalone function:

In [5]: np.mean(a, axis=1)
Out[5]: array([ 25. ,  30.5])

The reason your slicing wasn't working is because this is the syntax for slicing:

In [6]: a[:,0].mean() # first column
Out[6]: 45.0

In [7]: a[:,1].mean() # second column
Out[7]: 10.5

Solution 2 - Python

Here is a non-numpy solution:

>>> a = [[40, 10], [50, 11]]
>>> [float(sum(l))/len(l) for l in zip(*a)]
[45.0, 10.5]

Solution 3 - Python

If you do this a lot, NumPy is the way to go.

If for some reason you can't use NumPy:

>>> map(lambda x:sum(x)/float(len(x)), zip(*a))
[45.0, 10.5]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionotmezgerView Question on Stackoverflow
Solution 1 - PythonaskewchanView Answer on Stackoverflow
Solution 2 - PythonAndrew ClarkView Answer on Stackoverflow
Solution 3 - PythonNPEView Answer on Stackoverflow