[英]How to insert numpy arrays of size 1 into an empty numpy array?
I am defining two NumPy arrays:我正在定义两个 NumPy 数组:
PREtrain_labels = np.asarray([inpLblsArray[0:80]])
train_labels = np.array([])
TRstore = 0
i = 0
while i < len(train_images):
TRstore = np.asarray([PREtrain_labels.item(i)])
np.append(train_labels, TRstore)
i = i + 1
Here, I have a NumPy array PREtrain_labels
which contains integers and was sliced from a larger NumPy array.在这里,我有一个 NumPy 数组
PREtrain_labels
,它包含整数并从更大的 NumPy 数组中切分。 I defined an empty array train_labels
.我定义了一个空数组
train_labels
。 My goal is to fill the empty NumPy array ( train_labels
) with the selected slice of integers in the array PREtrain_labels
.我的目标是,以填补空与NumPy阵列(
train_labels
与在整数数组所选切片) PREtrain_labels
。 But there is a catch, as I take each integer from the PREtrain_labels
array, I want each integer to be placed inside another empty array which is called TRstore
.但是有一个问题,当我从
PREtrain_labels
数组中取出每个整数时,我希望每个整数都放在另一个名为TRstore
空数组中。 Then, I want to take the TRstore
NumPy array and place it inside the empty train_labels
array.然后,我想将
TRstore
NumPy 数组放入空的train_labels
数组中。 However, when I run the code and print the final train_labels
array, it is empty.但是,当我运行代码并打印最终的
train_labels
数组时,它是空的。
How can possibly fix this?怎么可能解决这个问题? If
np.append()
is the wrong method to use, which one should I use?如果
np.append()
是错误的使用方法,我应该使用哪一种? The code above will not run alone, so, I am simplifying my code below into a runnable version.上面的代码不会单独运行,因此,我将下面的代码简化为可运行的版本。 Thanks in advance!
提前致谢!
loopArr = np.array([1, 2, 3, 4, 5])
a = np.asarray([1, 2, 3, 4, 5])
target = np.array([])
store = 0
i = 0
while i < len(loopArr):
store = np.asarray([a.item(i)])
np.append(target, store)
i = i + 1
print(target)
In [30]: res = np.array([])
In [31]: np.append(res, np.array(1))
Out[31]: array([1.])
In [32]: res
Out[32]: array([], dtype=float64)
Out[31}
is a 1 element float array - because res
is float. Out[31}
是一个 1 元素的浮点数组 - 因为res
是浮点数。 But note that res
has not changed.但请注意
res
没有改变。 np.append
is a just badly conceived 'cover' for np.concatenate
. np.append
是一个公正的构思不佳“盖” np.concatenate
。
concatenate
is a bit pickier about the dimensions of the inputs concatenate
对输入的维度有点挑剔
In [33]: np.concatenate([ res, np.array(1)])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-33-bb45ea1930d0> in <module>
----> 1 np.concatenate([ res, np.array(1)])
<__array_function__ internals> in concatenate(*args, **kwargs)
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 0 dimension(s)
In [34]: np.concatenate([ res, np.array([1])])
Out[34]: array([1.])
This joins one (0,) shape array with a (1,) shape to produce a (1,).这将一个 (0,) 形状的数组与一个 (1,) 形状连接起来以产生一个 (1,)。 That
[]
array is nearly useless.那个
[]
数组几乎没用。
concatenate
takes a whole list of arrays (or even lists). concatenate
需要一个完整的数组列表(甚至是列表)。 Take advantage of that:充分利用这一点:
In [35]: np.concatenate([np.array([1]), np.array([2]), [3], [1,2,3]])
Out[35]: array([1, 2, 3, 1, 2, 3])
But why are you doing append
in a loop?但是你为什么要在循环中
append
呢? Why not list appends?为什么不列出追加? That works in-place and is relatively fast and error free.
就地工作并且相对快速且无错误。 Why make life hard for yourself?
为什么要让自己的生活变得艰难?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.