python transpose
1.陣列轉置和軸對換:陣列不僅有transpose方法,還有一個特殊的T屬性:
arr = np.arange(15).reshape(3,5)
arr
輸出:
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]])
arr.T
輸出:
array([[ 0, 5, 10], [ 1, 6, 11], [ 2, 7, 12], [ 3, 8, 13], [ 4, 9, 14]])2.進行矩陣計算時,經常需要用到該操作,比如利用np.dot計算矩陣內積XTX:
arr = np.random.randn(6,3)
arr
輸出:
array([[-0.83790345, -1.13304154, -0.42567014], [ 0.75742538, 1.24634357, -1.00116761], [ 0.54168995, -0.83717253, -1.11580943], [-0.13315165, 0.0331654 , 0.70605975], [-2.57536154, -0.68951735, 1.16959181], [-1.26193272, -1.24703158, 0.3183666 ]])
np.dot(arr.T,arr)
輸出:
array([[ 9.81189403, 4.78491411, -4.51395404], [ 4.78491411, 5.56963513, -1.01142215], [-4.51395404, -1.01142215, 4.39638499]])3.對於高維陣列,transpose需要得到一個由軸編號組成的元組才能對這些軸進行轉至(比較難理解):
arr = np.arange(16).reshape((2,2,4))
arr
輸出:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15]]])
輸出:
array([[[ 0, 1, 2, 3],
[ 8, 9, 10, 11]],
[[ 4, 5, 6, 7],
[12, 13, 14, 15]]])
提示:transpose(1,0,2)把原來的shape由(2,2,4)變成了(2,2,4),就是第一個軸和第二個軸上面的元素互換。
比如原來位置(0,1,0)上的元素為4,現在把它放到了(1,0,0)這個位置,就是下面那個位置由8變成了4,標出了紅色。
arr.transpose((1,0,2))
4.ndarray還有一個swapaxes方法,它接受一對軸變換:
arr
輸出:array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7]], [[ 8, 9, 10, 11], [12, 13, 14, 15]]])
arr.swapaxes(1,2)
輸出:array([[[ 0, 4], [ 1, 5], [ 2, 6], [ 3, 7]], [[ 8, 12], [ 9, 13], [10, 14], [11, 15]]])
5.通用函式sqrt、exp、maximum:
arr = np.arange(10)
arr
輸出:array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.sqrt(arr)
輸出:array([ 0. , 1. , 1.41421356, 1.73205081, 2. , 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])
np.exp(arr)
輸出:array([ 1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01, 5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03, 2.98095799e+03, 8.10308393e+03])
x = np.random.randn(8)
x
輸出:array([-0.24726724, 0.69709717, 0.9658356 , 1.89019088, -0.28912795, -0.09235779, 0.37690775, 0.9102138 ])
y = np.random.randn(8)
y
輸出:array([-0.05048326, -0.02207697, -0.59940773, -1.32029941, 0.30894105, -0.05807405, -1.5019804 , 0.12918562])
np.maximum(x,y) #元素級最大值
輸出:array([-0.05048326, 0.69709717, 0.9658356 , 1.89019088, 0.30894105, -0.05807405, 0.37690775, 0.9102138 ])
arr = np.random.randn(7)*5
arr
輸出:array([ -1.53462646, 6.15168006, 4.32588912, -0.05408803, -2.98953481, -10.83013834, 1.13673478])
np.modf(arr)
輸出:(array([-0.53462646, 0.15168006, 0.32588912, -0.05408803, -0.98953481, -0.83013834, 0.13673478]), array([ -1., 6., 4., -0., -2., -10., 1.]))
相關文章
- CUDA SDK例子分析(2):transpose
- [LeetCode] 867. Transpose MatrixLeetCode
- leetcode194. Transpose FileLeetCode
- ndarray的轉置(numpy.transpose()與A.T命令對比詳解)
- tf.transpose函式的用法講解(多維情況,看似複雜,其實也簡單)函式
- 【python】python安裝Python
- 【Python】Python使用redisPythonRedis
- Python 之父談 PythonPython
- 【Python】python練習Python
- 【Python】python 日期操作Python
- python ----python的安裝Python
- python:python的多程式Python
- 【Python】Python連線mysqlPythonMySql
- Python 3 能振興 PythonPython
- 【Python】Python安裝模組Python
- 【python】python APScheduler 框架Python框架
- python學習之初識pythonPython
- Python 序列化(Python IO)Python
- Python合集之Python函式Python函式
- 【Python】python類的繼承Python繼承
- 小白自學Python(一) -- Python教程Python
- 為Python加速 - python+memcachedPython
- Python 3 正在毀滅 PythonPython
- Python補充06 Python之道Python
- [python]python錯誤集錦Python
- Python list of class attributes - PythonPython
- 【python】Python 3 的新特性Python
- python--- 之The program 'python' can be found in the following packages: * python-minimal * python3PythonPackage
- python _Python
- PythonPython
- Python IDLE和Python的區別!Python入門教程Python
- Python補充02 Python小技巧Python
- Python 字串格式化(Python IO)Python字串格式化
- Python 檔案讀寫(Python IO)Python
- Python之將Python字串生成PDFPython字串
- python教程(一)·python環境搭建Python
- 小白自學Python(五)Python運算子Python
- 小白自學Python(六)Python字串(上)Python字串