0

While read .mat format data with Python and show as 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()

It raises an error: ValueError: cannot reshape array of size 784 into shape (16,16).

How could I reshape it correctly? Thanks.

X's shape:

print(X.shape)

Out:

(10000, 784)

Data's keys:

print(digits.keys())

Out:

dict_keys(['__header__', '__version__', '__globals__', 'Y', 'X'])
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
ah bon
  • 9,293
  • 12
  • 65
  • 148

2 Answers2

3

Of course, the solution is easier than you think.

ValueError: cannot reshape array of size 784 into shape (16,16).

28 x 28 = 784

Therefore, you need to reshape into the format (28,28) rather than (16,16)

Timbus Calin
  • 13,809
  • 5
  • 41
  • 59
0

I had similar problems but not with images, it looks similar in a way, so here is how I did mine. You might need to resize the data first: the data in the code below is your size =784, you do not necessarily need to abandon your shape

datas= np.array([data], order='C')
datas.resize((16,16))
datas.shape
derek
  • 21
  • 7