python-3.x - Python 3.X 名称错误
问题描述
我正在使用 Python 3,这段代码是用 PyCharm 编写的。我将概率更改为 p 以防止未声明它的冲突链接到教程文章的链接我从这里获得它的网站
import numpy as np
def coinFlip(p):
#perform the binomial distribution (returns 1 or 0)
result = np.random.binomial (1 , p)
#return flip to be added to numpy array
return result
'''Main Area'''
#probability of heads vs. tails. this can be changed
p = .5
#num of flips required. this can be changed
n = 10
#initiate array
FullResults = np.arrange(n)
#perform the desired number of flips required probability set above
for i in range(0, n):
fullResults[i] = coinFlip(p)
i+=1
#print results
print("probability is set to ", p)
print("Tails = 0, Heads = 1: ", fullResults)
#Total up heads and tails for easy user experience
print("head count: ", np.count_nonzero(fullResults == 1))
print("Tail count: ", np.count_nonzero(fullResults == 0))
解决方案
import numpy as np
def coinFlip(p):
#perform the binomial distribution (returns 1 or 0)
result = np.random.binomial (1 , p)
#return flip to be added to numpy array
return result
'''Main Area'''
#probability of heads vs. tails. this can be changed
p = .5
#num of flips required. this can be changed
n = 10
#initiate array
#################FullResults = np.arrange(n)
FullResults = np.arange(n)
#perform the desired number of flips required probability set above
fullResults = np.zeros(n)
for i in range(0, n):
fullResults[i] = coinFlip(p)
i+=1
#print results
print("probability is set to ", p)
print("Tails = 0, Heads = 1: ", fullResults)
#Total up heads and tails for easy user experience
print("head count: ", np.count_nonzero(fullResults == 1))
print("Tail count: ", np.count_nonzero(fullResults == 0))
我不得不做两个小的修改 -
- np.arange(n) 而不是 np.arrange(n)
- 我用零初始化了数组 fullResults
这是输出 -
probability is set to 0.5
Tails = 0, Heads = 1: [0. 0. 0. 1. 1. 1. 0. 0. 0. 1.]
head count: 4
Tail count: 6
推荐阅读
- java - 如何获取对象的 oracle 数组并在我的代码中使用它们?
- docker - Docker - 在容器之间共享命令
- laravel - Symfony \ 组件 \ HttpKernel \ 异常 \ MethodNotAllowedHttpException #25782
- amazon-web-services - Athena 从 Firehose > Glue > S3 parquet setup 返回空结果
- css - 我无法覆盖 woocommerce.css
- c# - C# Excel 在调试模式之外运行时不创建实例。(IIS 实例)
- sql - 如何将几个 SQL 查询合二为一?(微软 SQL)
- git - 切换分支后防止删除文件
- css - 为什么 flexbox 正确居中元素而定位属性却没有?
- python - 根据条件使用不同的上下文管理器