如何将新行添加到空的 numpy 数组 [英] How to add a new row to an empty numpy array
问题描述
使用标准 Python 数组,我可以执行以下操作:
arr = []arr.append([1,2,3])arr.append([4,5,6])# arr 现在是 [[1,2,3],[4,5,6]]
但是,我不能在 numpy.js 中做同样的事情.例如:
arr = np.array([])arr = np.append(arr, np.array([1,2,3]))arr = np.append(arr, np.array([4,5,6]))# arr 现在是 [1,2,3,4,5,6]
我也研究了 vstack
,但是当我在一个空数组上使用 vstack
时,我得到:
ValueError: 除了串联轴之外的所有输入数组维度必须完全匹配
那么如何在 numpy 中将新行附加到空数组中?
启动"你想要的数组的方法是:
arr = np.empty((0,3), int)
这是一个空数组,但具有适当的维数.
<预><代码>>>>阿尔数组([], 形状=(0, 3), dtype=int64)然后确保沿轴 0 附加:
arr = np.append(arr, np.array([[1,2,3]]),axis=0)arr = np.append(arr, np.array([[4,5,6]]),axis=0)
但是,@jonrsharpe 是对的.事实上,如果你打算在循环中追加,那么在你的第一个例子中追加到一个列表会快得多,然后在最后转换为一个 numpy 数组,因为你真的没有使用 numpy 作为打算在循环中使用:
在 [210]: %%timeit.....: l = [].....:对于 xrange(1000) 中的 i:.....: l.append([3*i+1,3*i+2,3*i+3]).....: l = np.asarray(l).....:1000 个循环,最好的 3 个:每个循环 1.18 毫秒在 [211] 中:%%timeit.....: a = np.empty((0,3), int).....:对于 xrange(1000) 中的 i:.....: a = np.append(a, 3*i+np.array([[1,2,3]]), 0).....:100 个循环,最好的 3 个:每个循环 18.5 毫秒在 [214]: np.allclose(a, l)出[214]:真
numpythonic 的方法取决于您的应用程序,但它更像是:
在 [220]: timeit n = np.arange(1,3001).reshape(1000,3)100000 个循环,最好的 3 个:每个循环 5.93 µs在 [221]: np.allclose(a, n)出[221]:真
Using standard Python arrays, I can do the following:
arr = []
arr.append([1,2,3])
arr.append([4,5,6])
# arr is now [[1,2,3],[4,5,6]]
However, I cannot do the same thing in numpy. For example:
arr = np.array([])
arr = np.append(arr, np.array([1,2,3]))
arr = np.append(arr, np.array([4,5,6]))
# arr is now [1,2,3,4,5,6]
I also looked into vstack
, but when I use vstack
on an empty array, I get:
ValueError: all the input array dimensions except for the concatenation axis must match exactly
So how do I do append a new row to an empty array in numpy?
The way to "start" the array that you want is:
arr = np.empty((0,3), int)
Which is an empty array but it has the proper dimensionality.
>>> arr
array([], shape=(0, 3), dtype=int64)
Then be sure to append along axis 0:
arr = np.append(arr, np.array([[1,2,3]]), axis=0)
arr = np.append(arr, np.array([[4,5,6]]), axis=0)
But, @jonrsharpe is right. In fact, if you're going to be appending in a loop, it would be much faster to append to a list as in your first example, then convert to a numpy array at the end, since you're really not using numpy as intended during the loop:
In [210]: %%timeit
.....: l = []
.....: for i in xrange(1000):
.....: l.append([3*i+1,3*i+2,3*i+3])
.....: l = np.asarray(l)
.....:
1000 loops, best of 3: 1.18 ms per loop
In [211]: %%timeit
.....: a = np.empty((0,3), int)
.....: for i in xrange(1000):
.....: a = np.append(a, 3*i+np.array([[1,2,3]]), 0)
.....:
100 loops, best of 3: 18.5 ms per loop
In [214]: np.allclose(a, l)
Out[214]: True
The numpythonic way to do it depends on your application, but it would be more like:
In [220]: timeit n = np.arange(1,3001).reshape(1000,3)
100000 loops, best of 3: 5.93 µs per loop
In [221]: np.allclose(a, n)
Out[221]: True
这篇关于如何将新行添加到空的 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!