元组使用NumPy数组的第一个单体维度

2024-09-29 17:13:49 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一个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)

Tags: numpy列表nprandom数组arraylistprint
1条回答
网友
1楼 · 发布于 2024-09-29 17:13:49

tuple()根据第一个维度提取元素。np.random.randn(1,1,2)是一个1x1x2矩阵。tuple()将其转换为以下tuple:(1x2矩阵,)

另一方面,如果使用np.random.randn(2,1,1),tuple()将其转换为:(1x1矩阵,1x1矩阵)

相关问题 更多 >

    热门问题