Numpy concatenate is slow: any alternative approach?

前端 未结 5 1060
眼角桃花
眼角桃花 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:10

    I think @thebeancounter 's solution is the way to go. If you do not know the exact size of your numpy array ahead of time, you can also take an approach similar to how vector class is implemented in C++.

    To be more specific, you can wrap the numpy ndarray into a new class which has a default size which is larger than your current needs. When the numpy array is almost fully populated, copy the current array to a larger one.

提交回复
热议问题