NumPy transpose 的定义与计算过程

NumPy transpose 的定义与计算过程

NumPy documentation
https://numpy.org/doc/stable/index.html

numpy.transpose
https://numpy.org/doc/stable/reference/generated/numpy.transpose.html

numpy.ndarray.transpose
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.transpose.html

1. numpy.transpose

transpose(a, axes=None)

Reverse or permute the axes of an array; returns the modified array.
返回修改后的数组。

reverse [rɪˈvɜː(r)s]:n. 反面,背面,倒退,相反的情况 v. 颠倒,倒车,撤销,彻底转变 adj. 相反的,反面的,反向的,背面的
permute [pə'mjuːt]:v. 交换,取代,置换,排列
invert [ˌɪnˈvɜː(r)t]:v. 倒置,反转,倒转,翻转 n. 颠倒的事物,翻转
transposition [ˌtrænspə'zɪʃ(ə)n]:n. 换位,移项,词序改变,变调 (曲)

For an array a with two axes, transpose(a) gives the matrix transpose.
对于具有两个轴的数组 atranspose(a) 给出矩阵转置。行元素更改为列元素,列元素更改为行元素。

Parameters

a: array_like
    Input array.

axes: tuple or list of ints, optional
    If specified, it must be a tuple or list which contains a permutation of [0, 1, ..., N-1] where N is the number of axes of a.
 
    The i'th axis of the returned array will correspond to the axis numbered `axes[i]` of the input.

    If not specified, defaults to `range(a.ndim)[::-1]`, which reverses the order of the axes.
    如果未指定,则默认为 `range(a.ndim)[::-1]`,它反转轴的顺序。

Returns

p: ndarray
    `a` with its axes permuted. A view is returned whenever possible.

1.1 Notes

Use transpose(a, argsort(axes)) to invert the transposition of tensors when using the axes keyword argument.

2. numpy.ndarray.transpose

ndarray.transpose(*axes)

Returns a view of the array with axes transposed.
返回轴已转置的数组视图。

For a 1-D array this has no effect, as a transposed vector is simply the same vector. To convert a 1-D array into a 2D column vector, an additional dimension must be added. np.atleast2d(a).T achieves this, as does a[:, np.newaxis].
对于一维数组,这没有影响,因为转置向量只是同一向量。要将一维数组转换为二维列向量,必须添加额外的维度。np.atleast2d(a).Ta[:, np.newaxis] 一样实现了这一点。

For a 2-D array, this is a standard matrix transpose.
对于二维数组,这是一个标准的矩阵转置。

For an n-D array, if axes are given, their order indicates how the axes are permuted. If axes are not provided and a.shape = (i[0], i[1], ... i[n-2], i[n-1]), then a.transpose().shape = (i[n-1], i[n-2], ... i[1], i[0]).
对于 n 维数组,如果给定轴,则它们的顺序指示轴的排列方式。

Parameters

axes: None, tuple of ints, or n ints
    None or no argument: reverses the order of the axes. (反转轴的顺序。)
    tuple of ints: i in the j-th place in the tuple means a’s i-th axis becomes a.transpose()’s j-th axis.
    n ints: same as an n-tuple of the same ints (this form is intended simply as a “convenience” alternative to the tuple form)

Returns

out: ndarray
    View of `a`, with axes suitably permuted.

3. Examples

3.1 一维矩阵

Transposing a 1-D array returns an unchanged view of the original array.
转置一维数组会返回原始的数组。

(base) yongqiang@yongqiang:~$ python
Python 3.9.5 (default, Jun  4 2021, 12:28:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.array([1, 2, 3, 4])
>>> a
array([1, 2, 3, 4])
>>> a.shape
(4,)
>>>
>>> a.transpose()
array([1, 2, 3, 4])
>>>
>>> a.transpose(0)
array([1, 2, 3, 4])
>>>
>>> exit()
(base) yongqiang@yongqiang:~$

3.2 二维矩阵

矩阵的转置是将数据元素的行标和列标互换,即 A ( i , j ) = M ( j , i ) A(i, j) = M(j, i) A(i,j)=M(j,i) 。The superscript “T” means “transpose”.
在这里插入图片描述

This illustrates the rule ( A T ) T = A (A^{T})^{T} = A (AT)T=A.
在这里插入图片描述

A symmetric matrix is equal to its transpose: A T = A A^{T} = A AT=A. A symmetric matrix must be a square matrix. In terms of its elements, if a matrix is symmetric, then a i j = a j i a_{ij} = a_{ji} aij=aji.
在这里插入图片描述

The main diagonal of a matrix consists of those elements that lie on the diagonal that runs from top left to bottom right. If the matrix is A, then its main diagonal are the elements who’s row number and column number are equal, a j j a_{jj} ajj.
在这里插入图片描述

在这里插入图片描述

(base) yongqiang@yongqiang:~$ python
Python 3.9.5 (default, Jun  4 2021, 12:28:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a.shape
(2, 2)
>>>
>>> a.transpose(0, 1)
array([[1, 2],
       [3, 4]])
>>>
>>> a.transpose(1, 0)
array([[1, 3],
       [2, 4]])
>>>
>>> a.transpose((1, 0))
array([[1, 3],
       [2, 4]])
>>>
>>> a.transpose()
array([[1, 3],
       [2, 4]])
>>>
>>> exit()
(base) yongqiang@yongqiang:~$

在这里插入图片描述

A 图中第一个方括号 [] 为 0 轴,第二个方括号 [] 为 1 轴。

a = np.array([[1, 2], [3, 4]])

a[0][0] = 1
a[0][1] = 2
a[1][0] = 3
a[1][1] = 4

B 图中第一个方括号 [] 为 0 轴,第二个方括号 [] 为 1 轴。 a.transpose((1, 0)) 表示交换 0 轴和 1 轴。

>>> a.transpose((1, 0))
array([[1, 3],
       [2, 4]])
>>>
>>> a.transpose()
array([[1, 3],
       [2, 4]])
>>>

b[0][0] = 1
b[0][1] = 3
b[1][0] = 2
b[1][1] = 4

Transposing a tensor is the same as changing indices. Down becomes right and right becomes down. You might know this from matrices, where the transpose of a matrix means that the width and height (m and n) are swapped.
在这里插入图片描述

3.3 三维矩阵

(base) yongqiang@yongqiang:~$ python
Python 3.9.5 (default, Jun  4 2021, 12:28:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.arange(24)
>>> a
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
>>>
>>> a = a.reshape(2, 3, 4)
>>> a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> a.shape
(2, 3, 4)
>>>
a[0][0][0] = 0 ; a[0][0][1] = 1 ; a[0][0][2] = 2 ; a[0][0][3] = 3 ;
a[0][1][0] = 4 ; a[0][1][1] = 5 ; a[0][1][2] = 6 ; a[0][1][3] = 7 ;
a[0][2][0] = 8 ; a[0][2][1] = 9 ; a[0][2][2] = 10; a[0][2][3] = 11;

a[1][0][0] = 12; a[1][0][1] = 13; a[1][0][2] = 14; a[1][0][3] = 15;
a[1][1][0] = 16; a[1][1][1] = 17; a[1][1][2] = 18; a[1][1][3] = 19;
a[1][2][0] = 20; a[1][2][1] = 21; a[1][2][2] = 22; a[1][2][3] = 23;

在这里插入图片描述

  • b = a.transpose((0, 1, 2)) - 按照原坐标轴改变序列,ba 相同,保持不变
>>> a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> a.shape
(2, 3, 4)
>>>
>>> b = a.transpose((0, 1, 2))
>>> b
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> b.shape
(2, 3, 4)
>>>
b[0][0][0] = 0 ; b[0][0][1] = 1 ; b[0][0][2] = 2 ; b[0][0][3] = 3 ;
b[0][1][0] = 4 ; b[0][1][1] = 5 ; b[0][1][2] = 6 ; b[0][1][3] = 7 ;
b[0][2][0] = 8 ; b[0][2][1] = 9 ; b[0][2][2] = 10; b[0][2][3] = 11;

b[1][0][0] = 12; b[1][0][1] = 13; b[1][0][2] = 14; b[1][0][3] = 15;
b[1][1][0] = 16; b[1][1][1] = 17; b[1][1][2] = 18; b[1][1][3] = 19;
b[1][2][0] = 20; b[1][2][1] = 21; b[1][2][2] = 22; b[1][2][3] = 23;

在这里插入图片描述

  • c = a.transpose((1, 0, 2)) - 将 0 轴和 1 轴交换
>>> a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> a.shape
(2, 3, 4)
>>>
>>> c = a.transpose((1, 0, 2))
>>> c
array([[[ 0,  1,  2,  3],
        [12, 13, 14, 15]],

       [[ 4,  5,  6,  7],
        [16, 17, 18, 19]],

       [[ 8,  9, 10, 11],
        [20, 21, 22, 23]]])
>>> c.shape
(3, 2, 4)
>>>
a[0][0][0] = 0 ; a[0][0][1] = 1 ; a[0][0][2] = 2 ; a[0][0][3] = 3 ;
a[0][1][0] = 4 ; a[0][1][1] = 5 ; a[0][1][2] = 6 ; a[0][1][3] = 7 ;
a[0][2][0] = 8 ; a[0][2][1] = 9 ; a[0][2][2] = 10; a[0][2][3] = 11;

a[1][0][0] = 12; a[1][0][1] = 13; a[1][0][2] = 14; a[1][0][3] = 15;
a[1][1][0] = 16; a[1][1][1] = 17; a[1][1][2] = 18; a[1][1][3] = 19;
a[1][2][0] = 20; a[1][2][1] = 21; a[1][2][2] = 22; a[1][2][3] = 23;

c = a.transpose((1, 0, 2)) - 将 0 轴和 1 轴交换

c[0][0][0] = 0 ; c[0][0][1] = 1 ; c[0][0][2] = 2 ; c[0][0][3] = 3 ;
c[1][0][0] = 4 ; c[1][0][1] = 5 ; c[1][0][2] = 6 ; c[1][0][3] = 7 ;
c[2][0][0] = 8 ; c[2][0][1] = 9 ; c[2][0][2] = 10; c[2][0][3] = 11;

c[0][1][0] = 12; c[0][1][1] = 13; c[0][1][2] = 14; c[0][1][3] = 15;
c[1][1][0] = 16; c[1][1][1] = 17; c[1][1][2] = 18; c[1][1][3] = 19;
c[2][1][0] = 20; c[2][1][1] = 21; c[2][1][2] = 22; c[2][1][3] = 23;

在这里插入图片描述

  • d = a.transpose((0, 2, 1)) - 将 1 轴和 2 轴交换
>>> a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> a.shape
(2, 3, 4)
>>>
>>> d = a.transpose((0, 2, 1))
>>> d
array([[[ 0,  4,  8],
        [ 1,  5,  9],
        [ 2,  6, 10],
        [ 3,  7, 11]],

       [[12, 16, 20],
        [13, 17, 21],
        [14, 18, 22],
        [15, 19, 23]]])
>>> d.shape
(2, 4, 3)
>>>
a[0][0][0] = 0 ; a[0][0][1] = 1 ; a[0][0][2] = 2 ; a[0][0][3] = 3 ;
a[0][1][0] = 4 ; a[0][1][1] = 5 ; a[0][1][2] = 6 ; a[0][1][3] = 7 ;
a[0][2][0] = 8 ; a[0][2][1] = 9 ; a[0][2][2] = 10; a[0][2][3] = 11;

a[1][0][0] = 12; a[1][0][1] = 13; a[1][0][2] = 14; a[1][0][3] = 15;
a[1][1][0] = 16; a[1][1][1] = 17; a[1][1][2] = 18; a[1][1][3] = 19;
a[1][2][0] = 20; a[1][2][1] = 21; a[1][2][2] = 22; a[1][2][3] = 23;

d = a.transpose((0, 2, 1)) - 将 1 轴和 2 轴交换

d[0][0][0] = 0 ; d[0][1][0] = 1 ; d[0][2][0] = 2 ; d[0][3][0] = 3 ;
d[0][0][1] = 4 ; d[0][1][1] = 5 ; d[0][2][1] = 6 ; d[0][3][1] = 7 ;
d[0][0][2] = 8 ; d[0][1][2] = 9 ; d[0][2][2] = 10; d[0][3][2] = 11;

d[1][0][0] = 12; d[1][1][0] = 13; d[1][2][0] = 14; d[1][3][0] = 15;
d[1][0][1] = 16; d[1][1][1] = 17; d[1][2][1] = 18; d[1][3][1] = 19;
d[1][0][2] = 20; d[1][1][2] = 21; d[1][2][2] = 22; d[1][3][2] = 23;

在这里插入图片描述

  • e = a.transpose((2, 1, 0)) - 将 0 轴和 2 轴交换
>>> a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> a.shape
(2, 3, 4)
>>>
>>> e = a.transpose((2, 1, 0))
>>> e
array([[[ 0, 12],
        [ 4, 16],
        [ 8, 20]],

       [[ 1, 13],
        [ 5, 17],
        [ 9, 21]],

       [[ 2, 14],
        [ 6, 18],
        [10, 22]],

       [[ 3, 15],
        [ 7, 19],
        [11, 23]]])
>>> e.shape
(4, 3, 2)
>>>
a[0][0][0] = 0 ; a[0][0][1] = 1 ; a[0][0][2] = 2 ; a[0][0][3] = 3 ;
a[0][1][0] = 4 ; a[0][1][1] = 5 ; a[0][1][2] = 6 ; a[0][1][3] = 7 ;
a[0][2][0] = 8 ; a[0][2][1] = 9 ; a[0][2][2] = 10; a[0][2][3] = 11;

a[1][0][0] = 12; a[1][0][1] = 13; a[1][0][2] = 14; a[1][0][3] = 15;
a[1][1][0] = 16; a[1][1][1] = 17; a[1][1][2] = 18; a[1][1][3] = 19;
a[1][2][0] = 20; a[1][2][1] = 21; a[1][2][2] = 22; a[1][2][3] = 23;

e = a.transpose((2, 1, 0)) - 将 0 轴和 2 轴交换

e[0][0][0] = 0 ; e[1][0][0] = 1 ; e[2][0][0] = 2 ; e[3][0][0] = 3 ;
e[0][1][0] = 4 ; e[1][1][0] = 5 ; e[2][1][0] = 6 ; e[3][1][0] = 7 ;
e[0][2][0] = 8 ; e[1][2][0] = 9 ; e[2][2][0] = 10; e[3][2][0] = 11;

e[0][0][1] = 12; e[1][0][1] = 13; e[2][0][1] = 14; e[3][0][1] = 15;
e[0][1][1] = 16; e[1][1][1] = 17; e[2][1][1] = 18; e[3][1][1] = 19;
e[0][2][1] = 20; e[1][2][1] = 21; e[2][2][1] = 22; e[3][2][1] = 23;

在这里插入图片描述

  • f = a.transpose((1, 2, 0)) - 将 (0, 1, 2) 轴交换为 (1, 2, 0)
>>> a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> a.shape
(2, 3, 4)
>>>
>>> f = a.transpose((1, 2, 0))
>>> f
array([[[ 0, 12],
        [ 1, 13],
        [ 2, 14],
        [ 3, 15]],

       [[ 4, 16],
        [ 5, 17],
        [ 6, 18],
        [ 7, 19]],

       [[ 8, 20],
        [ 9, 21],
        [10, 22],
        [11, 23]]])
>>> f.shape
(3, 4, 2)
>>>
a[0][0][0] = 0 ; a[0][0][1] = 1 ; a[0][0][2] = 2 ; a[0][0][3] = 3 ;
a[0][1][0] = 4 ; a[0][1][1] = 5 ; a[0][1][2] = 6 ; a[0][1][3] = 7 ;
a[0][2][0] = 8 ; a[0][2][1] = 9 ; a[0][2][2] = 10; a[0][2][3] = 11;

a[1][0][0] = 12; a[1][0][1] = 13; a[1][0][2] = 14; a[1][0][3] = 15;
a[1][1][0] = 16; a[1][1][1] = 17; a[1][1][2] = 18; a[1][1][3] = 19;
a[1][2][0] = 20; a[1][2][1] = 21; a[1][2][2] = 22; a[1][2][3] = 23;

f = a.transpose((1, 2, 0)) - 将 (0, 1, 2) 轴交换为 (1, 2, 0) 轴

f[0][0][0] = 0 ; f[0][1][0] = 1 ; f[0][2][0] = 2 ; f[0][3][0] = 3 ;
f[1][0][0] = 4 ; f[1][1][0] = 5 ; f[1][2][0] = 6 ; f[1][3][0] = 7 ;
f[2][0][0] = 8 ; f[2][1][0] = 9 ; f[2][2][0] = 10; f[2][3][0] = 11;

f[0][0][1] = 12; f[0][1][1] = 13; f[0][2][1] = 14; f[0][3][1] = 15;
f[1][0][1] = 16; f[1][1][1] = 17; f[1][2][1] = 18; f[1][3][1] = 19;
f[2][0][1] = 20; f[2][1][1] = 21; f[2][2][1] = 22; f[2][3][1] = 23;

在这里插入图片描述

  • g = a.transpose((2, 0, 1)) - 将 (0, 1, 2) 轴交换为 (2, 0, 1)
>>> a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> a.shape
(2, 3, 4)
>>>
>>> g = a.transpose((2, 0, 1))
>>> g
array([[[ 0,  4,  8],
        [12, 16, 20]],

       [[ 1,  5,  9],
        [13, 17, 21]],

       [[ 2,  6, 10],
        [14, 18, 22]],

       [[ 3,  7, 11],
        [15, 19, 23]]])
>>> g.shape
(4, 2, 3)
>>>
a[0][0][0] = 0 ; a[0][0][1] = 1 ; a[0][0][2] = 2 ; a[0][0][3] = 3 ;
a[0][1][0] = 4 ; a[0][1][1] = 5 ; a[0][1][2] = 6 ; a[0][1][3] = 7 ;
a[0][2][0] = 8 ; a[0][2][1] = 9 ; a[0][2][2] = 10; a[0][2][3] = 11;

a[1][0][0] = 12; a[1][0][1] = 13; a[1][0][2] = 14; a[1][0][3] = 15;
a[1][1][0] = 16; a[1][1][1] = 17; a[1][1][2] = 18; a[1][1][3] = 19;
a[1][2][0] = 20; a[1][2][1] = 21; a[1][2][2] = 22; a[1][2][3] = 23;

g = a.transpose((2, 0, 1)) - 将 (0, 1, 2) 轴交换为 (2, 0, 1) 轴

g[0][0][0] = 0 ; g[1][0][0] = 1 ; g[2][0][0] = 2 ; g[3][0][0] = 3 ;
g[0][0][1] = 4 ; g[1][0][1] = 5 ; g[2][0][1] = 6 ; g[3][0][1] = 7 ;
g[0][0][2] = 8 ; g[1][0][2] = 9 ; g[2][0][2] = 10; g[3][0][2] = 11;

g[0][1][0] = 12; g[1][1][0] = 13; g[2][1][0] = 14; g[3][1][0] = 15;
g[0][1][1] = 16; g[1][1][1] = 17; g[2][1][1] = 18; g[3][1][1] = 19;
g[0][1][2] = 20; g[1][1][2] = 21; g[2][1][2] = 22; g[3][1][2] = 23;

在这里插入图片描述

References

https://yongqiang.blog.csdn.net/
https://chortle.ccsu.edu/vectorlessons/vmch13/vmch13_14.html

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/128245194
今日推荐