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
原文地址:https://www.cnblogs.com/aguncn/p/10803686.html
时间: 2024-11-09 04:44:30