2017-04-25 67 views
0

我正在使用該接收一組的形狀M x NI灰度圖像作爲參數的功能的應用。numpy的重塑的數據集

print(train_set.shape)  
print(train_set[0].shape) 

>>(50,) 
>>(133, 100) 

我需要重塑這個數據集來塑造(I, M, N)

我想

train_set.reshape(len(train_set),len(train_set[0]),len(train_set[0][0])) 

但我有以下錯誤

>>>ValueError: cannot reshape array of size 50 into shape (50,133,100) 

所以,我怎樣才能重塑我的數據集(I, M, N)(50, 133, 100)在這種情況下?

編輯: 這是我如何建立數據集中

train_set =[] 
lbl_train=[] 
for cl_ in range(50): 
    for ex_ in range(15): 
     cl = str("%02d" % (cl_+1,)) 
     ex = str("%02d" % (ex_+1,)) 
     img = Image.open('cropped_faces/'+'s'+str(cl)+'_'+str(ex)+'.jpg').convert('L') 
     wpercent = (basewidth/float(img.size[0])) 
     hsize = int((float(img.size[1]) * float(wpercent))) 
     img = img.resize((basewidth, hsize), Image.ANTIALIAS) 
     lbl_train.append(cl) 
     mat = numpy.asarray(img,dtype=numpy.float) 
     train_set.append(mat.reshape(len(mat),len(mat[0]))) 

print(numpy.asarray(train_set).shape) 
print(numpy.asarray(train_set[0]).shape) 
+0

當你print(train_set [0] [0] .shape)時,你會得到什麼? –

+2

嘗試'np.stack(train_set)'。它看起來像你有一個數組的數組(但這些信息缺乏的問題)。根據你如何建立'train_set',numpy可能不知道如何自動將單個圖像合併成一個大陣列。 – kazemakase

+0

類型(train_set)和類型(train_set [0])的輸出是什麼? – Allen

回答

0

好吧,我想你已經有了在train_set是numpy的陣列的NumPy的對象數組,而不是一個多維數組。這應該給你你需要的東西:

#the np.r_ function will stack all the elements from train_set as a new array with the shape(len(train_set),len(train_set[0]),len(train_set[0][0])) 
train_set = np.r_[train_set.tolist()] 
train_set.shape 
Out[788]: (50, 133, 100) 
+0

我做了什麼,你說,但輸出仍是一樣的'train_set = numpy.r_ [train_set.tolist()] 打印(train_set.shape)'>>(50, ) –

+0

我發現了這個問題,train_set中的一些圖片有不同的尺寸...前六張圖片(133,100) (129,100) (149,100) (149,100) (151,100 ) (149,100) –