Python 2.7 如何将numpy数组从(#dim1,#dim2,#通道)重塑为(#通道,#dim1,#dim2)

Python 2.7 如何将numpy数组从(#dim1,#dim2,#通道)重塑为(#通道,#dim1,#dim2),python-2.7,numpy,reshape,Python 2.7,Numpy,Reshape,我有一个数组的形状是(#dim1,#dim2,#channel)。我想把它改成(#频道,#dim1,#dim2) plt.reformate(x,(#通道,#dim1,#dim2))显示了一个错误的图像。如果您使用的是Cifar10数据集,则可以使用以下代码: import numpy as np import matplotlib.pyplot as plt import cPickle def unpickle(file): with open(file, 'rb') as fo:

我有一个数组的形状是
(#dim1,#dim2,#channel)
。我想把它改成
(#频道,#dim1,#dim2)


plt.reformate(x,(#通道,#dim1,#dim2))
显示了一个错误的图像。

如果您使用的是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()
这给了你:

通过匹配图像的属性,您如何知道图像是错误的。另外
plt.imshow()
给出了
错误维度的错误
您能给出一个数据样本吗?通常,使用numpy进行整形效果很好。通过以下代码下载了Cifar10: