2 years ago
#23255
dujuanjuan
The return of sklearn.neighbors.KernelDensity in python is not a pdf?
I want to use the sklearn.neighbors.KernelDensity in python to obtain a pdf of a data set. However, after visualizing the probability Density function(PDF) from the kernel object, I found the value of some point is exceeds 1. The detail code is shown as the follow:
error_tr=data[start:end,0]
error_tr_arr=np.array(error_tr).reshape(-1,1)
#plot hist
weights=np.ones_like(error_tr)/float(len(error_tr))
weight=np.array(weights).reshape(-1,1)
plt.hist(error_tr_arr,weights=weights)
kde=KernelDensity(kernel='gaussian',bandwidth=0.1)
kd = kde.fit(error_tr_arr)
#visualize the probability Density function(PDF) from the kernel object
xs=np.linspace(-2,2,1000)
log_probs=kd.score_samples(xs.reshape(-1,1))
kde_pdf=np.exp(log_probs)
plt.plot(xs,kde_pdf,label='KDE Fitted Distribution')
And the plot result is : enter image description here
Can you give me some suggestions or insights?
python
scikit-learn
kernel-density
probability-density
0 Answers
Your Answer