简体   繁体   English

如何使用scipy gaussian_kde获得概率密度函数?

[英]How to get probability density function using scipy gaussian_kde?

I have a 1D data set which is saved in a 1D list. 我有一个1D数据集,保存在1D列表中。 What is the best way to get the probability density function? 获得概率密度函数的最佳方法是什么? I tried the usual approach of using scipy gaussian_kde. 我尝试了使用scipy gaussian_kde的常用方法。

array = np.array(values)
kde = gaussian_kde(array)
x = np.linspace(0, 50, 500)
plt.plot(x, kde(x), label="", color="blue")
plt.legend(loc='best')
plt.show()

输出量

Produced graph is not the expected probability density function, since probability density functions should have a value between 0 and 1 for each x. 生成的图不是预期的概率密度函数,因为对于每个x,概率密度函数应具有介于0和1之间的值。

Thanks 谢谢

Use the following code. 使用以下代码。

import os
import matplotlib.pyplot as plt
import sys
import math
import numpy as np
import scipy.stats as st
from scipy.stats._continuous_distns import _distn_names
from scipy.optimize import curve_fit

def get_pdf(latency_list):
    np_array = np.array(latency_list)  # convert the list into a numpy array
    ag = st.gaussian_kde(np_array)  # calculate the kernel density function for the latency values
    # list of equidistant values in the range of the latency values
    x = np.linspace(min(latency_list), max(latency_list), (max(latency_list) - min(latency_list)) * 10)
    y = ag(x)  # evaluate the latency values for each x value
    return x, y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 scipy 的 gaussian_kde 和 sklearn 的 KernelDensity 进行核密度估计会导致不同的结果 - Kernel Density Estimation using scipy's gaussian_kde and sklearn's KernelDensity leads to different results 获取SciPy的gaussian_kde函数使用的带宽 - Getting bandwidth used by SciPy's gaussian_kde function 从 Scipy gaussian_kde 中检索值 - Retrieve values from Scipy gaussian_kde 为什么 stat_density (R; ggplot2) 和 gaussian_kde (Python; scipy) 不同? - Why do stat_density (R; ggplot2) and gaussian_kde (Python; scipy) differ? 使用 scipy gaussian_kde 和 seaborn kdeplot 时的差异 KDE 渲染 - Diffrence KDE rendering when using scipy gaussian_kde and seaborn kdeplot 如何更改由matplotlib中的密度着色的散点图的gaussian_kde参数 - How can I change de parameters of gaussian_kde for a scatter plot colored by density in matplotlib gaussian_kde与密度积分和之间的不一致 - Inconsistency between gaussian_kde and density integral sum Scipy.stats gaussian_kde 从条件分布中重新采样 - Scipy.stats gaussian_kde to resample from conditional distribution gaussian_kde分布偏斜? - gaussian_kde with skewed distributions? scipy gaussian_kde 根据使用的方法产生不同的结果(权重与无权重) - scipy gaussian_kde produces different results depending on method used (weights vs no weights)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM