![](/img/trans.png)
[英]np.transpose() and np.reshape() combination gives different results in pure numpy and in numba
[英]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 -> 1
、 1 -> 2
和2 -> 3
。 在np.transpose()
中,您请求订单0,2,1
即1,3,2
。
在第二种情况下,您的数组形状是(4,2,3)
即dimension->value
格式,它是0 -> 4
、 1 -> 2
和2 -> 3
。 在np.transpose()
中,您请求订单2,0,1
即3,4,2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.