定义是变量向量减去均值向量,然后乘以变量向量减去均值向量的转置再求均值。例如x是变量,μ是均值,协方差矩阵等于E[(x-μ)(x-μ)^t],物理意义是这样的,例如x=(x1,x2,...,xi)那么协方差矩阵的第m行n列的数为xm与xn的协方差,若m=n,则是xn的方差。如果x的元素之间是独立的,那么协方差矩阵只有对角线是有值,因为x独立的话对于m≠n的情况xm与xn的协方差为0。另外协方差矩阵是对称的。
'''
@author: Garvin
'''
from numpy import *
import matplotlib.pyplot as plt
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=9999999):
meanVals = mean(dataMat, axis=0)
meanRemoved = dataMat - meanVals #remove mean
covMat = cov(meanRemoved, rowvar=0)
eigVals,eigVects = linalg.eig(mat(covMat))
eigValInd = argsort(eigVals) #sort, sort goes smallest to largest
eigValInd = eigValInd[:-(topNfeat+1):-1] #cut off unwanted dimensions
redEigVects = eigVects[:,eigValInd] #reorganize eig vects largest to smallest
lowDDataMat = meanRemoved * redEigVects#transform data into new dimensions
reconMat = (lowDDataMat * redEigVects.T) + meanVals
return lowDDataMat, reconMat
def plotBestFit(dataSet1,dataSet2):
dataArr1 = array(dataSet1)
dataArr2 = array(dataSet2)
n = shape(dataArr1)[0]
n1=shape(dataArr2)[0]
xcord1 = []; ycord1 = []
xcord2 = []; ycord2 = []
xcord3=[];ycord3=[]
j=0
for i in range(n):
xcord1.append(dataArr1[i,0]); ycord1.append(dataArr1[i,1])
xcord2.append(dataArr2[i,0]); ycord2.append(dataArr2[i,1])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')
ax.scatter(xcord2, ycord2, s=30, c='green')
plt.xlabel('X1'); plt.ylabel('X2');
plt.show()
if __name__=='__main__':
mata=loadDataSet('/Users/hakuri/Desktop/testSet.txt')
a,b= pca(mata, 2)
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/