借助於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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。