繁体   English   中英

元组消耗 NumPy 数组的第一个 singleton 维度

[英]Tuple consumes first singleton dimension of NumPy array

给定一个 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() 将其转换为以下元组: (1x2 matrix, )

另一方面,如果你使用 np.random.randn(2,1,1),tuple() 将它变成:(1x1 矩阵,1x1 矩阵)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM