ndarray的快速变异(替换numpy ndarray的一部分)

时间:2018-02-19 15:00:23

标签: python numpy

我正在寻找替换部分预先分配的numpy ndarray的最快方法。 您可以在下面看到sim_matrix(2D ndarray)。 " For loop"迭代它 rowwise 并覆盖levels(numpy.array)给出的值。

有很多方法可以找到:

  1. numpy.copyto()
  2. numpy.fromiter()
  3. =分配到
  4. 注意:为简单起见,我在每次迭代时分配相同的值,而在生产设置中不是这种情况。在实际设置中,levels包含每行的不同值。因此,不鼓励使用numpy.repeatnumpy.hstack作为答案。

    有没有更快的方法来更改ndarray的部分内容?

    numpy_overwrite.py:

    import numpy
    panel_size = 365 * 7
    rows = 1 * (1+1+2*4) * 10000
    # rows = 100
    levels = [1778.24] * panel_size
    levels_array = numpy.fromiter(levels,dtype='single', count = len(levels))
    sim_matrix = numpy.empty([rows, panel_size],dtype='single')
    
    def _copyto(sim_matrix_, level_):
        for i in range(sim_matrix_.shape[0]):
            numpy.copyto(sim_matrix[i], level_)
    
    
    def _fromiter(sim_matrix_, level_):
        len_level = len(level_)
        for i in range(sim_matrix_.shape[0]):
            sim_matrix[i] = numpy.fromiter(level_,
                                          dtype='single', 
                                          count = len_level)
    
    def _just_assign(sim_matrix_, level_):
        for i in range(sim_matrix_.shape[0]):
            sim_matrix[i] = level_
    

    numpy_overwrite_test.py

    import timeit
    import numpy_overwrite as npo
    print('_fromiter')
    print(timeit.timeit('npo._fromiter(npo.sim_matrix, npo.levels)', setup="import numpy_overwrite as npo;", number=3))
    print(timeit.timeit('npo._fromiter(npo.sim_matrix, npo.levels_array)', setup="import numpy_overwrite as npo;", number=3))
    print('_copyto')
    print(timeit.timeit('npo._copyto(npo.sim_matrix, npo.levels)', setup="import numpy_overwrite as npo;", number=3))
    print(timeit.timeit('npo._copyto(npo.sim_matrix, npo.levels_array)', setup="import numpy_overwrite as npo;", number=3))
    print('_just_assign')
    print(timeit.timeit('npo._just_assign(npo.sim_matrix, npo.levels)', setup="import numpy_overwrite as npo;", number=3))
    print(timeit.timeit('npo._just_assign(npo.sim_matrix, npo.levels_array)', setup="import numpy_overwrite as npo;", number=3))
    

    运行测试

    _fromiter
    =========
    from python list: 21.129429172957316
    from numpy.array: 61.76275303697912
    
    _copyto
    =========
    from python list: 52.4522930260282
    from numpy.array: 0.7605530479922891
    
    _just_assign
    =========
    from python list: 52.251478374004364
    from numpy.array: 0.6351132979616523
    

1 个答案:

答案 0 :(得分:1)

您是否可能只是在寻找这个?

sim_matrix[:] = levels

编辑:要修改单行,应使用sim_matrix[row_id] = levels

在旁注中,请注意using mutables as default function arguments