scipy.stats
库具有查找拟合分布的均值和中值而不是模式的函数。
如果在拟合数据后具有分布的参数,如何找到拟合分布的mode
?
如果我没弄错的话,您想找到拟合分布的模式,而不是给定数据的模式。基本上,我们可以按照以下3个步骤进行操作。
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]]
# fit the parameters of norm distribution
params = stats.norm.fit(data)
params
输出:
(0.059808015534485,1.0078822447165796)
请注意,stats.norm
有2个参数,例如loc
和scale
。对于scipy.stats
中的不同距离,参数是不同的。我认为将参数存储在元组中,然后在下一步中解压缩是很方便的。
# pass fitted params to the dist, and then get the 50% quantile
stats.norm.ppf(0.5,*params)
输出:
0.059808015534485
注意,norm
分布的mode
等于mean
。在这个例子中这是一个巧合。您可以尝试使用自己的数据和分布!