use of Jama.Matrix in project knime-core by knime.
the class PCAReverseNodeModel method execute.
/**
* Performs the PCA.
*
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
final PCAModelPortObject model = (PCAModelPortObject) inData[MODEL_INPORT];
final Matrix eigenvectors = EigenValue.getSortedEigenVectors(model.getEigenVectors(), model.getEigenvalues(), m_inputColumnIndices.length);
if (m_failOnMissingValues.getBooleanValue()) {
for (final DataRow row : (DataTable) inData[DATA_INPORT]) {
for (int i = 0; i < m_inputColumnIndices.length; i++) {
if (row.getCell(m_inputColumnIndices[i]).isMissing()) {
throw new IllegalArgumentException("data table contains missing values");
}
}
}
}
final String[] origColumnNames = ((PCAModelPortObjectSpec) ((PCAModelPortObject) inData[MODEL_INPORT]).getSpec()).getColumnNames();
final DataColumnSpec[] specs = createAddTableSpec((DataTableSpec) inData[DATA_INPORT].getSpec(), origColumnNames);
final CellFactory fac = new CellFactory() {
@Override
public DataCell[] getCells(final DataRow row) {
return convertInputRow(eigenvectors, row, model.getCenter(), m_inputColumnIndices, origColumnNames.length);
}
@Override
public DataColumnSpec[] getColumnSpecs() {
return specs;
}
@Override
public void setProgress(final int curRowNr, final int rowCount, final RowKey lastKey, final ExecutionMonitor texec) {
texec.setProgress((double) curRowNr / rowCount);
}
};
final ColumnRearranger cr = new ColumnRearranger((DataTableSpec) inData[DATA_INPORT].getSpec());
cr.append(fac);
if (m_removePCACols.getBooleanValue()) {
cr.remove(m_inputColumnIndices);
}
final BufferedDataTable result = exec.createColumnRearrangeTable((BufferedDataTable) inData[DATA_INPORT], cr, exec);
final PortObject[] out = { result };
return out;
}
use of Jama.Matrix in project knime-core by knime.
the class PCAApplyNodeModel method execute.
/**
* Performs the PCA.
*
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
final PCAModelPortObject model = (PCAModelPortObject) inData[MODEL_INPORT];
final int dimensions = m_dimSelection.getNeededDimensions();
if (dimensions == -1) {
throw new IllegalArgumentException("Number of dimensions not correct configured");
}
if (m_failOnMissingValues.getBooleanValue()) {
for (final DataRow row : (DataTable) inData[DATA_INPORT]) {
for (int i = 0; i < m_inputColumnIndices.length; i++) {
if (row.getCell(m_inputColumnIndices[i]).isMissing()) {
throw new IllegalArgumentException("data table contains missing values");
}
}
}
}
final Matrix eigenvectors = EigenValue.getSortedEigenVectors(model.getEigenVectors(), model.getEigenvalues(), dimensions);
final DataColumnSpec[] specs = PCANodeModel.createAddTableSpec((DataTableSpec) inData[DATA_INPORT].getSpec(), dimensions);
final int dim = dimensions;
final CellFactory fac = new CellFactory() {
@Override
public DataCell[] getCells(final DataRow row) {
return PCANodeModel.convertInputRow(eigenvectors, row, model.getCenter(), m_inputColumnIndices, dim, m_failOnMissingValues.getBooleanValue());
}
@Override
public DataColumnSpec[] getColumnSpecs() {
return specs;
}
@Override
public void setProgress(final int curRowNr, final int rowCount, final RowKey lastKey, final ExecutionMonitor texec) {
texec.setProgress((double) curRowNr / rowCount, "converting input row " + curRowNr + " of " + rowCount);
}
};
final ColumnRearranger cr = new ColumnRearranger((DataTableSpec) inData[DATA_INPORT].getSpec());
cr.append(fac);
if (m_removeOriginalCols.getBooleanValue()) {
cr.remove(m_inputColumnNames);
}
final BufferedDataTable result = exec.createColumnRearrangeTable((BufferedDataTable) inData[DATA_INPORT], cr, exec);
final PortObject[] out = { result };
return out;
}
use of Jama.Matrix in project h2o-2 by h2oai.
the class Gram method cholesky.
/**
* Compute the cholesky decomposition.
*
* In case our gram starts with diagonal submatrix of dimension N, we exploit this fact to reduce the complexity of the problem.
* We use the standard decomposition of the cholesky factorization into submatrices.
*
* We split the Gram into 3 regions (4 but we only consider lower diagonal, sparse means diagonal region in this context):
* diagonal
* diagonal*dense
* dense*dense
* Then we can solve the cholesky in 3 steps:
* 1. We solve the diagnonal part right away (just do the sqrt of the elements).
* 2. The diagonal*dense part is simply divided by the sqrt of diagonal.
* 3. Compute Cholesky of dense*dense - outer product of cholesky of diagonal*dense computed in previous step
*
* @param chol
* @return
*/
public Cholesky cholesky(Cholesky chol, boolean parallelize, String id) {
long start = System.currentTimeMillis();
if (chol == null) {
double[][] xx = _xx.clone();
for (int i = 0; i < xx.length; ++i) xx[i] = xx[i].clone();
chol = new Cholesky(xx, _diag.clone());
}
final Cholesky fchol = chol;
final int sparseN = _diag.length;
final int denseN = _fullN - sparseN;
// compute the cholesky of the diagonal and diagonal*dense parts
if (_diag != null)
for (int i = 0; i < sparseN; ++i) {
double d = 1.0 / (chol._diag[i] = Math.sqrt(_diag[i]));
for (int j = 0; j < denseN; ++j) chol._xx[j][i] = d * _xx[j][i];
}
ForkJoinTask[] fjts = new ForkJoinTask[denseN];
// compute the outer product of diagonal*dense
//Log.info("SPARSEN = " + sparseN + " DENSEN = " + denseN);
final int[][] nz = new int[denseN][];
for (int i = 0; i < denseN; ++i) {
final int fi = i;
fjts[i] = new RecursiveAction() {
@Override
protected void compute() {
int[] tmp = new int[sparseN];
double[] rowi = fchol._xx[fi];
int n = 0;
for (int k = 0; k < sparseN; ++k) if (rowi[k] != .0)
tmp[n++] = k;
nz[fi] = Arrays.copyOf(tmp, n);
}
};
}
ForkJoinTask.invokeAll(fjts);
for (int i = 0; i < denseN; ++i) {
final int fi = i;
fjts[i] = new RecursiveAction() {
@Override
protected void compute() {
double[] rowi = fchol._xx[fi];
int[] nzi = nz[fi];
for (int j = 0; j <= fi; ++j) {
double[] rowj = fchol._xx[j];
int[] nzj = nz[j];
double s = 0;
for (int t = 0, z = 0; t < nzi.length && z < nzj.length; ) {
int k1 = nzi[t];
int k2 = nzj[z];
if (k1 < k2) {
t++;
continue;
} else if (k1 > k2) {
z++;
continue;
} else {
s += rowi[k1] * rowj[k1];
t++;
z++;
}
}
rowi[j + sparseN] = _xx[fi][j + sparseN] - s;
}
}
};
}
ForkJoinTask.invokeAll(fjts);
// compute the cholesky of dense*dense-outer_product(diagonal*dense)
// TODO we still use Jama, which requires (among other things) copy and expansion of the matrix. Do it here without copy and faster.
double[][] arr = new double[denseN][];
for (int i = 0; i < arr.length; ++i) arr[i] = Arrays.copyOfRange(fchol._xx[i], sparseN, sparseN + denseN);
// Log.info(id + ": CHOLESKY PRECOMPUTE TIME " + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
// parallelize cholesky
if (parallelize) {
int p = Runtime.getRuntime().availableProcessors();
InPlaceCholesky d = InPlaceCholesky.decompose_2(arr, 10, p);
fchol.setSPD(d.isSPD());
arr = d.getL();
// Log.info (id + ": H2O CHOLESKY DECOMP TAKES: " + (System.currentTimeMillis()-start));
} else {
// make it symmetric
for (int i = 0; i < arr.length; ++i) for (int j = 0; j < i; ++j) arr[j][i] = arr[i][j];
CholeskyDecomposition c = new Matrix(arr).chol();
fchol.setSPD(c.isSPD());
arr = c.getL().getArray();
//Log.info ("JAMA CHOLESKY DECOMPOSE TAKES: " + (System.currentTimeMillis()-start));
}
for (int i = 0; i < arr.length; ++i) System.arraycopy(arr[i], 0, fchol._xx[i], sparseN, i + 1);
return chol;
}
use of Jama.Matrix 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);
}
use of Jama.Matrix in project h2o-3 by h2oai.
the class LinearAlgebraUtils method multiple.
static double[] multiple(double[] diagYY, /*diagonal*/
int nTot, int nVars) {
int ny = diagYY.length;
for (int i = 0; i < ny; i++) {
diagYY[i] *= nTot;
}
double[][] uu = new double[ny][ny];
for (int i = 0; i < ny; i++) {
for (int j = 0; j < ny; j++) {
double yyij = i == j ? diagYY[i] : 0;
uu[i][j] = (yyij - diagYY[i] * diagYY[j] / nTot) / (nVars * Math.sqrt(diagYY[i] * diagYY[j]));
if (Double.isNaN(uu[i][j])) {
uu[i][j] = 0;
}
}
}
EigenvalueDecomposition eigen = new EigenvalueDecomposition(new Matrix(uu));
double[] eigenvalues = eigen.getRealEigenvalues();
double[][] eigenvectors = eigen.getV().getArray();
int maxIndex = ArrayUtils.maxIndex(eigenvalues);
return eigenvectors[maxIndex];
}
Aggregations