本文介绍了在python中使用scipy截断正态分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Python3 中使用带有 scipy 的截断正态分布.我想做一些简单的事情:绘制以 0.5 为中心且范围从 0 到 1 的截断法线的 pdf.我有以下代码行

from scipy import truncnorm导入matplotlib.pyplot作为pltplt.plot([truncnorm.pdf(p,0,1, loc=0.5) for p in np.arange(0,1.1,0.1)])

然而,这并没有给出我期望的漂亮的钟形概率分布函数.相反,它在 0.5 之前等于 0,我不知道为什么.有什么建议吗?

解决方案

以下是根据

I am trying to use a truncated normal distribution with scipy in Python3. I want to do something simple: plot the pdf of a truncated normal centered at 0.5 and ranging from 0 to 1. I have the following code line

from scipy import truncnorm
import matplotlib.pyplot as plt
plt.plot([truncnorm.pdf(p,0,1, loc=0.5) for p in np.arange(0,1.1,0.1)])

However, this does not give the nice bell-shaped probability distribution function I would expect. Rather, it equals 0 before 0.5, and I cannot figure out why. Any advice on this?

解决方案

Here's the procedure to follow according to the documentation of truncnorm.

# user input
myclip_a = 0
myclip_b = 1
my_mean = 0.5
my_std = 0.3

a, b = (myclip_a - my_mean) / my_std, (myclip_b - my_mean) / my_std
x_range = np.linspace(-1,2,1000)
plt.plot(x_range, truncnorm.pdf(x_range, a, b, loc = my_mean, scale = my_std))

这篇关于在python中使用scipy截断正态分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 12:55