如何在python中从正态概率密度函数中找出概率?

问题描述 投票:0回答:1

基本上,我已经用平均值和标准差的值绘制了一条正态曲线。Y轴给出了概率密度。

我如何在x轴上找到某个值 "x "的概率?有什么Python函数可以解决这个问题,或者我如何编写代码?

python statistics probability probability-density probability-distribution
1个回答
1
投票

不是很清楚你说的概率密度函数,是指。

enter image description here

给定一定的平均值和标准差 在python中,你可以使用 stats.norm.fit 得到概率,例如,我们有一些数据,我们拟合一个正态分布。

from scipy import stats
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

data = stats.norm.rvs(10,2,1000)

x = np.linspace(min(data),max(data),1000)
mu, var = stats.norm.fit(data)
p = stats.norm.pdf(x, mu, std)

现在我们已经估计了平均值和标准差, 我们用pdf来估计概率,例如12. 5。

xval = 12.5
p_at_x = stats.norm.pdf(xval,mu,std)

我们可以绘制看看是否是你想要的。

fig, ax = plt.subplots(1,1)
sns.distplot(data,bins=50,ax=ax)
plt.plot(x,p)
ax.hlines(p_at_x,0,xval,linestyle ="dotted")
ax.vlines(xval,0,p_at_x,linestyle ="dotted")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.