2017-04-26 46 views
0

我有一個形狀爲(#dim1,#dim2,#channel)的數組。我想重塑它到(#channel, #dim1,#dim2)如何將(#dim1,#dim2,#channel)中的numpy數組重塑爲(#channel,#dim1,#dim2)

plt.reshape(x, (#channel, #dim1,#dim2))顯示我一個錯誤的形象。

+0

你怎麼知道的形象是錯誤的? – Robbie

+0

通過匹配其屬性。另外''plt.imshow()''給出''錯誤的尺寸''的錯誤 –

+0

你可以提供你的數據樣本嗎?一般重塑使用numpy的作品相當好。 – Robbie

回答

0

如果您使用的是Cifar10數據集,你可以使用下面的代碼:

import numpy as np 
import matplotlib.pyplot as plt 
import cPickle 

def unpickle(file): 
    with open(file, 'rb') as fo: 
     dict = cPickle.load(fo) 
    return dict 

# Read the data 
imageDict = unpickle('cifar-10-batches-py/data_batch_2') 
imageArray = imageDict['data'] 

# Now we reshape 
imageArray = np.swapaxes(imageArray.reshape(10000,32,32,3,order='F'), 1, 2) 

# Get the labels 
labels = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck'] 
imageLabels = [labels[i] for i in imageDict['labels']] 

# Plot some images 
fig, ax = plt.subplots(4,4, figsize=(8,8)) 
for axIndex in [(i,j) for i in range(4) for j in range(4)]: 
    index = np.random.randint(0,10000) 
    ax[axIndex].imshow(imageArray[index], origin='upper') 
    ax[axIndex].set_title(imageLabels[index]) 
    ax[axIndex].axis('off') 
fig.show() 

它給你: enter image description here

相關問題