I have a 2-dimensional numpy array of integers and I would like to compute a 1-dimensional numpy array containing the means of each array of the 2-dimensional array, so for example
array([[1, 2]
[3, 4]])
would return
array([1.5, 3.5])
Currently, I am using a list comprehension to do this
[sum(i) / len(i) for i in lst]
which works just fine, but I am curious if there is a way to do this using broadcasting and/or numpy functions. That would also be faster for large arrays, which I plan to use this function on. Any insights would be appreciated.
a = np.array([[1, 2], [3, 4]])
np.mean(a, axis=1)
This will give you the expected result.
In a 2-D array, axis=0
indicates the column and axis=1
indicates the row.