Numpy mean and std over every terms of arrays

I have a list of 2 dimensional arrays (same shape), and would like to get the mean and deviation for all terms, in a result array of the same shape as the inputs. I have trouble understanding from the doc whether this is possible. All my attempts with axis and keepdims parameters produce results of different shapes.

I would like for example to have: mean([x, x]) equal to x, and std([x, x]) zeroes shaped like x.

Is this possible without reshaping the arrays ? If not, how to do it with reshaping ?

Example:

>> x= np.array([[1,2],[3,4]])
>>> y= np.array([[2,3],[4,5]])
>>> np.mean([x,y])
3.0

I want [[1.5,2.5],[3.5,4.5]] instead.


As Divikar points out, you can pass the list of arrays to np.mean and specify axis=0 to average over corresponding values from each array in the list:

In [13]: np.mean([x,y], axis=0)
Out[13]: 
array([[ 1.5,  2.5],
       [ 3.5,  4.5]])

This works for lists of arbitrary length. For just two arrays, (x+y)/2.0 is faster:

In [20]: %timeit (x+y)/2.0
100000 loops, best of 3: 1.96 µs per loop

In [21]: %timeit np.mean([x,y], axis=0)
10000 loops, best of 3: 21.6 µs per loop
链接地址: http://www.djcxy.com/p/68104.html

上一篇: 在numpy中对数组的子集进行平均

下一篇: Numpy是指数组中的每一项