use of Jama.SingularValueDecomposition in project h2o-2 by h2oai.
the class PCA method buildModel.
public PCAModel buildModel(DataInfo dinfo, GramTask tsk) {
logStart();
// X'X/n where n = num rows
Matrix myGram = new Matrix(tsk._gram.getXX());
SingularValueDecomposition mySVD = myGram.svd();
// Extract eigenvalues and eigenvectors
// Note: Singular values ordered in weakly descending order by algorithm
double[] Sval = mySVD.getSingularValues();
// rows = features, cols = principal components
double[][] eigVec = mySVD.getV().getArray();
assert Sval.length == eigVec.length;
// DKV.put(EigenvectorMatrix.makeKey(input("source"), destination_key), new EigenvectorMatrix(eigVec));
// Compute standard deviation
double[] sdev = new double[Sval.length];
double totVar = 0;
double dfcorr = dinfo._adaptedFrame.numRows() / (dinfo._adaptedFrame.numRows() - 1.0);
for (int i = 0; i < Sval.length; i++) {
// if(standardize)
// Correct since degrees of freedom = n-1
Sval[i] = dfcorr * Sval[i];
sdev[i] = Math.sqrt(Sval[i]);
totVar += Sval[i];
}
// Proportion of total variance
double[] propVar = new double[Sval.length];
// Cumulative proportion of total variance
double[] cumVar = new double[Sval.length];
for (int i = 0; i < Sval.length; i++) {
propVar[i] = Sval[i] / totVar;
cumVar[i] = i == 0 ? propVar[0] : cumVar[i - 1] + propVar[i];
}
Key dataKey = input("source") == null ? null : Key.make(input("source"));
int ncomp = Math.min(getNumPC(sdev, tolerance), max_pc);
return new PCAModel(this, destination_key, dataKey, dinfo, tsk, sdev, propVar, cumVar, eigVec, mySVD.rank(), ncomp);
}
Aggregations