- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 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))
这是数据图表
您可以注意到图表并未平滑线条
最佳答案
您认为 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()
情节如下:
绿色是原始数据,红色是样条线,黄色是 UnivariateSpline,洋红色是 gaussian_1d 滤波后的数据。如果您查找这些函数,可能会发现诸如 sigma 之类的参数,您可以更改这些参数以进一步平滑数据。谷歌一下文档。
关于python - 如何在Python中使用高斯kde内核设置带宽来平滑线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32900854/
编写求解线性代数方程组的高斯-乔丹方法的任务是我选择用来推进学习 J 的一项练习。系统为 Ax=b,其中 A 是 n-by-n 矩阵,b 和未知的 x 是 n-向量。首先,我从带有控制结构的最简单形式
祝大家新年快乐! :) 我正在 Matlab 中编写 Gauss-Seidel 函数,但遇到了一些问题。 当精度达到 6 位小数时,迭代必须停止。这意味着x-xprevious的无限范数(要求使用它)
我正在尝试使用 scipy 和曲线拟合对我的数据进行高斯拟合,这是我的代码: import csv import numpy as np import matplotlib.pyplot as plt
[已解决,谢谢] 我在 C++ 中开发了下面的代码来使用高斯-塞德尔方法求解线性方程,但我似乎在填充数组时在运行时遇到了一个我无法弄清楚的问题。这是我的代码... #include int main(
我必须设计一种算法作为正向消元法的扩展,在矩阵上进行高斯约旦消元法。我的程序正在执行并创建数字的对角线,但它们并不都是 1。它也不会访问第一行和第一列以将它们更改为 0。最后一列,也就是答案所在的那一
我已经按照 Nixon Aguado 的算法实现了一个高斯滤波器。算法(找到此处描述的模板后 gaussian template )如下。 我相信伪代码是 MATLAB 风格的。 function c
在平滑图像时,我应该应用高斯和双边滤波器等哪种颜色空间版本(灰度、RGB、HSV 等)以获得最佳的去噪效果?是有一个总体趋势,还是在不同情况下会发生变化? 此外,您建议在图像处理中使用什么滤镜和色彩空
我需要根据 Java 中的正态分布对网格(MXN 矩阵)的单元格进行采样。 我知道the Apache Math library具有对一维(1D)值进行采样的函数,因此对于 vector 来说很好,但
我可以使用 random.gauss(mu, sigma) 函数生成高斯数据,但是如何生成二维高斯数据?有这样的功能吗? 最佳答案 如果你可以使用numpy,有numpy.random.multiva
为什么要使用滤波 消除图像中的噪声成分叫作图像的平滑化或滤波操作。信号或图像的能量大部分集中在幅度谱的低频和中频段是很常见的,而在较高频段,感兴趣的信息经常被噪声淹没。因此一个能降低高频成分幅度的
我正在执行高斯混合模型分类,并基于此,在 MATLAB 中使用“mvnpdf”函数。 据我所知,该函数返回传递给它的数据点或元素的多变量概率密度。 但是我试图在 C 上重新创建它,并且我假设 mvnp
I am using rbf,Support Vector machine for large training set=1135x9 matrix and test set{95x9}. I am
我现在多次偶然发现使用 scipy.curve_fit 在 python 中进行拟合比使用其他工具(例如根 ( https://root.cern.ch/ ) 例如,在拟合高斯分布时,使用 scipy
我想在 MATLAB 中绘制高斯波函数的二维表示。我希望 2D 图为一种颜色(绿色),远离高斯中心变得透明。 当我使用 imagesc 时(就像在下一个代码中一样),我在黑色方 block 上得到了一
如果我有数据(每日股票图表是一个很好的例子,但它可以是任何东西),其中我只知道 X 单位销售的范围(高 - 低)但我不知道确切的价格出售的任何给定元素。为简单起见,假设价格范围包含足够的桶(例如,40
这个问题在这里已经有了答案: Impulse, gaussian and salt and pepper noise with OpenCV (10 个回答) 关闭6年前。 我想知道 Python 中
我是一名优秀的程序员,十分优秀!