python---Numpy模塊中線性代數運算,統計和數學函數
阿新 • • 發佈:2019-05-03
逆矩陣 python 0.11 進入 2.6 pandas 1.0 行列式 arr
NUMPY告一段落,接下來,進入pandas.
import numpy as np # Numpy 線性代數運算 # Numpy 統計和數學函數 print(‘==========計算矩陣與其轉置矩陣的內積。===========‘) X = np.arange(15).reshape((3, 5)) print(X) print(X.T) print(np.dot(X.T, X)) print(‘==========計算兩個一維數組的外積。===========‘) arr1 = np.array([12, 43, 10], float) arr2 = np.array([21, 42, 14], float)print(np.outer(arr1, arr2)) print(‘==========計算兩個一維數組的內積。===========‘) print(np.inner(arr1, arr2)) print(‘==========計算兩個一維數組的向量積。===========‘) print(np.cross(arr1, arr2)) matrix = np.array([[74, 22, 10], [92, 31, 17], [21, 22, 12]], float) print(matrix) print(‘==========使用linalg子模塊det計算矩陣的行列式值。===========‘) print(np.linalg.det(matrix)) print(‘==========使用linalg子模塊inv生成逆矩陣。===========‘) inv_matrix = np.linalg.inv(matrix) print(inv_matrix) print(‘==========計算矩陣和逆矩陣的內積。===========‘) print(np.dot(inv_matrix, matrix)) print(‘==========使用linalg的eig計算矩陣的特征值和特征向量。===========‘) vals, vecs = np.linalg.eig(matrix)print(vals) print(vecs) arr = np.random.rand(8, 4) print(‘==========計算均值。===========‘) print(arr.mean()) print(np.mean(arr)) print(‘==========求和。===========‘) print(arr.sum())
PS C:\test> & C:/Python37/python.exe c:/test/ml.py ==========計算矩陣與其轉置矩陣的內積。=========== [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] [[ 0 5 10] [ 1 6 11] [ 2 7 12] [ 3 8 13] [ 4 9 14]] [[125 140 155 170 185] [140 158 176 194 212] [155 176 197 218 239] [170 194 218 242 266] [185 212 239 266 293]] ==========計算兩個一維數組的外積。=========== [[ 252. 504. 168.] [ 903. 1806. 602.] [ 210. 420. 140.]] ==========計算兩個一維數組的內積。=========== 2198.0 ==========計算兩個一維數組的向量積。=========== [ 182. 42. -399.] [[74. 22. 10.] [92. 31. 17.] [21. 22. 12.]] ==========使用linalg子模塊det計算矩陣的行列式值。=========== -2852.000000000003 ==========使用linalg子模塊inv生成逆矩陣。=========== [[ 0.00070126 0.01542777 -0.02244039] [ 0.26192146 -0.23772791 0.11851332] [-0.48141655 0.4088359 -0.09467041]] ==========計算矩陣和逆矩陣的內積。=========== [[ 1.00000000e+00 1.66533454e-16 5.55111512e-17] [-2.66453526e-15 1.00000000e+00 2.22044605e-16] [-2.44249065e-15 4.44089210e-16 1.00000000e+00]] ==========使用linalg的eig計算矩陣的特征值和特征向量。=========== [107.99587441 11.33411853 -2.32999294] [[-0.57891525 -0.21517959 0.06319955] [-0.75804695 0.17632618 -0.58635713] [-0.30036971 0.96052424 0.80758352]] ==========計算均值。=========== 0.4850533513332038 0.4850533513332038 ==========求和。=========== 15.521707242662522
python---Numpy模塊中線性代數運算,統計和數學函數