首页 > 代码库 > pca_python

pca_python

#python
from
numpy import * def loadData(filename): data=[] for line in open(filename).readlines(): ft=line.strip().split("\t") th=map(float,ft) data.append(th) return mat(data) def pca(datamat,k): meanval=mean(datamat,0) center=datamat-meanval covmat=(center.T*center)/(shape(datamat)[0]-1) eigval,eigvec=linalg.eig(mat(covmat)) eigvalind=argsort(eigval) sortind=eigvalind[:-(k+1):-1] eigve=eigvec[:,sortind] lowmat=center*eigve return lowmat datamat=loadData("testSet.txt") lowmat=pca(datamat,1) print lowmat
%matlab
data=http://www.mamicode.com/vpa(load(‘testSet.txt‘),10);>

  

 

pca_python