Home > Software engineering >  Sklearn KernelDensity gives identical results for two different models
Sklearn KernelDensity gives identical results for two different models

Time:02-10

I'm having trouble with KernelDensity from sklearn. I put in two completely different arrays to create two different models, but the two models have identical results (scores). They should have different results for different models, shouldn't they?

Here's my code:

from sklearn.neighbors import KernelDensity
import numpy as np
kde = KernelDensity(kernel="gaussian", bandwidth=15)
def reproducible_example():
     X1 = np.array([9,18,28,35,54,59,65,83,89,116,119,124,144])
     X2 = np.array([39,51,57,61,66,81,88,103,120,126,130,132,134])
     model1 = kde.fit(X1[:, np.newaxis])
     model2 = kde.fit(X2[:, np.newaxis])
     X_plot = np.linspace(0, 129, 130)
     score1 = model1.score_samples(X_plot[:, np.newaxis])
     score2 = model2.score_samples(X_plot[:, np.newaxis])
     print(np.exp(score1) == np.exp(score2))
reproducible_example()

The expected output is False, as the two different models should return two different scores. Instead, the output is this:

[ True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True  True  True]

Indicating that the two results are identical. How is that possible?

Thanks, Nathan

CodePudding user response:

  •  Tags:  
  • Related