本文介绍了如何转置3D矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个尺寸为(100, 33, 66)的3D矩阵x_test,我想将其尺寸更改为(100, 66, 33).

I have a 3D matrix x_test of size (100, 33, 66) and I want to change its dimensions to (100, 66, 33).

使用python3.5最有效的方法是什么?我正在寻找符合以下条件的东西:

What is the most efficient way to do this using python3.5? I look for something along those lines:

y = x_test.transpose()

推荐答案

您可以将所需的尺寸传递给函数 np.transpose (在您的情况下,请使用np.transpose(x_test, (0, 2, 1))).

You can pass the desired dimensions to the function np.transpose using in your case np.transpose(x_test, (0, 2, 1)).

例如,

import numpy as np

x_test = np.arange(30).reshape(3, 2, 5)

print(x_test)
print(x_test.shape)

这将打印

[[[ 0  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]]]
(3, 2, 5)

现在,您可以使用上方的命令转置矩阵

Now, you can transpose the matrix with the command from above

y = np.transpose(x_test, (0, 2, 1))
print(y)
print(y.shape)

这将给

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

 [[10 15]
  [11 16]
  [12 17]
  [13 18]
  [14 19]]

 [[20 25]
  [21 26]
  [22 27]
  [23 28]
  [24 29]]]
(3, 5, 2)

这篇关于如何转置3D矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:43