我试图编写一个python代码来计算随机停止的和,但当随机生成的数字的大小超过5时会遇到问题
MemoryError: Unable to allocate 15.5 PiB for an array with shape (49, 53, 43, 46, 52, 53, 57, 52, 52) and data type float64
在以下代码中:
#Create a function tn.func to calculate randomly stopped sum
import numpy as np
#Define a function
def tn_fun(n):
return sum(np.random.choice([50, 100, 200], n, replace=True, p=[0.3, 0.5, 0.2]))
N = np.random.poisson(50, 10)
# #Generate 10000 random values of N, using lambda = 50
TN = tn_fun(N)
print('Sample mean of the randomly stopped sum TN is',np.mean(TN))
print('Sample variance of the randomly stopped sum TN is', np.var(TN))
这就是你想要的
相关问题 更多 >
编程相关推荐