简单移动平均二维数组

2024-05-31 23:01:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为2D数组的每一行计算一个简单的移动平均值。每行中的数据是一个单独的数据集,所以我不能只计算整个数组中的SMA,我需要在每行中分别进行计算。我尝试了for循环,但它将窗口作为行,而不是单个值

我用来计算SMA的方程式是:a1+a2+…an/n 这是我目前掌握的代码:

import numpy as np  


#make amplitude array
amplitude=[0,1,2,3, 5.5, 6,5,2,2, 4, 2,3,1,6.5,5,7,1,2,2,3,8,4,9,2,3,4,8,4,9,3]


#split array up into a line for each sample
traceno=5                  #number of traces in file
samplesno=6                #number of samples in each trace. This wont change.

amplitude_split=np.array(amplitude, dtype=np.int).reshape((traceno,samplesno))

#define window to average over:
window_size=3

#doesn't work for values that come before the window size. i.e. index 2 would not have enough values to divide by 3
#define limits:
lowerlimit=(window_size-1)
upperlimit=samplesno

i=window_size

for row in range(traceno):
  for n in range(samplesno):
    while lowerlimit<i<upperlimit:
      this_window=amplitude_split[(i-window_size):i] 

      window_average=sum(this_window)/window_size

      i+=1
      print(window_average)

此数据集的预期输出为:

[[1,    2,    3.33, 4.66]
 [3,    2.66, 2.66, 3.  ]
 [4,    6,    4.33, 3.33]
 [4.33, 5,    7,    5.  ]
 [5,    5.33, 7,    5.33]]

但我得到了:

[2.         3.         3.         4.66666667 2.66666667 3.66666667]
[2.66666667 3.66666667 5.         5.         4.         2.33333333]
[2.         4.33333333 7.         5.         6.33333333 2.33333333]

Tags: 数据inforsizenp数组windowarray
2条回答

使用向量np.ones(window_size) / window_size使用^{}应该很容易计算,但不幸的是,该函数似乎无法广播相关操作。下面是另一种用^{}计算的简单方法:

import numpy as np

amplitude = [  0,   1,   2,   3, 5.5, 6,
               5,   2,   2,   4,   2, 3,
               1, 6.5,   5,   7,   1, 2,
               2,   3,   8,   4,   9, 2,
               3,   4,   8,   4,   9, 3]
traceno = 5
samplesno = 6
amplitude_split = np.array(amplitude, dtype=np.int).reshape((traceno, samplesno))
window_size = 3
# Scale down by window size
a = amplitude_split * (1.0 / window_size)
# Cumsum across columns
b = np.cumsum(a, axis=1)
# Add an initial column of zeros
c = np.pad(b, [(0, 0), (1, 0)])
# Take difference to get means
result = c[:, window_size:] - c[:, :-window_size]
print(result)
# [[1.         2.         3.33333333 4.66666667]
#  [3.         2.66666667 2.66666667 3.        ]
#  [4.         6.         4.33333333 3.33333333]
#  [4.33333333 5.         7.         5.        ]
#  [5.         5.33333333 7.         5.33333333]]

您可以使用卷积对window_size中的[1, 1, ..., 1]进行卷积,然后将其除以window_size以获得平均值(无需循环):

from scipy.signal import convolve2d

window_average = convolve2d(amplitude_split, np.ones((1, window_size)), 'valid') / window_size)

ones的卷积基本上是将窗口中的元素相加

输出:

[[1.         2.         3.33333333 4.66666667]
 [3.         2.66666667 2.66666667 3.        ]
 [4.         6.         4.33333333 3.33333333]
 [4.33333333 5.         7.         5.        ]
 [5.         5.33333333 7.         5.33333333]]

相关问题 更多 >