英文:
How does np.random.binomial work in numpy python
问题
以下是翻译好的部分:
我有以下代码,但即使阅读文档后,我仍然不确定它是如何工作的。
我想要以10%的概率产生0或1的值,其中1的概率为10%,0的概率为90%。上面的代码是否可以实现这个目标?
如果我将代码更改为以下内容会怎么样?当p参数中有3个值时,这对分布有什么意义?n等于2意味着可能的值可以是0、1、2吗?
英文:
I have the following code but I'm unsure how it works even after reading documentation..
print(np.random.binomial(n = 1, [0.1,0.9]))
I want to produce either 0 or 1 values with probability 10% being 1 and 90% being 0. Is the code above how you would do it?
What if I changed the code to the following? What does that mean for the distributions when I have 3 values in the p argument? Does n being 2 mean the possible values could be 0,1,2?
print(np.random.binomial(n = 2, p = [0.1,0.5,0.4]))
答案1
得分: 1
I took a look at the function implementation.
The parameter n
in the binomial distribution represents the number of Bernoulli experiments to perform. In the binomial distribution, p
signifies the equal probability of success in every Bernoulli experiment.
So, what does a list of p
values mean for a binomial distribution? Nothing. When you pass a list of probabilities, list_of_p
, to np.random.binomial(n, list_of_p)
, the function will produce as many samples as there are elements in list_of_p
. The function will check every element in list_of_p
falls within the range of 0 and 1.
In the particular case where all elements in list_of_p
are the same, you can siplify your function call by using the third parameter: size
. In other words, this both lines are equivalent:
np.random.binomial(n = 1, [0.1, 0.1])
np.random.binomial(n = 1, 0.1, 2)
So, if you specify the number of samples using two different arguments, we can provide the function contradictory information, don't you? Yes, but the function will raise an error if you do so.
# Works fine
np.random.binomial(n = 1, [0.1, 0.3], 2)
# Raises an exception
np.random.binomial(n = 1, [0.1, 0.3], 1)
# Works fine also if the list has a length of 1
np.random.binomial(n = 1, [0.1], 20)
To answer your question. A binomial distribution with n = 2
generates values ranging from 0 to 2. Take a look at the Binomial distribution definition. I will not elaborate as this would be math, not coding.
The function call
np.random.binomial(n = 2, p = [0.1, 0.5, 0.4])
will produce three samples of binomial distribution.
Specifically, each of them B(2, 0.1)
, B(2, 0.5)
and B(2, 0.4)
.
And the answer to your main question I think would be clear now. If you want to use the binomial function to simulate a Bernoulli experiment with probability 0.1 of yielding 1, you should write:
np.random.binomial(n = 1, p = 0.1)
英文:
I took a look at the function implementation.
The parameter n
in the binomial distribution represents the number of Bernoulli experiments to perform. In the binomial distribution, p
signifies the equal probability of success in every Bernoulli experiment.
So, what does a list of p
values mean for a binomial distribution? Nothing. When you pass a list of probabilities, list_of_p
, to np.random.binomial(n, list_of_p)
, the function will produce as many samples as there are elements in list_of_p
. The function will check every element in list_of_p
falls within the range of 0 and 1.
In the particular case where all elements in list_of_p
are the same, you can siplify your function call by using the third parameter: size
. In other words, this both lines are equivalent:
np.random.binomial(n = 1, [0.1, 0.1])
np.random.binomial(n = 1, 0.1, 2)
So, if you specify the number of samples using two different arguments, we can provide the function contradictory information, don't you? Yes, but the function will raise an error if you do so.
# Works fine
np.random.binomial(n = 1, [0.1, 0.3], 2)
# Raises an exception
np.random.binomial(n = 1, [0.1, 0.3], 1)
# Works fine also if the list has a length of 1
np.random.binomial(n = 1, [0.1], 20)
To answer your question. A binomial distribution with n = 2
generates values ranging from 0 to 2. Take a look at the Binomial distribution definition. I will not elaborate as this would be math, not coding.
The function call
np.random.binomial(n = 2, p = [0.1, 0.5, 0.4])
will produce three samples of binomial distribution.
Specifically, each of them B(2, 0.1)
, B(2, 0.5)
and B(2, 0.4)
.
And the answer to your main question I think would be clear now. If you want to use the binomial function to simulate a Bernoulli experiment with probability 0.1 of yielding 1, you should write:
np.random.binomial(n = 1, p = 0.1)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论