2014-04-24 4 views
-1

내가파이썬 NumPy와 : 저장된 배열을 다르게

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 

다시로드하려고로드하고 이전과 같은 dtype?

감사합니다. 코멘트에

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 
+2

은 'numpy.save'가 아니라'numpy.safe'라고 말할까요? ;-) – mgilson

+0

예, 그렇습니다 ... 답변을 제공하는 데 도움이됩니까? :) – cowhi

+3

나는 이것을 재현 할 수 없다. 문제를 설명하는 완전하고 독립적 인 예를 포함시킬 수 있습니까? 우리가 복사하고 실행할 수있는 것. 'uint8' 유형의 2D 배열을 저장하고로드하면로드 된 배열의 모양과 유형이 저장된 배열과 동일합니다. –

답변

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 
관련 문제