Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 不同区域不同宽度的python加权移动平均_Python 3.x_Matplotlib - Fatal编程技术网

Python 3.x 不同区域不同宽度的python加权移动平均

Python 3.x 不同区域不同宽度的python加权移动平均,python-3.x,matplotlib,Python 3.x,Matplotlib,我试图对一个高度振荡的数据进行振荡分析。振荡不均匀,在初始区域振荡较少 x = np.linspace(0, 1000, 1000001) y = some oscillating data say, sin(x^2) (原始数据文件太大,无法上传) 我想取函数的加权移动平均值,并绘制它。最初,函数的周期较大,因此我希望在较大的时间间隔内获取avarage。而我可以用较小的时间间隔来处理后者 我在以下帖子中找到了一个可能的优雅解决方案: 但是,我希望在x的不同区域有不同的宽度。当x在(010

我试图对一个高度振荡的数据进行振荡分析。振荡不均匀,在初始区域振荡较少

x = np.linspace(0, 1000, 1000001)
y = some oscillating data say, sin(x^2)
(原始数据文件太大,无法上传)

我想取函数的加权移动平均值,并绘制它。最初,函数的周期较大,因此我希望在较大的时间间隔内获取avarage。而我可以用较小的时间间隔来处理后者

我在以下帖子中找到了一个可能的优雅解决方案:

但是,我希望在x的不同区域有不同的宽度。当x在(0100)之间时,我想要宽度=0.6,而当x在(101300)之间时,宽度=0.2,依此类推

这就是我试图实现的(用我有限的编程知识!)

def加权移动平均值(x,y,步长=0.05):#更改宽度以控制平均值
bin_中心=np.arange(np.min(x),np.max(x)-0.5*步长,步长)+0.5*步长
bin_平均值=np.零(len(bin_中心))
#我们将用高斯函数加权
def高斯(x,amp=1,平均值=0,σ=1):
返回amp*np.exp(-(x均值)**2/(2*西格玛**2))
如果x.any()<100:
对于范围(0,len(bin_中心))中的索引:
bin_中心=bin_中心[索引]
权重=高斯分布(x,平均值=宾_中心,σ=0.6)
bin_平均[指数]=np.平均值(y,权重=权重)
其他:
对于范围(0,len(bin_中心))中的索引:
bin_中心=bin_中心[索引]
权重=高斯分布(x,平均值=宾_中心,σ=0.1)
bin_平均[指数]=np.平均值(y,权重=权重)
返回(宾尤中心,宾尤平均值)

不用说,这是行不通的!我得到了第一个sigma值的曲线图。请帮助……

下面的代码片段应该或多或少地完成您尝试的操作。您的代码中主要存在一个逻辑问题,
x。any()<100
将始终是
True
,因此您永远不会执行第二部分

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 1000)
y = np.sin(x**2)

def gaussian(x,amp=1,mean=0,sigma=1):
    return amp*np.exp(-(x-mean)**2/(2*sigma**2))

def weighted_average(x,y,step_size=0.3):
    weights = np.zeros_like(x)
    bin_centers  = np.arange(np.min(x),np.max(x)-.5*step_size,step_size)+.5*step_size
    bin_avg = np.zeros_like(bin_centers)

    for i, center in enumerate(bin_centers):
        # Select the indices that should count to that bin
        idx = ((x >= center-.5*step_size) & (x <= center+.5*step_size))

        weights = gaussian(x[idx], mean=center, sigma=step_size)
        bin_avg[i] = np.average(y[idx], weights=weights)
    return (bin_centers,bin_avg)

idx = x <= 4
plt.plot(*weighted_average(x[idx],y[idx], step_size=0.6))

idx = x >= 3
plt.plot(*weighted_average(x[idx],y[idx], step_size=0.1))

plt.plot(x,y)
plt.legend(['0.6', '0.1', 'y'])
plt.show()


当然,您可以根据局部方差自适应地更改
a
。还要注意的是,根据
a
x

中的步长,这有一个先验的小偏差。对于堆栈溢出,您的问题有点过于笼统和面向数学。试着用一个明确的预期结果来编辑你的帖子(比如“我想生成一个由bla-bla-bla组成的列表”)。此外,您的职能范围也不清楚。“返回(bin_中心,bin_平均值)”应该在函数中吗?我已经编辑了这个问题。您也可以参考链接问题了解初始详细信息。谢谢您的回答。
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 1000)
y = np.sin(x**2)

def gaussian(x,amp=1,mean=0,sigma=1):
    return amp*np.exp(-(x-mean)**2/(2*sigma**2))

def weighted_average(x,y,step_size=0.3):
    weights = np.zeros_like(x)
    bin_centers  = np.arange(np.min(x),np.max(x)-.5*step_size,step_size)+.5*step_size
    bin_avg = np.zeros_like(bin_centers)

    for i, center in enumerate(bin_centers):
        # Select the indices that should count to that bin
        idx = ((x >= center-.5*step_size) & (x <= center+.5*step_size))

        weights = gaussian(x[idx], mean=center, sigma=step_size)
        bin_avg[i] = np.average(y[idx], weights=weights)
    return (bin_centers,bin_avg)

idx = x <= 4
plt.plot(*weighted_average(x[idx],y[idx], step_size=0.6))

idx = x >= 3
plt.plot(*weighted_average(x[idx],y[idx], step_size=0.1))

plt.plot(x,y)
plt.legend(['0.6', '0.1', 'y'])
plt.show()
x = np.linspace(0, 60, 1000)
y = np.sin(x**2)

z = np.zeros_like(x)
z[0] = x[0]

for i, t in enumerate(x[1:]):
    a=.2
    z[i+1] = a*y[i+1] + (1-a)*z[i]

plt.plot(x,y)
plt.plot(x,z)
plt.legend(['data', 'moving average'])
plt.show()