Created
April 14, 2013 02:57
-
-
Save dz1984/5381215 to your computer and use it in GitHub Desktop.
使用R軟體執行主成份分析(Principal Components Analysis, PCA)減少資料維度。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dat = as.matrix(read.table("marks.dat",head=T)); | |
dim(dat); | |
plot(dat); | |
#Step 1: 求出共變異矩陣(covariance matrix) | |
covMat = cov(dat); | |
#Step 2 計算特徴值(eigenvalues)及特徴向量(eigenvectors)。 | |
eig = eigen(covMat); | |
eigVals = eig$values; | |
eigVects = eig$vectors; | |
#Step 3 決定K維度並依序取出k列之特徴向量。 | |
redEigVects = eigVects[,1]; | |
#Step 4 投射資料。 | |
lowDDataMat = dat %*% redEigVects; | |
dim(lowDDataMat); | |
plot(lowDDataMat%*%redEigVects); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment