Numpy concatenate is slow: any alternative approach?

前端 未结 5 1044
眼角桃花
眼角桃花 2020-12-16 00:48

I am running the following code:

for i in range(1000)
    My_Array=numpy.concatenate((My_Array,New_Rows[i]), axis=0)

The above code is slow

5条回答
  •  無奈伤痛
    2020-12-16 01:32

    Assume you have a large list of 2D numpy arrays, with the same number of columns and different number of rows like this :

    x = [numpy_array1(r_1, c),......,numpy_arrayN(r_n, c)]

    concatenate like this:

    while len(x) != 1:
        if len(x) == 2:
            x = np.concatenate((x[0], x[1]))
            break
        for i in range(0, len(x), 2):
            if (i+1) == len(x):
                x[0] = np.concatenate((x[0], x[i]))
            else:
                x[i] = np.concatenate((x[i], x[i+1]))
    
        x = x[::2]
    

提交回复
热议问题