找出适当的numpy.reshape为-1选项

时间:2017-11-17 10:15:01

标签: python arrays numpy reshape

我有一个(希望)快速的Numpy问题,我希望你能帮助我。 我想用numpy.reshape将(5000,32,32,3)转换成(5000,3072),我得到的唯一线索就是:

# Reshape each image data into a 1-dim array
print (X_train.shape, X_test.shape) # Should be: (5000, 32, 32, 3) (500, 32, 32, 3)
#####################################################################
# TODO (2):                                                         #
# Reshape the image data to one dimension.                          #
#                                                                   #
# Hint: Look at the numpy reshape function and have a look at -1    # 
#       option                                                      #
#####################################################################
X_train = 
X_test = 
#####################################################################
#                       END OF YOUR CODE                            #
#####################################################################
print (X_train.shape, X_test.shape) # Should be: (5000, 3072) (500, 3072)

我一直在花费最后一天搜索谷歌的例子,但显然这太微不足道了。帮助

1 个答案:

答案 0 :(得分:1)

您可以这样做:

X_train = np.reshape(X_train, (5000, -1))
X_test = np.reshape(X_test, (500, -1))

工作示例:

import numpy as np
a = np.zeros((5000,32,32,3))
b = np.reshape(a, (5000, -1))

print(a.shape)
print(b.shape)

# Output
# (5000, 32, 32, 3)
# (5000, 3072)

numpy.reshape 将尝试将源数组 a 放入一个长度为5000的第一个维度的数组中。-1告诉reshape调整第二个长度维度取决于源数组的总长度 a