Original: http://www.cnblogs.com/leonwen/p/5158947.html
The algorithm is ported by MATLAB (see the previous blog post for details). But the final output is inconsistent with MATLAB, it is found that in the invocation of the internal function Eig to solve eigenvalues and eigenvectors, both eigenvalues are consistent, but the eigenvectors are different.
But, theoretically, it makes sense, because eigenvectors are inherently non-unique. The most puzzling thing is that even if the eigenvectors are inconsistent, why is the result of using PCA quite contrasting? It feels like Python's inaccurate line is a little bit bigger.
The code is as follows:
#-*-coding:utf-8-*-"""Created on Tue Jan 12 21:45:57 2016
@author: Text Core"""ImportOS fromPILImportImage fromNumPyImport* fromPylabImport*Path= R'D:\Projects\PythonProj\ImageProcessing\2012'Sst_dir=os.listdir (path) File_len=Len (sst_dir) sst_list= [] forIinchRange (File_len): Str= Path +'\\'+Sst_dir[i]#txt = open (str). Read ()sst_list.append (str) Immatrix= [] forIminchSst_list:text=loadtxt (IM) immatrix.append (text) Immatrix=Array (Immatrix)#Display OutputFigure () Gray () forIinchRange (File_len): Subplot (3,4,i + 1) pic= Immatrix[i].reshape (180,360) pic= Pic[::-1]#picshow = rot90 (pic,4)imshow (pic) Colorbar () show ()#Convert to sample populationX =Immatrix. T#get to this sizeM,n = X.shape[0:2]#obtain the average of each sampleMeanval = mean (X,axis =0)#Tempmean = Tile (Meanval, (64800,1))#Sample matrix de -CentralizedX = X-tile (Meanval, (64800,1))#Calculate covarianceS = dot (x.t,x)/(m-1)#calculate eigenvalues eg and eigenvector evEg,ev =Linalg.eig (S)#eg1,ev1 = Linalg.eigh (S) # There are sorting differences between the two algorithms, Eigh return results from small to large#calculate a new ingredientY =Dot (Immatrix. T,ev) figure () Gray () forIinchrange (N): Subplot (3,4,i + 1) out=Y[:,i]Outpic= Out.reshape (180,360) Outpic= Outpic[::-1]imshow (outpic)Colorbar () show ()
The results show:
Raw Data display
PCA main component Analysis result output
May be my own reason, is being found out, which is the guidance of the Great God of the guidance of the class to give me a little bit ~ First thanked ~o (∩_∩) o~
Python Remote sensing Data principal component analysis