<jupyter_start><jupyter_text>Kernel Density EstimationStatTools provides the `KernelDensityEstimator` class in its `smoothing` module to estimate the density of a univariate data sample.<jupyter_code>import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="ticks", palette="muted", color_codes=True)

from stattools.smoothing import KernelDensityEstimator
from stattools.datasets import load_old_faithful
from stattools.visualization import func_plot<jupyter_output><empty_output><jupyter_text>Example: simulated univariate Gaussian data<jupyter_code># Generate data
dist = st.norm(loc=1, scale=3)
data = dist.rvs(size=500, random_state=np.random.RandomState(0))

# Plot histogram of sample and true density
plt.figure()
plt.hist(data, bins="auto", alpha=0.5, density=True)
func_plot(dist.pdf, ls="--", c="k", label="True density")
plt.legend(loc="best", frameon=True, shadow=True)
plt.title("Histogram of simulated Gaussian data")
plt.show()
plt.close()
kde = KernelDensityEstimator()
kde.fit(data)

plt.figure()
plt.hist(data, bins="auto", alpha=0.5, density=True)
func_plot(dist.pdf, ls="--", c="k", label="True density")
kde.plot(c="r")
plt.legend(loc="best", frameon=True, shadow=True)
plt.title(f"Gaussian KDE (bandwidth: {kde.bandwidth:.3f})")
plt.show()
plt.close()
bandwidths = (0.25, 0.5, 1, 2)

plt.figure()
plt.hist(data, bins="auto", alpha=0.5, density=True)
for bandwidth in bandwidths:
    kde = KernelDensityEstimator()
    kde.fit(data, bandwidth=bandwidth)
    kde.plot(label=f"Bandwidth: {bandwidth}")
plt.legend(loc="best", frameon=True, shadow=True)
plt.title(f"Gaussian KDE, varying bandwidths")
plt.show()
plt.close()<jupyter_output><empty_output><jupyter_text>Example: Old Faithful dataset<jupyter_code>data = load_old_faithful()

plt.figure()
plt.scatter(data.Duration, data.Wait, c="b", edgecolor="k", alpha=0.7)
plt.xlabel("Eruption Duration")
plt.ylabel("Time Until Next Eruption")
plt.title("Scatter plot of the Old Faithful dataset")
plt.show()
plt.close()
kde = KernelDensityEstimator()
kde.fit(data, bandwidth=0.7)

plt.figure()
plt.xlim(min(data.Duration), max(data.Duration))
plt.ylim(min(data.Wait), max(data.Wait))
kde.plot(cmap="Blues")
plt.xlabel("Eruption Duration")
plt.ylabel("Time Until Next Eruption")
plt.title("Kernel Density Estimate Contours")
plt.show()
plt.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15089620675281368, 'fraction_numerical': 0.010004168403501459, 'mean_word_length': 7.421052631578948, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '485076', 'license': 'MIT', 'n_tokens_mistral': 931, 'n_tokens_neox': 853, 'n_words': 188, 'path': 'examples/Kernel Density Estimation.ipynb', 'repo_name': 'artemmavrin/SLTools', 'size': '2399'}