[英]How to construct a kernel density estimation with a gaussian kernel?
我想在这里使用高斯 kernel 构造未知密度 f 的估计量:
我试图使用这两个公式来实现代码,但它让我无处可去:
n = 100
X = np.random.normal(0,1,n)
a = min(X)
b = max(X)
sigma = 1
Xplot = np.linspace(a,b, num = len(X))
ftrue = np.zeros((100,1))
ftrue = (np.exp(-(Xplot**2))/(2*(sigma**2)))/(np.sqrt(2*np.pi)*sigma)
#np.exp(-0.5*Xplot**2)/np.sqrt(2*np.pi)
def K (x,y,sigma) :
return((np.exp(-((x-y)**2))/(2*(sigma**2)))/(np.sqrt(2*np.pi)*sigma))
fest = np.zeros((100,1))
fest2 = np.zeros((100,1))
mat = np.zeros((100,n))
KER = np.zeros((100,n))
for i in range(100):
for j in range(n):
U = (Xplot[i]-X[j])/sigma
mat[i,j] = np.exp(-0.5*U**2)/np.sqrt(2*np.pi)
# KER[i,j] = KernelDensity(U, kernel="gaussian")
KER[i,j] = (np.mean(K(Xplot[i],X[j],sigma))) / sigma # I'm not sure about that
fest = mat.mean(axis=1)
fest2 = KER.mean(axis=1)
plt.plot(Xplot,ftrue,'r')
plt.plot(Xplot,fest,'b')
plt.plot(Xplot,fest2,'g')
你知道如何构建这个 kernel 密度估计器吗?
谢谢 !
我自己图书馆的无耻插件。
这里有用于 kernel 密度估计的效用函数。
from local_models.local_models import GaussianKernel
from local_models.utils import kernel_density
import numpy as np
import matplotlib.pyplot as plt
X_train = np.random.normal(size=100).reshape(-1,1)
y_train = np.ones_like(X_train).flatten()
X_test = np.linspace(-4,4,1000).reshape(-1,1)
kernel = GaussianKernel(bandwidth = 0.1)
KDE = kernel_density(X_test, X_train, kernel)
plt.plot(X_test, KDE)
plt.scatter(X_train, y_train, c='r')
plt.show()
我试过了,这是正确的吗? :
def kernel(x, y, sigma):
return (1 / np.sqrt(2 * np.pi * sigma)) * np.exp(-(x - y)**2 / (2 * sigma**2))
n=100
X=np.random.normal(0,1,n)
a = min(X)
b = max(X)
Xplot = np.linspace(a,b, num = len(X))
sigma=[0.1,1,4]
t = len(X)
print(t)
ftrue = np.zeros((100,1))
ftrue = np.exp(-0.5*Xplot**2)/np.sqrt(2*np.pi)
plt.plot(Xplot,ftrue,'r')
plt.legend(loc='upper left')
y=[np.mean(kernel(X, Xplot[i], sigma[0]))/sigma[0] for i in range(t)]
plt.plot(Xplot,y,'b')
plt.legend(loc='upper left')
y=[np.mean(kernel(X, Xplot[i], sigma[1]))/sigma[1] for i in range(t)]
plt.plot(Xplot,y, 'orange')
plt.legend(loc='upper left')
y=[np.mean(kernel(X, Xplot[i], sigma[2]))/sigma[2] for i in range(t)]
plt.plot(Xplot,y, 'green' )
plt.legend(loc='upper left')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.