我想要计算每个数组内的元素的np.sum。我试图用np.sum(outcome_list[0] == 'H'
来代替np.sum(outcome_list[j] == 'H'
,以便每个“列表”都有自己的数据集,但它不喜欢它。更大的问题是,我将如何构建一个具有给定基本列表的数组以及要在该列表的每个元素中执行的操作?列表中的元素的总和
编辑:
的throw_a_coin定义
def throw_a_coin(N):
return np.random.choice(['H','T'], size=N)
N =40
试验(如上图所示)是组可
for i in trials:
throws = throw_a_coin(i)
outcome_list.append(throws)
for j in outcome_list:
print("Number of Heads:", np.sum(outcome_list[0] == 'H'))
print (j)
0至被作用
编辑2:
问题,如下所示的解决,但是我得到超过13号的“概率” - 看来,该系统通过试验运行多于一次。
def throw_a_coin(N):
return np.random.choice(['H','T'], size=N)
trials = [10, 30, 50, 70, 100, 130, 170, 200, 500, 1000, 2000, 5000, 10000]
for i in trials:
throws = throw_a_coin(i)
outcome_list.append(throws)
probabilities = []
for j in outcome_list:
print("Number of Heads:", np.sum(j == 'H'))
print("Number of Throws:", len(j))
print("p = Number of Heads/Total Throws:", (np.sum(j == 'H'))/len(j))
probabilities.append((np.sum(j =='H'))/len(j))
print (j)
print("\n")
print(probabilities)
您是否想要统计头数? – Rishav
你能否附上代码而不是代码的照片? –
@Rishav - 是的,计算每次试验的头数 – aiwan