Python 中的平滑/噪声过滤数据
Smoothing / noise filtering data in Python
我有table数据如下
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,然后将其放入数据框中,以便我看到值。
拜托,是否有一些内置的库 - 平滑数据的方法?
采用这种格式?
我找到了 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 正在产生非常奇怪的值。
window 长度为 1000
当我将 window 设置为 len(df) +1 (为了它是奇数)然后我得到这些数据:
您收到该错误是因为以下行:
x = range(1,len(df))
.
正如错误告诉您的那样,range
对象没有属性 min
。
但是,numpy.array()
s 会,所以如果您将该行更改为
x = np.arange(1, len(df))
那么这个错误(至少)就会消失。
编辑:
为了使函数执行您希望它执行的操作,您应该将其更改为 x = np.arange(1, len(df)+1)
我有table数据如下
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,然后将其放入数据框中,以便我看到值。
拜托,是否有一些内置的库 - 平滑数据的方法? 采用这种格式?
我找到了 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 正在产生非常奇怪的值。 window 长度为 1000
当我将 window 设置为 len(df) +1 (为了它是奇数)然后我得到这些数据:
您收到该错误是因为以下行:
x = range(1,len(df))
.
正如错误告诉您的那样,range
对象没有属性 min
。
但是,numpy.array()
s 会,所以如果您将该行更改为
x = np.arange(1, len(df))
那么这个错误(至少)就会消失。
编辑:
为了使函数执行您希望它执行的操作,您应该将其更改为 x = np.arange(1, len(df)+1)