1 #Create a DataSet, 5*7 the matrix2 defloadexdata ():3 return[[1,1,1, 0,0],4[2,2,2, 0,0],5[1,1,1, 0,0],6[5,5,5, 0,0],7[1,1,0,2,2],8[0,0,0,3,3],9[0,0,0,1,1]];Ten #SVD decomposition of matrices One fromNumPyImport*; A defsvd_ (): -Data=Loadexdata (); -u,sigma,vt=LINALG.SVD (Data); the Print('the results of the SVD decomposition Sigma are:', Sigma); - returnU,SIGMA,VT; - """ - Array ([9.72140007e+00, 5.29397912e+00, 6.84226362e-01, + 1.52344501e-15, 2.17780259e-16]) - you can see that the last two values are very small, so you can remove the last two values + """ A #approximate refactoring primitive matrices at defReconstructmat (): -u,sigma,vt=svd_ (); -Sig3=mat ([[Sigma[0],0,0],[0,sigma[1],0],[0,0,sigma[2]]]); -Reconmat=u[:,:3]*sig3*vt[:3,:]; - returnReconmat;
The result of the reconstructed matrix is as follows:
1>>>Reconmat2Matrix ([[[1.00000000e+00, 1.00000000e+00, 1.00000000e+00,3-1.51788304E-17, -1.02999206E-17],4[2.00000000e+00, 2.00000000e+00, 2.00000000e+00,51.73472348E-18, 1.12757026E-17],6[1.00000000e+00, 1.00000000e+00, 1.00000000e+00,77.61977287e-16, 7.66747776e-16],8[5.00000000e+00, 5.00000000e+00, 5.00000000e+00,96.59194921E-17, 9.02056208E-17],Ten[1.00000000e+00, 1.00000000e+00, -7.21644966e-16, One2.00000000e+00, 2.00000000e+00], A[1.66533454e-16, 1.30451205e-15, -8.88178420e-16, -3.00000000e+00, 3.00000000e+00], -[6.24500451e-17, 4.57966998e-16, -3.33066907e-16, the1.00000000e+00, 1.00000000e+00]])
The original matrix is:
1 >>> loadexdata ()2 [[1, 1, 1, 0, 0],
[0, 0, 0, 1, 1]]
Main question: How to find the number of singular values??
There are many heuristic strategies, two typical methods are: (1) preserving 90% of the energy information in the matrix, the sum of the squares of the singular values is the total energy, (2) preserving the first 2000 or 3,000 singular values in the matrix, when there are thousands of singular values;
In Python, we use SVD for matrix decomposition and reconstruction of primitive matrices.