借助于Numpy numpy.transpose(),我们可以使用以下命令执行简单的转置函数:numpy.transpose()
的方法另一方面,它可以转置二维数组,对一维数组没有影响。此方法转置二维numpy数组。
参数:
axes : [None, tuple of ints, or n ints] If anyone wants to pass the parameter then you can but it’s not all required. But if you want than remember only pass (0, 1) or (1, 0). Like we have array of shape (2, 3) to change it (3, 2) you should pass (1, 0) where 1 as 3 and 0 as 2.返回: ndarray
范例1:
在此示例中,我们可以看到仅用一行就可以很容易地转置数组。
# importing python module named numpy
import numpy as np
# making a 3x3 array
gfg = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# before transpose
print(gfg, end ='\n\n')
# after transpose
print(gfg.transpose())
输出:
[[1 2 3] [4 5 6] [7 8 9]] [[1 4 7] [2 5 8] [3 6 9]]
范例2:
在这个例子中,我们演示了元组在numpy.transpose()
。
# importing python module named numpy
import numpy as np
# making a 3x3 array
gfg = np.array([[1, 2],
[4, 5],
[7, 8]])
# before transpose
print(gfg, end ='\n\n')
# after transpose
print(gfg.transpose(1, 0))
输出:
[[1 2] [4 5] [7 8]] [[1 4 7] [2 5 8]]
相关用法
- Python Numpy numpy.ndarray.__add__()用法及代码示例
- Python Numpy numpy.ndarray.__sub__()用法及代码示例
- Python Numpy numpy.ndarray.__mul__()用法及代码示例
- Python Numpy numpy.ndarray.__truediv__()用法及代码示例
- Python Numpy numpy.ndarray.__xor__()用法及代码示例
- Python Numpy numpy.ndarray.__pos__()用法及代码示例
- Python Numpy numpy.ndarray.__and__()用法及代码示例
- Python Numpy numpy.ndarray.__neg__()用法及代码示例
- Python Numpy numpy.ndarray.__isub__()用法及代码示例
- Python Numpy numpy.ndarray.__rshift__()用法及代码示例
- Python Numpy numpy.ndarray.__iadd__()用法及代码示例
- Python Numpy numpy.ndarray.__pow__()用法及代码示例
- Python Numpy numpy.ndarray.__lshift__()用法及代码示例
- Python Numpy numpy.ndarray.__divmod__()用法及代码示例
- Python Numpy numpy.ndarray.__invert__()用法及代码示例
注:本文由纯净天空筛选整理自Jitender_1998大神的英文原创作品 Python | Numpy numpy.transpose()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。