繁体   English   中英

np.transpose 行为因不同的数组结构而异

[英]np.transpose behavior different for different array constructions

我在np.transpose中遇到了这种奇怪的行为,其中在numpy数组和从列表构造的数组上使用时它的工作方式不同。 作为 MWE,提供了以下代码。

import numpy as np

a = np.random.randint(0, 5, (1, 2, 3))
print(a.shape)
# prints (1, 2, 3)
b = np.transpose(a, (0, 2, 1))
print(b.shape)
# prints (1, 3, 2), which is expected

# Constructing array from list of arrays
c = np.array([np.random.randint(0, 5, (2, 3)), np.random.randint(0, 5, (2, 3)), np.random.randint(0, 5, (2, 3)), np.random.randint(0, 5, (2, 3))])
print(c.shape)
# prints (4, 2, 3)
d = np.transpose(c, (2, 0, 1))
print(d.shape)
# prints (3, 4, 2), whereas I expect it to be (2, 3, 4)

我不明白这种行为。 为什么从列表构造的数组的维度混淆了? 任何帮助表示赞赏。

np.transpose()按照您指定的顺序选择您指定的维度。

在您的第一种情况下,您的数组形状是(1,2,3)dimension->value格式,它是0 -> 11 -> 22 -> 3 np.transpose()中,您请求订单0,2,11,3,2

在第二种情况下,您的数组形状是(4,2,3)dimension->value格式,它是0 -> 41 -> 22 -> 3 np.transpose()中,您请求订单2,0,13,4,2

暂无
暂无

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

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