马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
参考书籍:《机器学习实战》
import numpy as np
def loadDataSet(fileName, delim = '\t'):
fr = open(fileName)
stringArr = [line.strip().split(delim) for line in fr.readlines()]
datArr = [list(map(float,line)) for line in stringArr]
return np.mat(datArr)
def pca(dataMat, topNfeat = 9999999):
meanVals = np.mean(dataMat, axis = 0)
meanRemoved = dataMat - meanVals
covMat = np.cov(meanRemoved, rowvar = 0)#rowvar=0表示传入的一行表示一个样本
eigVals, eigVects = np.linalg.eig(np.mat(covMat))
eigValInd = np.argsort(eigVals)
eigValInd = eigValInd[:-(topNfeat+1):-1]
redEigVects = eigVects[:,eigValInd]
lowDDataMat = meanRemoved * redEigVects#所有点在新的一组基上的值,无方向
reconMat = (lowDDataMat * redEigVects.T) + meanVals#所有点在新的一组基上的投影,有方向的
return lowDDataMat, reconMat
if __name__ == '__main__':
dataMat = loadDataSet(r'C:\...\testSet.txt')
lowData, reconMat = pca(dataMat, 1)
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(dataMat[:, 0].flatten().A[0], dataMat[:, 1].flatten().A[0],
marker = '^', s = 90)
ax.scatter(reconMat[:, 0].flatten().A[0], reconMat[:, 1].flatten().A[0],
marker = 'o', s = 50, c = 'red')
|