Reputation: 320
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.
Upvotes: 0
Views: 232
Reputation: 1533
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))
Upvotes: 1
Reputation: 304147
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]
Upvotes: 0
Reputation: 2119
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)]
Upvotes: 0