Python的双重求和 [英] Double Summation in Python

查看:366
本文介绍了Python的双重求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写代码以进行两次求和(请参见图片)

其中; M是主题, N是审判, Yijt是被测波形数据(3d数组)

到目前为止,我有; 给定Y是按Y [主题,试验,时间]

排列的数据

# ranges:
I = len(Y)
J = len(Y[0])

Y_i_vals = 0

for i in range(M):
    for j in range(N):
        Y_i_vals = Y_i_vals +Y[i][j]
Yt = (1.0/(M*N)) * Y_i_vals

这似乎不是执行此操作的最有效方法,我也不确定它是否给出了正确的结果.

解决方案

如果您使用的是numpy,请这样做

np.mean(Y)

此外,最好将示例输入和预期输出数据添加到您的问题中.

如果您希望每个t都可以使用它,则可以执行np.mean(np.mean(a, axis=0), axis=0) ,或者如@ophion所述,可以在较新版本(1.71及更高版本)的NumPy中将其缩短为np.mean(a, axis=(0, 1)).

I am trying to write a code to conduct a double summation (see pic)

in which; M is the subjects, N is the Trials, Yijt is the measured wave form data (3d array)

so far I have; Given Y is the data arranged as Y[subjects, trials, time]

# ranges:
I = len(Y)
J = len(Y[0])

Y_i_vals = 0

for i in range(M):
    for j in range(N):
        Y_i_vals = Y_i_vals +Y[i][j]
Yt = (1.0/(M*N)) * Y_i_vals

this doesnt seem the most effective way to do this, nor am i certain it is giving the correct result.

解决方案

If you're using numpy just do

np.mean(Y)

Also, it's good to add sample input and expected output data to your question.

If you want means for each t you can do np.mean(np.mean(a, axis=0), axis=0) , or as noted by @ophion you can shorten this to np.mean(a, axis=(0, 1)) in newer (1.71 and on) versions of NumPy.

这篇关于Python的双重求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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