给定一个NumPy数组:
array = np.random.randn(1,1,2)
print(array.shape)
# (1, 1, 2)
对其调用tuple()会占用第一个维度:
tupled_array = tuple(array)
print(tupled_array[0].shape)
# (1, 2) <- why?
我很好奇为什么
如果我们用列表包装NumPy数组:
tupled_list_array = tuple([array])
print(tupled_list_array[0].shape)
# (1, 1, 2)
tuple()根据第一个维度提取元素。np.random.randn(1,1,2)是一个1x1x2矩阵。tuple()将其转换为以下tuple:(1x2矩阵,)
另一方面,如果使用np.random.randn(2,1,1),tuple()将其转换为:(1x1矩阵,1x1矩阵)
相关问题 更多 >
编程相关推荐