Optimal way to append to numpy array

前端 未结 2 1780
我在风中等你
我在风中等你 2020-12-31 19:11

I have a numpy array and I can simply append an item to it using append, like this:

numpy.append(myarray, 1)

In this case I just appended t

2条回答
  •  没有蜡笔的小新
    2020-12-31 19:21

    If you know the size of the array at the end of the run, then it is going to be much faster to pre-allocate an array of the appropriate size and then set the values. If you do need to append on-the fly, it's probably better to try to not do this one element at a time, instead appending as few times as possible to avoid generating many copies over and over again. You might also want to do some profiling of the difference in timings of np.append, np.hstack, np.concatenate. etc.

提交回复
热议问题