0

I am doing a classification problem. My training set is X_train containing 60000 elements and each element has 784 features (basically the intensity of pixels of an image). I want to reshape the images in a 28 * 28 array and store them in another array. I tried but can't find a solution. How can I do that?

for x in range(60000):
    X_new=X_train[x].reshape(28,28)

len(X_new)

I expect len(X_new) be 60000 but its length is showing as 28.

1
  • I've seen this error too many times (overwriting a variable each time instead of appending to a list), but this might be the purest example. I wonder if there is a duplicate I can point people to? Commented Jun 25, 2019 at 7:03

3 Answers 3

1

Without context, both other answers might be right. However, I'm going to venture a guess that your X_train is already a numpy.array with shape (60000, 784). In this case len(X_train) will return 60000. If so, what you want to do is simply:

X_new = X_train.reshape((-1, 28, 28))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for such a great help.
0

You should assign X_train[x] instead of X_new:

for x in range(60000): X_train[x] = X_train[x].reshape(28,28)

otherwise, X_new will store only the last element of the list. You can create a new array if you do not want to spoil the old one:

X_new = [X_train[x].reshape(28,28) for x in range(60000)]

Comments

0

Possibly you mean to do this:

X_new = []
for x in range(60000):
    X_new.append(X_train[x].reshape(28, 28))

len(X_new)

You can also use a list comprehension

X_new = [x.reshape(28, 28) for x in X_train]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.