Module Name: pca.py
PCA principle and the principle of tightening techniques to be mended ...
#-*-coding:utf-8-*-" "Created on March 2, 2015 principal component analysis of p-14 image of @author:ayumi Phoenixch01" " fromPILImportImageImportNumPydefPCA (X):"""principal component analysis: input; matrix x each behavior a training data return: Projection matrix (sorted by dimension importance), variance, and mean"""X=Numpy.asarray (X) N_data,dim= X.shape#Axis_0, Axis_1mean_x= X.mean (axis=0) X-=mean_xifN_data <Dim:#the number of dimensions is greater than the number of samples, using compact techniquesR_sigma = Numpy.dot (x,x.t)#m x MEign_values, eign_vectors = Numpy.linalg.eigh (R_sigma)#returns the eigenvalues and eigenvectors of an H-matrix or symmetric array (ascending order)TMP = Numpy.dot (x.t,eign_vectors)#(n2,m) x (m,m)V = Tmp[::-1]#Matrix v each line of vectors is orthogonalS =numpy.sqrt (eign_values) forIinchXrange (v.shape[1]): V[:,i]/=SElse: #PCA-SVDU,s,v =NUMPY.LINALG.SVD (X) V= V[:n_data]#It is only reasonable to return the former N_data data #returns the projection vector matrix, the eigenvalue, the mean value returnV, S, mean_xif __name__=="__main__": fromPILImportImageImportNumPyImportPylabImportImtools as IMT path= R"E:\dataset lib\pcv_data\fontimages\a_thumbs"imlist=imt.get_imlist (path) IM=Numpy.array (Image.open (imlist[0)) M,n= Im.shape[0:2] N_im=Len (imlist) Im_matrix=Numpy.array ([Numpy.array (Image.open (Each_im)). Flatten () forEach_iminchImlist],'F') V,s,im_mean=PCA (Im_matrix)#show mean image with top seven feature graphspylab.figure () Pylab.gray () Pylab.subplot (2,4,1) Pylab.imshow (Im_mean.reshape (m,n) ) forIinchXrange (7): Pylab.subplot (2,4,i+2) Pylab.imshow (V[i].reshape (m,n) )#new projection as a fresh sampleK = 10PrintIm_matrix.shape,v.shape#K-eigenvectors before vy = Numpy.dot (im_matrix,v[0:k,:]. T#(M,N2) * ((K,N2). T) = [M,k] PrintY.shape#Show Restore ImageIm_matrix_tidle = Numpy.dot (y,v[0:k,:]) +Im_mean pylab.figure () Pylab.gray () forIinchXrange (8): Pylab.subplot (2,4,i+1) Pylab.imshow (Im_matrix_tidle[i].reshape (m,n)) pylab.show ()#saving mean and principal component data ImportPickle F= Open ('FONT_PCA_MODELS.PKL','WB') Pickle.dump (Im_mean, F) pickle.dump (V, F) f.close ()#load mean and principal components data ImportPickle F= Open ('FONT_PCA_MODELS.PKL','RB') Im_mean= F.load (f)#the order in which the objects are loaded must be the same as the Save orderV =f.load (f) f.close ()
Mean picture with the first 7 eigenvectors:
Top 7 images Reduced-dimensionality reduction images
PCV Study note-CH1 principal component Analysis implementation