首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python中的平滑/噪声过滤数据

Python中的平滑/噪声过滤数据
EN

Stack Overflow用户
提问于 2018-08-21 20:45:42
回答 1查看 1.6K关注 0票数 0

我有一个包含以下数据的表

代码语言:javascript
复制
article price   wished outcome
horse    10         10
duck     15         15
child    9        15 - 21
panda    21         21
lamb     24         22
gorilla  23         23

我希望将列Price平滑到期望的Price,然后将其放入dataframe中,这样我就可以看到这些值。

请问,有没有什么内置的库方法可以平滑数据?以这种格式?

我发现了savitzky-golay滤波器、移动平均等等,但我无法对这些数据进行处理-其中x轴是某个乘积=非值。

求求你,你能帮忙吗?

谢谢!

代码语言:javascript
复制
 d = {'Price': [10, 15, 9, 21,24,23], 'Animal': ['horse', 'lamb', 'gorilla', 'child','panda','duck']}
 df = pd.DataFrame(d)

 import matplotlib.pyplot as plt 
 from scipy.optimize import curve_fit
 from scipy.interpolate import interp1d
 from scipy.signal import savgol_filter
 import numpy as np

 x = np.arange(1,len(df)+1)
 y = df['Price']

 xx = np.linspace(x.min(),x.max(), 1001)

 # interpolate + smooth
 itp = interp1d(x,y, kind='quadratic') #kind = 'linear', 'nearest' (dobre      vysledky), slinear (taky ok), cubic (nebrat), quadratic - nebrat
 window_size, poly_order = 1001, 1
 yy_sg = savgol_filter(itp(xx), window_size, poly_order)


 # or fit to a global function
 # to stejne jako scipy.optimize.curve.fit 
 def func(x, A, B, x0, sigma):
     return A+B*np.tanh((x-x0)/sigma)

 fit, _ = curve_fit(func, x, y)
 yy_fit = func(xx, *fit)

 fig, ax = plt.subplots(figsize=(7, 4))
 ax.plot(x, y, 'r.', label= 'Unsmoothed curve')
 ax.plot(xx, yy_fit, 'b--', label=r"$f(x) = A + B \tanh\left(\frac{x-x_0}     {\sigma}\right)$")
 ax.plot(xx, yy_sg, 'k', label= "Smoothed curve")
 plt.legend(loc='best')


 I am getting : AttributeError: 'range' object has no attribute 'min'

Savitzky golay产生了非常奇怪的值。窗口长度为1000

当我将window设置为len(df) +1 (为了使其为奇数)时,我会得到以下数据:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-21 20:59:31

因为下面这行代码,你会得到这个错误:x = range(1,len(df))

正如错误所告诉您的,range对象没有min属性。

但是,numpy.array()是这样做的,所以如果您将该行更改为x = np.arange(1, len(df)),则此错误(至少)将消失。

编辑:为了让函数执行您希望它做的事情,您应该将其更改为x = np.arange(1, len(df)+1)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51949000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档