Numpy transpose()如何按axis重新布局数据

background: Numpy transpose的axis在N维情况下如何重新布局数据的?虽然axis与stack/mean/median/sum/joint的axis一样,但自己能快速知道新数据如何布局有点难度
 

重点:

(1) 提问者的例子比较特别,第0与第1维都包含2个(子)数组,所以重新布局的结果上看上去变化不大。参第1个回答Alex Riley的图中整数用int64,这个应该是回答者的一种假设,并不是说明Numpy默认使用int64. 

 提问者的原数组

[  [  [ 0,  1,  2,  3],  [ 4,  5,  6,  7]],   [ [ 8,  9, 10, 11],  [12, 13, 14, 15] ] ]

 transpose后结果变成

[  [[ 0,  1,  2,  3],   [ 8,  9, 10, 11]]  ,  [[ 4,  5,  6,  7],   [12, 13, 14, 15]]  ]

参考:python - How does NumPy's transpose() method permute the axes of an array? - Stack Overflow

优点:第1回答人的解答最形象,图很清晰生动,从内存角度理解如何分组。

 提问者的原数组

[  [  [ 0,  1,  2,  3],  [ 4,  5,  6,  7]],   [ [ 8,  9, 10, 11],  [12, 13, 14, 15] ] ]

3个axis的可视化表达

内存中transpose根据axis读取数据

 一旦指定新的axis ,新数组还是(2,2,4)

  这里注意axis1 数据已经变化了

内存数据排列虽然没变,但读取位置已经变了

第5回答人James Shapiro从数字角度的解释让人非常容易理解数组坐标之间是如何交换的。可以作为参考,但头脑中但不容易快速知道新数组的样子

Simple 3-dimensional tensor example

Suppose you have the (2,2,4)-tensor

[[[ 0  1  2  3]
  [ 4  5  6  7]]

 [[ 8  9 10 11]
  [12 13 14 15]]]

If we look at the coordinates of each point, they are as follows:下面是按坐标方式显示每个数值的位置

[[[ (0,0,0)  (0,0,1)  (0,0,2)  (0,0,3)]
  [ (0,1,0)  (0,1,1)  (0,1,2)  (0,1,3)]]

 [[ (1,0,0)  (1,0,1) (1,0,2) (0,0,3)]
  [ (1,1,0)  (1,1,1) (1,1,2) (0,1,3)]]

Now suppose that the array above is example_array and we want to perform the operation: example_array.transpose(1,2,0)

For the (1,2,0)-transformation, we shuffle the coordinates as follows (note that this particular transformation amounts to a "left-shift": 下面就是数组按axis坐标做了交换,形成新的数组。原来的第1维坐标放到现在的第0维,原来的第2维坐标放现在第1维,原来的第0维坐标放现在第2维

(0,0,0)  ->  (0,0,0)
(0,0,1)  ->  (0,1,0)
(0,0,2)  ->  (0,2,0)
(0,0,3)  ->  (0,3,0)
(0,1,0)  ->  (1,0,0)
(0,1,1)  ->  (1,1,0)
(0,1,2)  ->  (1,2,0)
(0,1,3)  ->  (1,3,0)
(1,0,0)  ->  (0,0,1)
(1,0,1)  ->  (0,1,1)
(1,0,2)  ->  (0,2,1)
(0,0,3)  ->  (0,3,0)
(1,1,0)  ->  (1,0,1)
(1,1,1)  ->  (1,1,1)
(1,1,2)  ->  (1,2,1)
(0,1,3)  ->  (1,3,0)

Now, for each original value, place it into the shifted coordinates in the result matrix.

For instance, the value 10 has coordinates (1, 0, 2) in the original matrix and will have coordinates (0, 2, 1) in the result matrix. It is placed into the first 2d tensor submatrix in the third row of that submatrix, in the second column of that row.

Hence, the resulting matrix is:

array([[[ 0,  8],
        [ 1,  9],
        [ 2, 10],
        [ 3, 11]],

       [[ 4, 12],
        [ 5, 13],
        [ 6, 14],
        [ 7, 15]]])

General n-dimensional tensor approach

For n-dimensional tensors, the algorithm is the same. Consider all of the coordinates of a single value in the original matrix. Shuffle the axes for that individual coordinate. Place the value into the resulting, shuffled coordinates in the result matrix. Repeat for all of the remaining values.

缺点: N/A

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值