Numpy ndarray.transpose()函数| Python
numpy.ndarray.transpose()
函数返回轴转置的数组视图。
对于一维数组,这没有影响,因为转置向量只是相同的向量。对于二维数组,这是标准矩阵转置。对于 nD 数组,如果给定轴,则它们的顺序指示轴的排列方式。如果未提供轴且 arr.shape = (i[0], i[1], ... i[n-2], i[n-1]),则 arr.transpose().shape = (i[n -1], i[n-2], … i[1], i[0])。
Syntax : numpy.ndarray.transpose(*axes)
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 arr’s i-th axis becomes arr.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)
Return : [ndarray] View of arr, with axes suitably permuted.
代码#1:
# Python program explaining
# numpy.ndarray.transpose() function
# importing numpy as geek
import numpy as geek
arr = geek.array([[5, 6], [7, 8]])
gfg = arr.transpose()
print( gfg )
输出 :
[[5 7]
[6 8]]
代码#2:
# Python program explaining
# numpy.ndarray.transpose() function
# importing numpy as geek
import numpy as geek
arr = geek.array([[5, 6], [7, 8]])
gfg = arr.transpose((1, 0))
print( gfg )
输出 :
[[5 7]
[6 8]]