【问题标题】:Assign sampled multinomial values uniformly at random随机均匀地分配采样多项式值
【发布时间】:2014-06-06 02:25:51
【问题描述】:

我正在使用np.random.multinomial 对多项分布进行采样M 次(给定概率[X_0 X_1 .. X_n] 它返回从指定多项式采样的计数[C_0 C_1 ... C_n],其中\sum_i C_i = M)。给定这些采样值(C_i's),我想将它们随机均匀地分配给我拥有的一些对象。

目前我正在做的是:

draws = np.random.multinomial(M, probs, size=1)
draws = draws[0]
draws_list = []
for idx,num in enumerate(draws):
    draws_list += [idx]*num
random.shuffle(draws_list)

那么draws_list 是一个随机打乱的采样值列表。

问题在于填充draws_list(for 循环)非常慢。有没有更好/更快的方法来做到这一点?

【问题讨论】:

    标签: python numpy random statistics random-sample


    【解决方案1】:

    试试这个代码。这个策略是先分配内存,再填充数据。

    draws_list1 = np.empty(M, dtype=np.int)
    acc = 0
    for idx, num in enumerate(draws):
        draws_list1[acc:acc+num].fill(idx)
        acc += num
    

    这是用于分析的完整代码。

    import numpy as np
    import cProfile
    
    M=10000000
    
    draws = np.random.multinomial(M, [1/6.]*6, size=1)
    draws = draws[0]
    
    draws_list1 = np.empty(M, dtype=np.int)
    
    def impl0():
        draws_list0 = []
        for idx, num in enumerate(draws):
            draws_list0 += [idx]*num
        return draws_list0
    
    def impl1():
        acc = 0
        for idx, num in enumerate(draws):
            draws_list1[acc:acc+num].fill(idx)
            acc += num
        return draws_list1
    
    cProfile.run("impl0()")
    cProfile.run("impl1()")
    

    这是cProfile 的结果。如果语句np.empty 位于函数impl1 中,则0.020 seconds 已过。

         3 function calls in 0.095 seconds
    
     Ordered by: standard name
    
     ncalls  tottime  percall  cumtime  percall filename:lineno(function)
          1    0.020    0.020    0.095    0.095 <string>:1(<module>)
          1    0.076    0.076    0.076    0.076 prof.py:11(impl0)
          1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
    
    
           9 function calls in 0.017 seconds
    
     Ordered by: standard name
    
     ncalls  tottime  percall  cumtime  percall filename:lineno(function)
          1    0.000    0.000    0.017    0.017 <string>:1(<module>)
          1    0.000    0.000    0.017    0.017 prof.py:17(impl1)
          1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
          6    0.017    0.003    0.017    0.003 {method 'fill' of 'numpy.ndarray' objects}
    

    【讨论】:

      猜你喜欢
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 2011-07-21
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多