Python Numpy线性代数操作
Python Numpy线性代数函数操作
1、使用dot计算矩阵乘法
import numpy as np from numpy import ones from __builtin__ import int print 'Matrix multiplication' mat23 = np.arange(1,7).reshape(2,3) mat32 = np.arange(-1,-7,-1).reshape(3,2) dotMatrix = np.dot(mat32,mat23)
print dotMatrix print np.dot(mat23,ones([3,3]))
2、获得方阵的对角线元素,计算方阵的迹,计算方阵的行列式
print "Get diag,trace and determinant" print dotMatrix dia = np.diag(dotMatrix) print dia print np.trace(dotMatrix) print np.linalg.det(dotMatrix)
3、矩阵的转置,生成单位矩阵
print "Transpose" print mat23 print mat23.T print mat23.transpose() print "eye 5" print np.eye(5,dtype=int)