我的数据表如下
article price wished outcome
horse 10 10
duck 15 15
child 9 15 - 21
panda 21 21
lamb 24 22
gorilla 23 23
我想将“价格”列平滑到希望的价格,然后将其放入数据框,以便查看值。
请,有一些内置的库-使数据平滑的方法吗? 以这种格式?
我发现了savitzky-golay滤波器,移动平均线等。 但是我无法在这类数据上实现-x轴是某个乘积=不值。
请,您能帮忙吗?
谢谢!
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(以使其为奇数)时,我将获得以下数据:
答案 0 :(得分:1)
由于以下行,您收到该错误:
x = range(1,len(df))
。
错误告诉您,range
对象没有属性min
。
但是,numpy.array()
个是,所以如果您将该行更改为
x = np.arange(1, len(df))
,然后此错误(至少)将消失。
编辑:
为了使函数执行您想要的操作,应将其更改为x = np.arange(1, len(df)+1)