Matrix operations
On the difference between matrix and array in NumPy: http://blog.csdn.net/vincentlipan/article/details/20717163
The difference between Matrix and array: Numpy matrices must be 2-dimensional, but Numpy arrays (ndarrays) can be multidimensional (1d,2d,3d ND). The matrix is a small branch of an array that is contained in an array. So the matrix has all the attributes of the array.
1. Basic operations
ImportNumPy as NPA= Np.array ([[ -1,2],[2,3]]) b= Np.array ([[3,4],[4,5]])Print '\ a:\n', aPrint '\ b:\n', b##转置Print '\ n a transpose:\n', A.T##共扼矩阵#print ' \ n a h:\n ', A.I##逆矩阵Print '\ n a inv:\n', NP.LINALG.INV (a)#Seeking Inverse##转置Print '\ n a transpose:\n', A.T#A + B, matrix additionPrint "\ a+b: \ n", A +b#A-B, matrix subtractionPrint "\ n: \ n", A-b#2x2 matrices, matrix multiplicationPrint "\ n A mul b:\n", A.dot (b.t)#2x3 Matrix, matrix point multiplicationPrint "\ n a dot B: \ n", A *b#2x3 matrix, matrix point dividePrint "\ A/b \ n:", A/Np.linalg.inv (b)#Find tracesPrint "\ n A Trace", Np.trace (a)#features, eigenvectorsEigval,eigvec =Np.linalg.eig (a)#Eigval = Np.linalg.eigvals (a) #直接求解特征值Print "\ n a eig value:\n", Eigval,Print'\ n a eig vector:\n', Eigvec
Result of Operation:
a:[[-1 2] [ 2 3]] b:[[3 4] [4 5]] a transpose:[[-1 2] [ 2 3]] a inv:[[-0.42857143 0.28571429] [ 0.28571429 0.14285714]] a transpose:[[-1 2] [ 2 3]] a+b: [[2 6] [6 8]] a-b: [[-4-2] [-2-2]] a mul b:[[5 6] [18 23]] a dot b: [[-3 8] [ 8 15]] a/b: [[0.2 0.5] [ 0.5-1. ]] A Trace2a Eig value:[-1.82842712 3.82842712] a Eig vector:[[-0.92387953-0.38268343] [ 0.38268343-0.92387953]]
2. Special matrices
ImportNumPy as NPA= Np.zeros ([4,5])#All ZeroPrint '\nall zero \ n', AA= Np.ones ([7,6])# all OnePrint '\nall one \ n', AA= Np.eye (4,7)#4x7 DiagonalPrint '\n4x7 diagonal \ n', AA= Np.diag (range (5))#5x5 DiagonalPrint '\n5x5 diagonal \ n', AA= Np.empty ((2,3))Print '\nempty \ n', AA= Np.arange (10, 30, 5)#Array ([ten, +,]), 1-dPrint '\ n Array ([ten, +,]), 1-d \ n', AA= Np.linspace (0, 2, 9)#9 numbers from 0 to 2Print '\n9 numbers from 0 to 2 \ n', AA= Np.random.random ((2,3))#Random matricsPrint '\nrandom matrics \ n', aImportNumPy as NPA= Np.zeros ([4,5])#All ZeroPrint '\nall zero \ n', AA= Np.ones ([7,6])# all OnePrint '\nall one \ n', AA= Np.eye (4,7)#4x7 DiagonalPrint '\n4x7 diagonal \ n', AA= Np.diag (range (5))#5x5 DiagonalPrint '\n5x5 diagonal \ n', AA= Np.empty ((2,3))Print '\nempty \ n', A?a= Np.arange (10, 30, 5)#Array ([ten, +,]), 1-dPrint '\ n Array ([ten, +,]), 1-d \ n', AA= Np.linspace (0, 2, 9)#9 numbers from 0 to 2Print '\n9 numbers from 0 to 2 \ n', AA= Np.random.random ((2,3))#Random matricsPrint '\nrandom matrics \ n', a
Result of Operation:
All Zero [[0. 0.0. 0.0.] [0.0. 0.0. 0.] [0. 0.0. 0.0.] [0.0. 0.0. 0.]]all one [[1.1. 1.1. 1.1.] [ 1.1. 1.1. 1.1.] [ 1.1. 1.1. 1.1.] [ 1.1. 1.1. 1.1.] [ 1.1. 1.1. 1.1.] [ 1.1. 1.1. 1.1.] [ 1.1. 1.1. 1.1.]] 4x7 Diagonal [[1. 0.0. 0.0. 0.0.] [0.1. 0.0. 0.0. 0.] [0. 0.1. 0.0. 0.0.] [0.0. 0.1. 0.0. 0.]]5x5 Diagonal [[0 0 0 0 0] [010 0 0] [0 020 0] [0 0 030] [0 0 0 04]]empty [[0.06012241 0.30847312 0.20174074] [ 0.37654373 0.71036135 0.15586512]] Array ([10, 15, 20, 25]), 1-D [10 15 20 25]9 numbers from0 to 2[0.0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ]random matrics [[0.44052293 0.42283564 0.44825331] [ 0.66735609 0.32664018 0.17015328]]
Python knowledge (6)--numpy doing matrix operations