简体   繁体   中英

equivalent of “ksdensity” MATLAB built-in function in Python

I need the python equivalent code for the below MATLAB code:

[f,xi] = ksdensity(data,'Support','positive','Function','cdf');

I find the below python code, but I don't know how I can provide its cdf . I appreciate it if you could guide me.

from scipy import stats
kde = stats.gaussian_kde(data)

Try this, link .

def insert_size(insert_size_distribution):
    """Calculate cumulative distribution function from the raw insert size
    distributin. Uses 1D kernel density estimation.

    Args:
        insert_size_distribution (list): list of insert sizes from aligned
        read pairs

    Returns:
        1darray: a cumulative density function
    """
    kde = stats.gaussian_kde(
        insert_size_distribution,
        bw_method=0.2 / np.std(insert_size_distribution, ddof=1))
    x_grid = np.linspace(
        min(insert_size_distribution),
        max(insert_size_distribution), 1000)
    kde = kde.evaluate(x_grid)
    cdf = np.cumsum(kde)
    cdf = cdf / cdf[-1]
    return cdf 

The following code works for me:

import scipy
kde = scipy.stats.gaussian_kde(data)
my_cdf = scipy.stats.norm.cdf(kde)

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