问题描述
同时使用 Python 读取 .mat
格式的数据并显示为 plt
:
import scipy.io as spio
import numpy as np
import matplotlib.pyplot as plt
digits = spio.loadmat('./data/digits.mat',squeeze_me=True)
X = digits['X']
plt.imshow(np.reshape(X[5,:],(16,16)),cmap='Greys')
plt.show()
它引发了一个错误:ValueError: cannot reshape array of size 784 into shape (16,16)
。
我怎样才能正确地重塑它?谢谢。
X
的形状:
print(X.shape)
出:
(10000,784)
数据键:
print(digits.keys())
出:
dict_keys(['__header__','__version__','__globals__','Y','X'])
解决方法
当然,解决方案比您想象的要容易。
ValueError: 无法将大小为 784 的数组重塑为形状 (16,16)。
28 x 28 = 784
因此,您需要重塑为格式(28,28)
而不是(16,16)