简体   繁体   中英

Python fast Kernel Density estimation (probability density function)

I'm looking for a way to get the kernel density function of a data set and plot it for arbitrary data points. Using Scipy stats module, I came up with the following code:

import numpy as np
import scipy.stats as st

def get_pdf(data):
    a = np.array(data)
    ag = st.gaussian_kde(a)
    x = np.linspace(0, max(data), max(data)*10)
    y = ag(x)
    return x, y

This gives the expected result, but the performance is very poor, when the data set size is large.

I found fastkde as an implementation for fast kernel density estimation. But I could not figure out a way to use this in the same way I used Scipy stats KDE.

Can someone give me some insight?

Thanks

You may be looking for something like this:

from fastkde.fastKDE import pdf


def get_pdf(data):
    y, x = pdf(data)
    return x, y

Note that, in general, fastKDE.pdf() returns pdf, axes (the PDF and the axes of the PDF, analogous to hist, bins for a histogram).

If there are multiple input variables, the axes variable is a list of the axes, with each axis corresponding to an input variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM