[英]How can I make an array in python whose elements are 2 array
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). 我的训练集是
X_train
包含60000
元素,每个元素具有784
特征(基本上是图像像素的强度)。 I want to reshape the images in a 28 * 28
array and store them in another array. 我想在
28 * 28
数组中重塑图像并将它们存储在另一个数组中。 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. 我期望
len(X_new) be 60000
但其长度显示为28。
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)
. 但是,我冒昧地猜测您的
X_train
已经是一个形状为(60000, 784)
X_train
)的numpy.array
。 In this case len(X_train)
will return 60000
. 在这种情况下,
len(X_train)
将返回60000
。 If so, what you want to do is simply: 如果是这样,您要做的只是:
X_new = X_train.reshape((-1, 28, 28))
You should assign X_train[x]
instead of X_new
: 您应该分配
X_train[x]
而不是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. 否则,
X_new
将仅存储列表的最后一个元素。 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)]
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.