相关疑难解决方法(0)

python中的加权移动平均线

我有基本随机间隔采样的数据.我想用numpy(或其他python包)来计算加权移动平均线.我有一个移动平均线的粗略实现,但我很难找到一个好的方法来进行加权移​​动平均线,因此朝向边框中心的值的加权大于边缘的值.

在这里,我生成一些样本数据,然后采用移动平均线.我怎样才能最轻松地实现加权移动平均线?谢谢!

import numpy as np
import matplotlib.pyplot as plt

#first generate some datapoint for a randomly sampled noisy sinewave
x = np.random.random(1000)*10
noise = np.random.normal(scale=0.3,size=len(x))
y = np.sin(x) + noise

#plot the data
plt.plot(x,y,'ro',alpha=0.3,ms=4,label='data')
plt.xlabel('Time')
plt.ylabel('Intensity')

#define a moving average function
def moving_average(x,y,step_size=.1,bin_size=1):
    bin_centers  = np.arange(np.min(x),np.max(x)-0.5*step_size,step_size)+0.5*step_size
    bin_avg = np.zeros(len(bin_centers))

    for index in range(0,len(bin_centers)):
        bin_center = bin_centers[index]
        items_in_bin = y[(x>(bin_center-bin_size*0.5) ) & (x<(bin_center+bin_size*0.5))]
        bin_avg[index] = np.mean(items_in_bin)

    return bin_centers,bin_avg

#plot the moving average
bins, average = moving_average(x,y)
plt.plot(bins, average,label='moving average')

plt.show() …
Run Code Online (Sandbox Code Playgroud)

python numpy scipy pandas

16
推荐指数
1
解决办法
2万
查看次数

标签 统计

numpy ×1

pandas ×1

python ×1

scipy ×1