Let's now apply the Epanechnikov density estimation to perform an example of anomaly detection. According to the structure of the probability density, we have decided to impose a cut-off at p(x) < 0.005. Such a condition is displayed in the following screenshot:
Epanechnikov density estimation with anomaly cut-off
The red dots indicate the age limits for a sample to be classified as an anomaly. Let's compute the probability densities for some test points:
import numpy as np
test_data = np.array([12, 15, 18, 20, 25, 30, 40, 50, 55, 60, 65, 70, 75, 80, 85, 90]).reshape(-1, 1)
test_densities_epanechnikov = np.exp(kd_epanechnikov.score_samples(test_data))
test_densities_gaussian = np.exp(kd_gaussian.score_samples(test_data))
for age, density in zip(np.squeeze(test_data), test_densities_epanechnikov):
print('p(Age = {:d}) = {:.7f} ({})'.format...