首页 > 解决方案 > 多维 Numpy 数组的 Numpy.reshape 给出相同的大小但不同的数字顺序

问题描述

我想将 shape 数组转换(a, b, c, d)为 shape (b*c*d, a)

问题是 Array A 和 B 的输出不同:

x = np.array([np.arange(1,37)]) #1D array of 1 to 36.
a = 3
b = 2
c = 2
d = 3
Array = np.reshape(x, (a, b, c, d)) #shape (3, 2, 2, 3)

A = poop.reshape(a, b*c*d).T
B = poop.reshape(b*c*d, a)


print("Array shape:" + str(Array.shape))
print(Array)
print("#####")
print("Array A:")
print(A)
print("#####")
print("Array B:")
print(B)

我认为 Numpy 的重塑将(row, column)作为输入,因此反转然后转置会导致相同的结果(column, row).T

更改“Order”参数似乎不会使数组 B 与数组 A 匹配。

谢谢你。

输出是:

Array shape:(3, 2, 2, 3)
[[[[ 1  2  3]
   [ 4  5  6]]

  [[ 7  8  9]
   [10 11 12]]]


 [[[13 14 15]
   [16 17 18]]

  [[19 20 21]
   [22 23 24]]]


 [[[25 26 27]
   [28 29 30]]

  [[31 32 33]
   [34 35 36]]]]
#####
Array A:
[[ 1 13 25]
 [ 2 14 26]
 [ 3 15 27]
 [ 4 16 28]
 [ 5 17 29]
 [ 6 18 30]
 [ 7 19 31]
 [ 8 20 32]
 [ 9 21 33]
 [10 22 34]
 [11 23 35]
 [12 24 36]]
#####
Array B:
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]
 [13 14 15]
 [16 17 18]
 [19 20 21]
 [22 23 24]
 [25 26 27]
 [28 29 30]
 [31 32 33]
 [34 35 36]]

标签: pythonarraysnumpymultidimensional-array

解决方案


推荐阅读