繁体   English   中英

在 Numpy 中将 3D 重塑为 2D 不会产生预期的 output

[英]Reshaping 3D to 2D in Numpy does not produced intended output

给定一个 3D 数组如下

nnodes,nslice,nsbj=2,4,3
arr=np.random.randint(10, size=(nsbj,nslice,nnodes))

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

我想把它重塑成一个形状数组 (2,12)

5   3   7   3   2   7   8   1   7   8   5   8
0   3   9   5   4   6   8   6   7   1   9   9

使用 A,C,F 的order变化不会产生预期的 output

arr.(2,-1,order='F') # tested also against A, and C

同样,以下也没有给出我的意图

arr.transpose(0,2,1).reshape(2,-1,order='F')

为了重现性,这里是玩具代码

import numpy as np
np.random.seed(0)
nnodes,nslice,nsbj=2,4,3
arr=np.random.randint(10, size=(nsbj,nslice,nnodes))

你可以ravel / flatten然后reshape

arr.ravel().reshape(2,-1,order='F')

output:

array([[5, 3, 7, 3, 2, 7, 8, 1, 7, 8, 5, 8],
       [0, 3, 9, 5, 4, 6, 8, 6, 7, 1, 9, 9]])

替代reshapetranspose

arr.reshape(-1,2).T

暂无
暂无

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

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