【发布时间】:2017-12-15 18:24:22
【问题描述】:
我正在努力为小输入范围实现 KDE 的 scikit-learn 实现。以下代码有效。将除数变量增加到 100 并且 KDE 很困难:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
from sklearn.neighbors import KernelDensity
# make data:
np.random.seed(0)
divisor = 1
gaussian1 = (3 * np.random.randn(1700))/divisor
gaussian2 = (9 + 1.5 * np.random.randn(300)) / divisor
gaussian_mixture = np.hstack([gaussian1, gaussian2])
# illustrate proper KDE with seaborn:
sns.distplot(gaussian_mixture);
# now implement in sklearn:
x_grid = np.linspace(min(gaussian1), max(gaussian2), 200)
kde_skl = KernelDensity(bandwidth=0.5)
kde_skl.fit(gaussian_mixture[:, np.newaxis])
# score_samples() returns the log-likelihood of the samples
log_pdf = kde_skl.score_samples(x_grid[:, np.newaxis])
pdf = np.exp(log_pdf)
fig, ax = plt.subplots(1, 1, sharey=True, figsize=(7, 4))
ax.plot(x_grid, pdf, linewidth=3, alpha=0.5)
工作正常。但是,将“除数”变量更改为 100,scipy 和 seaborn 可以处理较小的数据值。我的实现无法使用 Sklearn 的 KDE:
我做错了什么,我该如何纠正?我需要 KDE 的 sklearns 实现,所以不能使用其他算法。
【问题讨论】:
标签: python scikit-learn gaussian kde