Tib*_*anu 6 python random numpy
我需要从 N 个不同的正态分布中采样多次 (M) 次。这种重复采样将依次发生数千次。我想以最有效的方式做到这一点,因为我不想在这个过程结束之前就老死。代码看起来像这样:
import numpy as np
# bunch of stuff that is unrelated to the problem
number_of_repeated_processes = 5000
number_of_samples_per_process = 20
# the normal distributions I'm sampling from are described by 2 vectors:
#
# myMEANS <- an numpy array of length 10 containing the means for the distributions
# myVAR <- an numpy array of length 10 containing the variance for the distributions
for i in range(number_of_repeated_processes):
# myRESULT is a list of arrays containing the results for the sampling
#
myRESULT = [np.random.normal(loc=myMEANS[j], scale=myVAR[j], size = number_of_samples_per_process) for j in range(10)]
#
# here do something with myRESULT
# end for loop
Run Code Online (Sandbox Code Playgroud)
问题是......有没有更好的方法来获取 myRESULT 矩阵
np.random.normal直接接受means-var作为数组,您可以选择一个大小来覆盖一次运行中的所有采样而无需循环:
myRESULT = np.random.normal(loc=myMEANS, scale=myVAR, size = (number_of_samples_per_process, number_of_repeated_processes,myMEANS.size))
Run Code Online (Sandbox Code Playgroud)
这将为-数组中的每个mean-var对返回一个number_of_samples_per_process按列。例如,要访问-的示例,请使用。这应该会在一定程度上提高你的表现。number_of_repeated_processesmyMEANSmyVARmyMEANS[i]myVAR[i]myRESULT[...,i]