从骨灰盒中抽取 2 个数字并替换 - Python
Draw 2 numbers from urn with replacement - Python
我的骨灰盒包含数字 1.3 和 0.9,我想每次模拟绘制 35 次并替换。然后执行最终计算,将结果附加到列表中。
我总共要执行 10000 次模拟。
我的代码如下所示:
#Draw either 1.3 or 0.9
returns = [1.3,0.9]
#No. of simulations
simulations = 10000
#10000 for loops
for i in range(simulations):
lst = []
#each iteration should include 35 random draws with replacement
for i in range(35):
lst.append(random.choices(returns,1))
lst = np.array(lst)
#Do final calculation and append solution to list
ret = []
ret.append((prod(lst)^(1/35))-1)
我收到的错误是 TypeError: 'int' object is not iterable
。我明白为什么它不起作用,因为我正在尝试将整数转换为列表对象....但我只是不知道如何解决这个问题?
完整堆栈跟踪:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-5d61655781f6> in <module>
9 #each iteration should include 35 random draws with replacement
10 for i in range(35):
---> 11 lst.append(random.choices(returns,1))
12
13 lst = np.array(lst)
~/opt/anaconda3/lib/python3.7/random.py in choices(self, population, weights, cum_weights, k)
355 total = len(population)
356 return [population[_int(random() * total)] for i in range(k)]
--> 357 cum_weights = list(_itertools.accumulate(weights))
358 elif weights is not None:
359 raise TypeError('Cannot specify both weights and cumulative weights')
TypeError: 'int' object is not iterable
如果要将 lst 转换为 numpy
数组,可以改用 numpy.random.choice
。这也将消除 for 循环的需要。
import numpy as np
#Draw either 1.3 or 0.9
urn = [1.3,0.9]
#No. of simulations
simulations = 10000
#No. of draws
draws = 35
# simulate the draws from the urn
X=np.random.choice(urn,(draws,simulations))
# print first 10 elements as a check
print(X[1:10])
# print shape as a check
print(X.shape)
输出:
[[1.3 1.3 1.3 ... 0.9 1.3 1.3]
[0.9 1.3 0.9 ... 0.9 0.9 0.9]
[0.9 1.3 0.9 ... 1.3 1.3 0.9]
...
[1.3 0.9 0.9 ... 1.3 0.9 0.9]
[1.3 1.3 1.3 ... 0.9 0.9 1.3]
[1.3 1.3 0.9 ... 0.9 1.3 1.3]]
(35, 10000)
我把returns的名字改成了骨灰盒。 returns 在 python 中有点混乱。
当你打电话时:
random.choices(returns,1)
python认为1对应权重,如果对应允许选择元素个数的k为return,则必须这样指定:
random.choices(returns,k=1)
但默认为1所以不需要通知
我的骨灰盒包含数字 1.3 和 0.9,我想每次模拟绘制 35 次并替换。然后执行最终计算,将结果附加到列表中。 我总共要执行 10000 次模拟。
我的代码如下所示:
#Draw either 1.3 or 0.9
returns = [1.3,0.9]
#No. of simulations
simulations = 10000
#10000 for loops
for i in range(simulations):
lst = []
#each iteration should include 35 random draws with replacement
for i in range(35):
lst.append(random.choices(returns,1))
lst = np.array(lst)
#Do final calculation and append solution to list
ret = []
ret.append((prod(lst)^(1/35))-1)
我收到的错误是 TypeError: 'int' object is not iterable
。我明白为什么它不起作用,因为我正在尝试将整数转换为列表对象....但我只是不知道如何解决这个问题?
完整堆栈跟踪:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-5d61655781f6> in <module>
9 #each iteration should include 35 random draws with replacement
10 for i in range(35):
---> 11 lst.append(random.choices(returns,1))
12
13 lst = np.array(lst)
~/opt/anaconda3/lib/python3.7/random.py in choices(self, population, weights, cum_weights, k)
355 total = len(population)
356 return [population[_int(random() * total)] for i in range(k)]
--> 357 cum_weights = list(_itertools.accumulate(weights))
358 elif weights is not None:
359 raise TypeError('Cannot specify both weights and cumulative weights')
TypeError: 'int' object is not iterable
如果要将 lst 转换为 numpy
数组,可以改用 numpy.random.choice
。这也将消除 for 循环的需要。
import numpy as np
#Draw either 1.3 or 0.9
urn = [1.3,0.9]
#No. of simulations
simulations = 10000
#No. of draws
draws = 35
# simulate the draws from the urn
X=np.random.choice(urn,(draws,simulations))
# print first 10 elements as a check
print(X[1:10])
# print shape as a check
print(X.shape)
输出:
[[1.3 1.3 1.3 ... 0.9 1.3 1.3]
[0.9 1.3 0.9 ... 0.9 0.9 0.9]
[0.9 1.3 0.9 ... 1.3 1.3 0.9]
...
[1.3 0.9 0.9 ... 1.3 0.9 0.9]
[1.3 1.3 1.3 ... 0.9 0.9 1.3]
[1.3 1.3 0.9 ... 0.9 1.3 1.3]]
(35, 10000)
我把returns的名字改成了骨灰盒。 returns 在 python 中有点混乱。
当你打电话时:
random.choices(returns,1)
python认为1对应权重,如果对应允许选择元素个数的k为return,则必须这样指定:
random.choices(returns,k=1)
但默认为1所以不需要通知