为什么无法将形状为(60000,10,1)到(60000,10)的数组重塑?

时间:2019-04-16 16:02:10

标签: python-3.x numpy reshape

我正在学习张量流课程,其中一个步骤是将形状为(60000,10,1)的numpy数组重塑为形状(60000,10)。但是在调用 ndarray.reshape 方法后,形状似乎没有改变-仍然是(60000,10,1)。

但是,我尝试直接设置 shape 属性,它可以正常工作!喜欢:

# Setting shape attribute works - fragment of python code
# Shape of training[1] is (60000, 10, 1)

training[1] = np.array([vectorized_result(y) for y in training[1]])

training[1].shape = (training[1].shape[0],training[1].shape[1])

# print the shape
print(training[1].shape)


# definition of vectorized_result
def vectorized_result(j):
    """Return a 10-dimensional unit vector with a 1.0 in the jth
    position and zeroes elsewhere.  This is used to convert a digit
    (0...9) into a corresponding desired output from the neural
    network."""
    e = np.zeros((10, 1))
    e[j] = 1.0
    return e

运行代码,然后获取

$ python test.py
(60000, 10)

使用 reshape 方法,但失败

# fragment of python code

training[1] = np.array([vectorized_result(y) for y in training[1]])

training[1].reshape((training[1].shape[0],training[1].shape[1]))

# print the shape
print(training[1].shape)


# definition of vectorized_result
def vectorized_result(j):
    """Return a 10-dimensional unit vector with a 1.0 in the jth
    position and zeroes elsewhere.  This is used to convert a digit
    (0...9) into a corresponding desired output from the neural
    network."""
    e = np.zeros((10, 1))
    e[j] = 1.0
    return e

运行代码,然后获取


$ python test.py
(60000, 10, 1)

请帮助我解决这个问题。

非常感谢。

1 个答案:

答案 0 :(得分:0)

尝试以下更改:

training[1] = training[1].reshape((training[1].shape[0],training[1].shape[1]))