如何在python中选择要平均的矩阵位置?

时间:2019-07-05 00:06:48

标签: python-3.x numpy

我有一个形状为(1,517)的矩阵,我只想对前11个元素取平均值,从0开始计数,即0、1、2、3等。

我尝试使用np.mean(时间[idx]),其中idx是我要平均的值的位置

print(times)
array([[1.55079361e+09, 1.55079377e+09, 1.55079394e+09, 1.55079410e+09,
        1.55079430e+09, 1.55079446e+09, 1.55079462e+09, 1.55079479e+09,
        1.55079495e+09, 1.55079512e+09, 1.55079528e+09, 1.55079544e+09,
        1.55079561e+09, 1.55079577e+09, 1.55079594e+09, 1.55079614e+09,
        1.55079630e+09, 1.55079646e+09, 1.55079663e+09, 1.55079679e+09,
        1.55079695e+09, 1.55079712e+09, 1.55079728e+09, 1.55079744e+09,
        1.55079761e+09, 1.55079781e+09, 1.55079797e+09, 1.55079814e+09,
        1.55079830e+09, 1.55079846e+09, 1.55079863e+09, 1.55079879e+09,
        1.55079895e+09, 1.55079912e+09, 1.55079928e+09, 1.55079945e+09,
        1.55079964e+09, 1.55079981e+09, 1.55079997e+09, 1.55080014e+09,
        1.55080030e+09, 1.55080046e+09, 1.55080063e+09, 1.55080079e+09,
        1.55080096e+09, 1.55080112e+09, 1.55080128e+09, 1.55080148e+09,
        1.55080164e+09, 1.55080181e+09, 1.55080197e+09, 1.55080214e+09,
        1.55080230e+09, 1.55080246e+09, 1.55080263e+09, 1.55080279e+09,
        1.55087981e+09]])

idx = mlab.find((tiempos>=1550793600) & (tiempos<1550795400))
print(idx)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10], dtype=int64)
np.mean(times[idx])

我想对idx位置中的数据值取平均值,我是新来的,我很困惑,希望您能为我提供帮助,

1 个答案:

答案 0 :(得分:0)

那是因为您有一个嵌套的数组,表示在数组内的数组,即在长度1的数组内的长度为57的数组。

len(times)
#Output
1

len(times[0])
#Output
57

所以您所做的是正确的,只需在times[0]上执行即可:

times = times[0]

现在您的代码可以使用了

idx = mlab.find((tiempos>=1550793600) & (tiempos<1550795400))
np.mean(times[idx])
#Output
1550794449.090909