use of org.ejml.data.DMatrixRMaj in project clusterMaker2 by RBVI.
the class PrincipalComponentAnalysis method eigenToSampleSpace.
/**
* Converts a vector from eigen space into sample space.
*
* @param eigenData Eigen space data.
* @return Sample space projection.
*/
public double[] eigenToSampleSpace(double[] eigenData) {
if (eigenData.length != numComponents)
throw new IllegalArgumentException("Unexpected sample length");
DMatrixRMaj s = new DMatrixRMaj(A.getNumCols(), 1);
DMatrixRMaj r = DMatrixRMaj.wrap(numComponents, 1, eigenData);
CommonOps_DDRM.multTransA(V_t, r, s);
DMatrixRMaj mean = DMatrixRMaj.wrap(A.getNumCols(), 1, this.mean);
CommonOps_DDRM.add(s, mean, s);
return s.data;
}
use of org.ejml.data.DMatrixRMaj in project clusterMaker2 by RBVI.
the class PrincipalComponentAnalysis method computeBasis.
/**
* Computes a basis (the principal components) from the most dominant eigenvectors.
*
* @param numComponents Number of vectors it will use to describe the data. Typically much
* smaller than the number of elements in the input vector.
*/
public void computeBasis(int numComponents) {
if (numComponents > A.getNumCols())
throw new IllegalArgumentException("More components requested that the data's length.");
if (sampleIndex != A.getNumRows())
throw new IllegalArgumentException("Not all the data has been added");
if (numComponents > sampleIndex)
throw new IllegalArgumentException("More data needed to compute the desired number of components");
this.numComponents = numComponents;
// compute the mean of all the samples
for (int i = 0; i < A.getNumRows(); i++) {
for (int j = 0; j < mean.length; j++) {
mean[j] += A.get(i, j);
}
}
for (int j = 0; j < mean.length; j++) {
mean[j] /= A.getNumRows();
}
// subtract the mean from the original data
for (int i = 0; i < A.getNumRows(); i++) {
for (int j = 0; j < mean.length; j++) {
A.set(i, j, A.get(i, j) - mean[j]);
}
}
// Compute SVD and save time by not computing U
SingularValueDecomposition_F64<DMatrixRMaj> svd = DecompositionFactory_DDRM.svd(A.numRows, A.numCols, false, true, false);
if (!svd.decompose(A))
throw new RuntimeException("SVD failed");
V_t = svd.getV(null, true);
DMatrixRMaj W = svd.getW(null);
// Singular values are in an arbitrary order initially
SingularOps_DDRM.descendingOrder(null, false, W, V_t, true);
// strip off unneeded components and find the basis
V_t.reshape(numComponents, mean.length, true);
}
use of org.ejml.data.DMatrixRMaj in project clusterMaker2 by RBVI.
the class PrincipalComponentAnalysis method sampleToEigenSpace.
/**
* Converts a vector from sample space into eigen space.
*
* @param sampleData Sample space data.
* @return Eigen space projection.
*/
public double[] sampleToEigenSpace(double[] sampleData, int row) {
if (sampleData.length != A.getNumCols())
throw new IllegalArgumentException("Unexpected sample length");
DMatrixRMaj mean = DMatrixRMaj.wrap(A.getNumCols(), 1, this.mean);
DMatrixRMaj s = new DMatrixRMaj(A.getNumCols(), 1, true, sampleData);
DMatrixRMaj r = new DMatrixRMaj(numComponents, 1);
CommonOps_DDRM.subtract(s, mean, s);
// FastTSne.writeMatrix("s-"+row,s);
CommonOps_DDRM.mult(V_t, s, r);
return r.data;
}
use of org.ejml.data.DMatrixRMaj in project narchy by automenta.
the class PrincipalComponentAnalysis method sampleToEigenSpace.
/**
* Converts a vector from sample space into eigen space.
*
* @param sampleData Sample space data.
* @return Eigen space projection.
*/
private double[] sampleToEigenSpace(double[] sampleData) {
if (sampleData.length != A.getNumCols())
throw new IllegalArgumentException("Unexpected sample length");
DMatrixRMaj mean = DMatrixRMaj.wrap(A.getNumCols(), 1, this.mean);
DMatrixRMaj s = new DMatrixRMaj(A.getNumCols(), 1, true, sampleData);
DMatrixRMaj r = new DMatrixRMaj(numComponents, 1);
subtract(s, mean, s);
mult(V_t, s, r);
return r.data;
}
use of org.ejml.data.DMatrixRMaj in project narchy by automenta.
the class PrincipalComponentAnalysis method computeBasis.
/**
* Computes a basis (the principal components) from the most dominant eigenvectors.
*
* @param numComponents Number of vectors it will use to describe the data. Typically much
* smaller than the number of elements in the input vector.
*/
private void computeBasis(int numComponents) {
if (numComponents > A.getNumCols())
throw new IllegalArgumentException("More components requested that the data's length.");
int rows = A.getNumRows();
if (sampleIndex != rows)
throw new IllegalArgumentException("Not all the data has been added");
if (numComponents > sampleIndex)
throw new IllegalArgumentException("More data needed to compute the desired number of components");
this.numComponents = numComponents;
// compute the mean of all the samples
for (int i = 0; i < rows; i++) for (int j = 0; j < mean.length; j++) mean[j] += A.get(i, j);
for (int j = 0; j < mean.length; j++) mean[j] /= rows;
// subtract the mean from the original data
for (int i = 0; i < rows; i++) for (int j = 0; j < mean.length; j++) A.add(i, j, -mean[j]);
// Compute SVD and save time by not computing U
SingularValueDecomposition<DMatrixRMaj> svd = DecompositionFactory_DDRM.svd(A.numRows, A.numCols, false, true, false);
if (!svd.decompose(A))
throw new RuntimeException("SVD failed");
V_t = svd.getV(null, true);
DMatrixRMaj W = svd.getW(null);
// Singular values are in an arbitrary order initially
descendingOrder(null, false, W, V_t, true);
// strip off unneeded components and find the basis
V_t.reshape(numComponents, mean.length, true);
}
Aggregations