Skip to content

Instantly share code, notes, and snippets.

@dz1984
Created April 14, 2013 02:57
Show Gist options
  • Save dz1984/5381215 to your computer and use it in GitHub Desktop.
Save dz1984/5381215 to your computer and use it in GitHub Desktop.
使用R軟體執行主成份分析(Principal Components Analysis, PCA)減少資料維度。
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