python - 将 Numpy 数组从 (3, 2, 3) reshape 为 (3, 3, 2)

标签 python arrays numpy reshape

我有以下形状为 (3, 2, 3) 的 3d numpy 数组。

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

 [[ 6  7  8]
  [ 9 10 11]]

 [[12 13 14]
  [15 16 17]]
             ]

但是,我需要按以下顺序 reshape 为 (3, 3, 2):

[
 [[ 0  3]
  [ 6  9]
  [12 15]]

 [[ 1  4]
  [ 7 10]
  [13 16]]

 [[ 2  5]
  [ 8 11]
  [14 17]]
          ]

我目前正在使用 Jupyter,并进行了大量的试验和错误。

谢谢您的建议!

最佳答案

要定义原始数组,您可以使用:

np.arange(18).reshape(3,2,3)

正如@Divakar 在评论中提到的,您可以使用:

np.arange(18).reshape(3,2,3).transpose(2,0,1)

得到想要的结果。

来自 np.transpose documentation :

axes: By default, reverse the dimensions, otherwise permute the axes according to the values given.

2,0,1是从 (3,2,3) 开始所需的排列形状为(3,3,2) 。它还会转换 (3400, 7, 100)塑造成(100, 3400, 7) .

另一种方法是使用 np.rollaxis (来自@Divakar 的另一个提示):

np.rollaxis(np.arange(18).reshape(3,2,3),2)

关于python - 将 Numpy 数组从 (3, 2, 3) reshape 为 (3, 3, 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062921/

相关文章:

Python Sphinx 包含指令 : ignore the header from included file

c++ - 了解具有动态内存分配的二维数组

java - 模式错误纠正

C++ 将数组指针作为函数参数传递

image-processing - 我如何计算 scikit 图像或 mahotas 中的黑色空间数量?

python - 使用新的矩阵再次创建新的矩阵?

python-3.x - 检查 Python 列表元素是否在 Pandas 数据框行中

python - 在python中将指数形式的数字转换为十进制形式

python - 更改 rPython 中的 python 版本?

python - 将空格分隔的文件转换为 CSV