如何平滑数组中的值(没有多项式方程)?

2024-10-01 04:55:35 发布

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

所以基本上我有一些数据,我需要找到一种方法来平滑它(这样从它生成的线是平滑的,而不是抖动的)。当绘制出数据时,现在看起来如下:placeholder text for image

我希望它看起来像这样:placeholder text for image

我试着用thisnumpy方法得到这条线的方程,但它对我不起作用,因为曲线重复(有多个读数,所以曲线上升,饱和,然后下降,然后重复多次),所以没有一个真正的方程可以表示这一点

我也试过this,但由于上述原因,它没有起作用

该图定义如下:

gx = [] #x is already taken so gx -> graphx
gy = [] #same as above

#Put in data

#Get nice data #[this is what I need help with]

#Plot nice data and original data

plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

我认为最适用于我的解决方案的方法是获得每2个点的平均值,并将其设置为两个点的值,但我不同意这种想法-潜在值可能会丢失


Tags: 数据方法data定义is绘制原因plt
2条回答

您可以使用无限地平线过滤器

import numpy as np
import matplotlib.pyplot as plt

x = 0.85 # adjust x to use more or less of the previous value
k = np.sin(np.linspace(0.5,1.5,100))+np.random.normal(0,0.05,100)
filtered = np.zeros_like(k)
#filtered = newvalue*x+oldvalue*(1-x)
filtered[0]=k[0]
for i in range(1,len(k)):
# uses x% of the previous filtered value and 1-x % of the new value
    filtered[i] = filtered[i-1]*x+k[i]*(1-x) 

plt.plot(k)
plt.plot(filtered)
plt.show()

我算出了,通过平均4个结果,我能够显著地平滑图表。下面是一个演示:

demo

希望这能帮助任何需要它的人

相关问题 更多 >