In [1]:
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib import rc
%matplotlib auto
import numpy as np
from sklearn.neighbors import KernelDensity
from scipy.stats import gaussian_kde
rc('font', **{'family': 'serif'})
rc('text', usetex=True)
rc('text.latex', unicode=True)
rc('text.latex', preamble='\\usepackage[utf8]{inputenc}')
rc('text.latex', preamble='\\usepackage[russian]{babel}')
In [2]:
def plot_kernels():
fig, ax = plt.subplots(figsize=(8, 6))
# fig, ax = plt.plot()
ax.grid(color='white', linestyle='-', linewidth=10)
X_src = np.zeros((1, 1))
x_grid = np.linspace(-3, 3, 1000)
for kernel in ['gaussian', 'tophat', 'epanechnikov',
'linear', 'cosine']:
log_dens = KernelDensity(kernel=kernel).fit(X_src).score_samples(x_grid[:, None])
ax.plot(x_grid, np.exp(log_dens), lw=3, alpha=0.5)
ax.set_ylim(0, 1.05)
ax.set_xlim(-2.9, 2.9)
ax.legend([u"гаусове", u"пряме", u"оптимальне", u"трикутне", u"квадратичне"])
plt.show()
In [5]:
plot_kernels()