如何在scipy.stats中获得分配模式

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

scipy.stats库具有查找拟合分布的均值和中值而不是模式的函数。

如果在拟合数据后具有分布的参数,如何找到拟合分布的mode

python scipy statistics distribution
1个回答
1
投票

如果我没弄错的话,您想找到拟合分布的模式,而不是给定数据的模式。基本上,我们可以按照以下3个步骤进行操作。

步骤1:从分布中生成数据集

from scipy import stats
# generate a norm data with 0 mean and 1 variance
data = stats.norm.rvs(loc= 0,scale = 1,size = 100)
data[0:5]

输出:

array([1.76405235,0.40015721,0.97873798,2.2408932,1.86755799]]

步骤2:拟合参数

# fit the parameters of norm distribution
params = stats.norm.fit(data)
params

输出:

(0.059808015534485,1.0078822447165796)

请注意,stats.norm有2个参数,例如locscale。对于scipy.stats中的不同距离,参数是不同的。我认为将参数存储在元组中,然后在下一步中解压缩是很方便的。

步骤3:获得拟合分布的模式(0.5分位数)

# pass fitted params to the dist, and then get the 50% quantile
stats.norm.ppf(0.5,*params)

输出:

0.059808015534485

注意,norm分布的mode等于mean。在这个例子中这是一个巧合。您可以尝试使用自己的数据和分布!

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