我有5个大型numpy数组,我想将它们合并为一个numpy数组。使用np.concatenate没有帮助,因为MemoryError:Unable toallocate ...所以我决定使用np.memmap。我的数组形状如下:
#print(arrayA.shape) (29097, 280, 212, 3)
#print(arrayB.shape) (16058, 280, 212, 3)
#print(arrayC.shape) (15412, 280, 212, 3)
#print(arrayD.shape) (21634, 280, 212, 3)
#print(arrayF.shape) (9477 , 280, 212, 3)
我的代码:
import glob
import numpy as np
npfiles= glob.glob("D:/mycode/*.npy")
npfiles.sort()
#print(npfiles)
# create a memory-mapped array
pred = np.memmap('memm4', dtype='uint8', mode='w+', shape=(91678,280,212,3))
print(pred.shape)
for i,npfile in enumerate(npfiles):
pred[i,:,:,:]=np.load(npfile)
np.save('D:/mycode/pred.npy',pred)
但它向我显示了这个问题“无法将输入数组从形状(29097,280,212,3)广播到形状(280,212,3)有人可以帮助我,谢谢
目前,您将3维张量放入4维1i
变量包含文件从0开始的索引4。因此pred[i,:,:,:]
仅具有三维但是您需要指出要在内存中存储阵列的位置。
last_index = 0
for npfile in npfiles:
temporary_array = np.load(npfile)
pred[last_index:last_index+ len(temporary_array),:,:,:] = temporary_array
last_index += len(temporary_array)
您可能还想尝试类似hdf5 /的方法,它可以使您轻松存储大型阵列。