numpy.transpose を使う
Python
Published: 2019-10-05

やったこと

numpy.transpose を使ってみます。

確認環境

$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__version__
'1.16.4'

調査

>>> x = np.arange(6)
>>> x = x.reshape(3, 2)
>>> x
array([[0, 1],
       [2, 3],
       [4, 5]])
>>> x.transpose()
array([[0, 2, 4],
       [1, 3, 5]])
>>> x.transpose(1, 0)
array([[0, 2, 4],
       [1, 3, 5]])
>>> x.transpose(0, 1)
array([[0, 1],
       [2, 3],
       [4, 5]])

次元を入れ替えます。

参考