gpt4 book ai didi

python - 如何在Python中使用高斯kde内核设置带宽来平滑线条

转载 作者:行者123 更新时间:2023-12-01 04:27:22 24 4
gpt4 key购买 nike

我正在尝试使用 python gaussian_kde 平滑以下数据,但是它无法正常工作,看起来 kde 正在对整个数据集的分布进行重新采样,而不是对每个点使用带宽并给出权重平滑

from scipy.stats import gaussian_kde
import matplotlib.pyplot as plt
import numpy as np
y=[ 191.78 , 191.59, 191.59, 191.41, 191.47, 191.33, 191.25 \
,191.33 , 191.48 , 191.48, 191.51, 191.43, 191.42, 191.54 \
,191.5975, 191.555, 191.52 , 191.25 , 191.15 , 191.01 ]
x = np.linspace(1 ,20,len(y))
kde= gaussian_kde(y)
kde.set_bandwidth(bw_method=kde.factor / 3)

fig, ax = plt.subplots(figsize=(10, 10))
ax.legend(loc='center left', bbox_to_anchor=(1.05, 0.5), frameon=False)
ax.scatter(x, y, color='black', label='data')
ax.plot(x,y,color='red')
ax.plot(x,kde(x))

这是数据图表

Chart of the data without smoothing

您可以注意到图表并未平滑线条

Chart after smoothing

最佳答案

您认为 kde_gaussian 平滑了一条线,但它实际上所做的是平滑数据集的密度分布估计。您的数据不是这样的数据集,而是 x/y 坐标。

以下是平滑线性数据的一些方法示例:

#from scipy.stats import gaussian_kde
import matplotlib.pyplot as plt
import numpy as np

from scipy import interpolate

from scipy import ndimage

y=[ 191.78 , 191.59, 191.59, 191.41, 191.47, 191.33, 191.25 \
,191.33 , 191.48 , 191.48, 191.51, 191.43, 191.42, 191.54 \
,191.5975, 191.555, 191.52 , 191.25 , 191.15 , 191.01 ]
x = np.linspace(1 ,20,len(y))

# convert both to arrays
x_sm = np.array(x)
y_sm = np.array(y)

# resample to lots more points - needed for the smoothed curves
x_smooth = np.linspace(x_sm.min(), x_sm.max(), 200)

# spline - always goes through all the data points x/y
y_spline = interpolate.spline(x, y, x_smooth)

spl = interpolate.UnivariateSpline(x, y)

sigma = 2
x_g1d = ndimage.gaussian_filter1d(x_sm, sigma)
y_g1d = ndimage.gaussian_filter1d(y_sm, sigma)

fig, ax = plt.subplots(figsize=(10, 10))
ax.legend(loc='center left', bbox_to_anchor=(1.05, 0.5), frameon=False)

plt.plot(x_sm, y_sm, 'green', linewidth=1)
plt.plot(x_smooth, y_spline, 'red', linewidth=1)
plt.plot(x_smooth, spl(x_smooth), 'yellow', linewidth=1)
plt.plot(x_g1d,y_g1d, 'magenta', linewidth=1)

plt.show()

情节如下:

enter image description here

绿色是原始数据,红色是样条线,黄色是 UnivariateSpline,洋红色是 gaussian_1d 滤波后的数据。如果您查找这些函数,可能会发现诸如 sigma 之类的参数,您可以更改这些参数以进一步平滑数据。谷歌一下文档。

关于python - 如何在Python中使用高斯kde内核设置带宽来平滑线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32900854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com