如何在间隔中分离随机数,然后获得python间隔的概率?

发布于 2025-01-23 07:48:49 字数 78 浏览 3 评论 0原文

我必须使用python中的numpy生成2000浮点随机数,然后获得这些数字的整数间隔,并最终计算频率和概率。

有人可以帮我吗?

I have to generate 2000 float random numbers with a normal distribution using numpy in Python, then get the integer intervals of those numbers and finally calculate the frequency and probability.

Can someone please help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2025-01-30 07:48:49

iiuc,我们知道正态分布中(-1,1)之间的概率为68%,如果我们测试,我们会发现这是正确的:

import matplotlib.pyplot as plt
import numpy as np

mu, sigma = 0, 1 # mean and standard deviation
rnorm = np.random.normal(mu, sigma, 2000)

freq = np.sum(np.logical_and(rnorm>=-1, rnorm<=1))
print(f'frequency between (-1,1): {(freq)}')

prob = freq/2000*100
print(f'probability between (-1,1): {prob} %')

# Or (-2,2) is 95% and (-3,3) is 99.7 like below:
# >>> np.sum(np.logical_and(rnorm>=-2, rnorm<=2))/2000*100
# 95.85000000000001
# >>> np.sum(np.logical_and(rnorm>=-3, rnorm<=3))/2000*100
# 99.7

输出:

frequency between (-1,1): 1365
probability between (-1,1): 68.25 %

您可以使用plt.hist.hist (您可以根据需要更改bin),然后计算以下类似于下面的频率和概率:

plt.hist(rnorm, bins=50)
plt.show()

freq = rnorm[rnorm < 0]
print(f'frequency less than ZERO: {len(freq)}')

prob = rnorm[rnorm < 0]
print(f'probability less than ZERO: {len(freq)/2000*100} %')

“在此处输入图像说明”

frequency less than ZERO: 1003
probability less than ZERO: 50.14999999999999 %

IIUC, We know the probability between (-1,1) in a normal distribution is 68% and If we test we see this is correct:

import matplotlib.pyplot as plt
import numpy as np

mu, sigma = 0, 1 # mean and standard deviation
rnorm = np.random.normal(mu, sigma, 2000)

freq = np.sum(np.logical_and(rnorm>=-1, rnorm<=1))
print(f'frequency between (-1,1): {(freq)}')

prob = freq/2000*100
print(f'probability between (-1,1): {prob} %')

# Or (-2,2) is 95% and (-3,3) is 99.7 like below:
# >>> np.sum(np.logical_and(rnorm>=-2, rnorm<=2))/2000*100
# 95.85000000000001
# >>> np.sum(np.logical_and(rnorm>=-3, rnorm<=3))/2000*100
# 99.7

Output:

frequency between (-1,1): 1365
probability between (-1,1): 68.25 %

You can use plt.hist (you can change bin as you want) and then compute frequency and probability like below:

plt.hist(rnorm, bins=50)
plt.show()

freq = rnorm[rnorm < 0]
print(f'frequency less than ZERO: {len(freq)}')

prob = rnorm[rnorm < 0]
print(f'probability less than ZERO: {len(freq)/2000*100} %')

Output:

enter image description here

frequency less than ZERO: 1003
probability less than ZERO: 50.14999999999999 %
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文