如何通过迭代创建其他 numpy 数组的 numpy 数组?



我正在尝试制作一个 numpy 数组,其中每个元素都是一个 (48,48( 形状的 numpy 数组,本质上是一个大列表,我可以在其中迭代并检索每次不同的 48x48 数组。

for i in range(1):
new_image = np.fromstring(train_np[i][1],dtype=int,sep=" ")
new_image = new_image.reshape(48,48) #create the new 48x48 correctly
image_train = np.stack([image_train,new_image]) #this line only works once

当范围为 1(仅运行一次(时,stack 返回预期结果形状 (2, 48 48(。不过运行不止一次,产量

ValueError: all input arrays must have the same shape

在这种情况下,有没有比 np.stack 更好的操作?我想迭代一下,看看形状变成 (2,48,48( -> (3,48,48( -> (4,48,48( ...等等。

numpy

针对大型并行计算进行了优化。 它在设计上没有非常灵活的数据形状,因此,如果您无法对数据的形状进行硬编码,通常最好使用列表(灵活(,然后在末尾调用numpy来堆叠它们。

image_train = []
for i in range(1):
new_image = np.fromstring(train_np[i][1],dtype=int,sep=" ")
new_image = new_image.reshape(48,48) 
image_train.append(new_image) 
image_train = np.stack(image_train)

也就是说,你可以为此使用np.concatenate,但这将是非常缓慢和糟糕的做法。

最新更新