13.2.2 在NUmpy中实现PCA
将数据转换成前N个主成分的伪代码大致如下:
去除平均值
计算协方差矩阵
计算协方差矩阵的特征值和特征向量
将特征值从大到小排列
保留最上面的N个特征向量
将数据转换到上述的N个特征向量构建的新空间中
在NumPy中实现PCA:
#coding:utf-8 from numpy import *
def loadDataSet(filename,delim = '\t'):
fr = open(filename)
stringArr = [line.strip().split(delim) for line in fr.readlines()]
datArr = [map(float,line) for line in stringArr]
return mat(datArr)
def pca(datamat,topNfeat = 999999):
meanVals = mean(datamat,axis = 0)
meanRemoved = datamat -meanVals
covMat = cov(meanRemoved,rowvar = 0)
eigVals,eigVect = linalg.eig(mat(covMat))
eigValInd = argsort(eigVals)
eigValInd = eigValInd[:-(topNfeat+1):-1]
redEigVects = eigvals(:,eigValInd)
lowDDataMat = meanRemoved*redEigVects
reconMat = (lowDDataMat*redEigVects.T)+meanVals
return lowDDataMat,reconMat
资料来源:《机器学习实战》