每个数组项的平均值和标准差 [英] Numpy mean and std over every terms of arrays

查看:233
本文介绍了每个数组项的平均值和标准差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维数组(相同形状)的列表,并希望获得所有项的均值和偏差,其结果数组的形状与输入相同.我无法从文档中了解这是否可行.我所有使用axis和keepdims参数的尝试都会产生不同形状的结果.

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.

例如,我希望具有:均值([x,x])等于x,以及std([x,x])零,形状像x.

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 ?

示例:

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

我想要的是[[1.5,2.5],[3.5,4.5]].

推荐答案

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]])

这适用于任意长度的列表.对于仅两个阵列, (x+y)/2.0更快:

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

这篇关于每个数组项的平均值和标准差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆