資源簡介
我用java實(shí)現(xiàn)了PCA算法,其中調(diào)用了matlab的函數(shù)。

代碼片段和文件信息
package?com.pca;
import?java.math.*;
import?a2.ab;
import?Jama.Matrix;
public?class?Pca?{
//將原始數(shù)據(jù)標(biāo)準(zhǔn)化
public?double[][]?Standardlizer(double[][]?x){
int?n=x.length; //二維矩陣的行號(hào)
int?p=x[0].length; //二維矩陣的列號(hào)
double[]?average=new?double[p]; //每一列的平均值
double[][]?result=new?double[n][p]; //標(biāo)準(zhǔn)化后的向量
double[]?var=new?double[p];??????//方差
//取得每一列的平均值
for(int?k=0;k double?temp=0;
for(int?i=0;i temp+=x[i][k];
}
average[k]=temp/n;
}
//取得方差
for(int?k=0;k double?temp=0;
for(int?i=0;i temp+=(x[i][k]-average[k])*(x[i][k]-average[k]);
}
var[k]=temp/(n-1);
}
//獲得標(biāo)準(zhǔn)化的矩陣
for(int?i=0;i for(int?j=0;j result[i][j]=(double)?((x[i][j]-average[j])/Math.sqrt(var[j]));
}
}
return?result;
}
//計(jì)算樣本相關(guān)系數(shù)矩陣
public?double[][]?CoefficientOfAssociation(double[][]?x){
int?n=x.length; //二維矩陣的行號(hào)
int?p=x[0].length; //二維矩陣的列號(hào)
double[][]?result=new?double[p][p];//相關(guān)系數(shù)矩陣
for(int?i=0;i for(int?j=0;j double?temp=0;
for(int?k=0;k temp+=x[k][i]*x[k][j];
}
result[i][j]=temp/(n-1);
}
}
return?result;
}
//計(jì)算相關(guān)系數(shù)矩陣的特征值
public?double[][]?FlagValue(double[][]?x){
//定義一個(gè)矩陣
??????Matrix?A?=?new?Matrix(x);
??????//由特征值組成的對(duì)角矩陣
?????Matrix?B=A.eig().getD();
?????double[][]?result=B.getArray();
?????return?result;
}
//計(jì)算相關(guān)系數(shù)矩陣的特征向量
public?double[][]?FlagVector(double[][]?x){
//定義一個(gè)矩陣
??????Matrix?A?=?new?Matrix(x);
??????//由特征向量組成的對(duì)角矩陣
?????Matrix?B=A.eig().getV();
?????double[][]?result=B.getArray();
?????return?result;
}
//假設(shè)閾值是90%,選取最大的前幾個(gè)
public?int[]?SelectPrincipalComponent(double[][]?x){
int?n=x.length; //二維矩陣的行號(hào)列號(hào)
double[]?a?=?new?double[n];
int[]?result?=?new?int[n];
int?k=0;
double?temp?=?0;
int?m=0;
double?total=0;
for(int?i=0;i for(int?j=0;j if(i==j){
a[k]=x[i][j];
}
}
k++;
}
double[]?temp1=new?double[a.length];
System.arraycopy(a?0?temp1?0?a.length);
for(int?i=0;i temp=temp1[i];
for(int?j=i;j if(temp<=temp1[j]){
temp=temp1[j];
temp1[j]=temp1[i];
}
temp1[i]=temp;
}
}
for(int?i=0;i temp=a[i];
for(int?j=0;j if(a[j]>=temp){
temp=a[j];
k=j;
}
result[m]=k;
}
a[k]=-1000;
m++;
}
for(int?i=0;i total+=temp1[i];
}
int?sum=1;
temp=temp1[0];
for(int?i=0;i if(temp/total<=0.9){
temp+=temp1[i+1];
sum++;
}
}
int[]?end=new?int[sum];
System.arraycopy(result?0?end?0?sum);
return?end;
}
//取得主成分
public?double[][]?PrincipalComponent(double[][]?xint[]?y){
int?n=x.length;
double[][]?Result=new?double[n][y.length];
int?k=y.length-1;
for(int?i=0;i for(int?j=0
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2013-08-05?17:23??PCATest\
?????文件?????????455??2013-08-06?17:15??PCATest\.classpath
?????文件?????????383??2013-08-05?15:25??PCATest\.project
?????目錄???????????0??2013-08-05?15:25??PCATest\.settings\
?????文件?????????598??2013-08-05?15:26??PCATest\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2013-08-06?17:15??PCATest\bin\
?????目錄???????????0??2013-08-06?17:15??PCATest\bin\com\
?????目錄???????????0??2013-08-06?17:15??PCATest\bin\com\pca\
?????文件????????3242??2013-08-06?19:26??PCATest\bin\com\pca\Pca.class
?????文件????????4658??2013-08-07?19:48??PCATest\bin\com\pca\Test.class
?????目錄???????????0??2013-08-06?17:27??PCATest\externalJar\
?????文件???????52232??2013-08-06?17:14??PCATest\externalJar\a2.jar
?????文件???????35981??2013-01-28?16:42??PCATest\externalJar\Jama-1.0.3.jar
?????目錄???????????0??2013-08-05?15:26??PCATest\src\
?????目錄???????????0??2013-08-05?15:26??PCATest\src\com\
?????目錄???????????0??2013-08-05?16:31??PCATest\src\com\pca\
?????文件????????3271??2013-08-06?19:26??PCATest\src\com\pca\Pca.java
?????文件????????2638??2013-08-07?19:48??PCATest\src\com\pca\Test.java
評(píng)論
共有 條評(píng)論