用matplotlib按样本绘制概率密度函数

2024-05-15 02:16:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想画一个概率密度函数的近似值,基于 我有一个样本;模仿直方图行为的曲线。我可以 有我想要的那么大的样品。


Tags: 样品直方图曲线样本概率密度函数
2条回答

如果要绘制分布,并且您知道它,请将其定义为函数,并按此方式绘制:

import numpy as np
from matplotlib import pyplot as plt

def my_dist(x):
    return np.exp(-x ** 2)

x = np.arange(-100, 100)
p = my_dist(x)
plt.plot(x, p)
plt.show()

如果没有作为分析函数的精确分布,也许可以生成一个大样本,提取直方图并以某种方式平滑数据:

import numpy as np
from scipy.interpolate import UnivariateSpline
from matplotlib import pyplot as plt

N = 1000
n = N//10
s = np.random.normal(size=N)   # generate your data sample with N elements
p, x = np.histogram(s, bins=n) # bin it into n = N//10 bins
x = x[:-1] + (x[1] - x[0])/2   # convert bin edges to centers
f = UnivariateSpline(x, p, s=n)
plt.plot(x, f(x))
plt.show()

可以在UnivariateSpline函数调用中增加或减少s(平滑因子)来增加或减少平滑。例如,使用这两种方法: dist to func

您需要做的是使用scipy.stats.kde包中的高斯kde。

根据您的数据,您可以这样做:

from scipy.stats.kde import gaussian_kde
from numpy import linspace
# create fake data
data = randn(1000)
# this create the kernel, given an array it will estimate the probability over that values
kde = gaussian_kde( data )
# these are the values over wich your kernel will be evaluated
dist_space = linspace( min(data), max(data), 100 )
# plot the results
plt.plot( dist_space, kde(dist_space) )

内核密度可以随意配置,可以轻松处理N维数据。 它还可以避免在askewchan给出的绘图中看到的样条曲线扭曲。

enter image description here

相关问题 更多 >

    热门问题