问题描述
在n次试验中,成功概率为p,我需要找到小于或等于x次成功的概率
我建立了一个函数来查找x个成功案例。
我试图将此功能与MAP一起使用
< br /> To this <br/>
不幸的是,我收到了无效的语法错误,或者仅仅是一个MAP对象
def bin_dist(n,p,x):
if x > n:
raise ValueError('the number of success cannot be greater than the number of trials')
else:
def fact2(x):
if x < 0:
return ValueError("number of successes cannot be less than 1")
else:
fact = 1
for i in range(1,x + 1):
fact = fact * i
return fact
def combination2(n,x):
return(fact2(n)/fact2(n-x)*fact2(x))
print("The probability of x successes,followed by the total number of arrangements/possible combinations used to find x:")
print(combination2(n,x) * (p)**(x) * ((1-p)**(n-x)))
def bin_cdf(n,x):
li = range(0,x)
print map(bin_dist,li)
return map(bin_dist,li)
bin_cdf(3,0.5,2)
我还被告知,将MAP转换为列表时,用于返回x个成功项中的p个的函数bin_dist缺少两个位置参数
File "<ipython-input-1-e333d90fddea>",line 21
print map (bin_dist,li)
^
SyntaxError : invalid syntax
我还尝试了从0到x的迭代,并找到了所有值的总和,但似乎发现了可疑的结果
----> 3 print ( list ( map ( bin_dist,li ) ) )
4 return ( list ( map ( bin_dist,li ) ) )
5
TypeError : bin_dist () missing 2 required positional arguments: 'p' and 'x'
至少在我看来,堆栈溢出社区是互联网上最明亮的社区。强烈建议任何建议
解决方法
很抱歉提出经典问题:您是否需要开发自己的功能?在Python中已经有很多功能可以做到这一点:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binom.html