ah bon
ah bon

Reputation: 10051

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

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'])

Upvotes: 0

Views: 6200

Answers (2)

D.lola
D.lola

Reputation: 2319

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

Upvotes: 0

Timbus Calin
Timbus Calin

Reputation: 15043

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)

Upvotes: 3

Related Questions