You have the wrong mental model for using NumPy efficiently.
(您对有效使用NumPy的思维模式有误。)
NumPy arrays are stored in contiguous blocks of memory. (NumPy数组存储在连续的内存块中。)
If you want to add rows or columns to an existing array, the entire array needs to be copied to a new block of memory, creating gaps for the new elements to be stored. (如果要向现有阵列添加行或列,则需要将整个阵列复制到新的内存块中,从而为要存储的新元素创建间隙。)
This is very inefficient if done repeatedly to build an array. (如果反复进行以构建数组,则效率非常低下。)
In the case of adding rows, your best bet is to create an array that is as big as your data set will eventually be, and then add data to it row-by-row:
(在添加行的情况下,最好的选择是创建一个与数据集最终大小一样大的数组,然后逐行向其中添加数据:)
>>> import numpy
>>> a = numpy.zeros(shape=(5,2))
>>> a
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
>>> a[0] = [1,2]
>>> a[1] = [2,3]
>>> a
array([[ 1., 2.],
[ 2., 3.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…