我正在尝试使用
保存数组print myArray.shape, myArray.dtype # returns (yyy, xxx) uint8
numpy.save('savedFile.npy',myArray)
然后再次使用
加载它myArray = numpy.load('savedFile.npy')
print myArray.shape, myArray.dtype # returns (yyy, ) object
如何使阵列与之前的形状和类型相同?
谢谢!
---编辑---
以下是转载的问题:
import numpy as np
import cv2, os.path
allImageIDs = []
allImages = []
countImagesMax = 20
countImages = 0
while countImages < countImagesMax:
image = np.uint8(np.random.randint(2, size=(144,192)) *255)
allImages.append(np.reshape(image, (image.shape[0]*image.shape[1])))
allImageIDs.append(countImages)
countImages += 1
myArray = [np.array(allImages), np.array(allImageIDs)]
if not os.path.exists("savedFile.npy"):
np.save('savedFile.npy',myArray)
print myArray[0].shape, myArray[0].dtype # returns (yyy, xxx) uint8
else:
myArray = np.load('savedFile.npy')
print myArray[0].shape, myArray[0].dtype # returns (yyy, ) object
答案 0 :(得分:0)
评论中的讨论帮助我找到了使用np.savez
代替np.save
的解决方案。谢谢大家的意见。
import numpy as np
import cv2, os.path
allImageIDs = []
allImages = []
countImagesMax = 20
countImages = 0
while countImages < countImagesMax:
image = np.uint8(np.random.randint(2, size=(144,192)) *255)
allImages.append(np.reshape(image, (image.shape[0]*image.shape[1])))
allImageIDs.append(countImages)
countImages += 1
myArray = [np.array(allImages), np.array(allImageIDs)]
if not os.path.exists("savedFile.npz"):
np.savez('savedFile.npz', images=myArray[0], ids=myArray[1])
print myArray[0].shape, myArray[0].dtype # returns (yyy, xxx) uint8
else:
npzfile = np.load('savedFile.npz')
myArray = [npzfile['images'], npzfile['ids']]
print myArray[0].shape, myArray[0].dtype # returns (yyy, xxx) uint8