Search in sources :

Example 1 with EigenvalueDecomposition

use of Jama.EigenvalueDecomposition in project knime-core by knime.

the class PCAComputeNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    if (!(inData[DATA_INPORT] instanceof BufferedDataTable)) {
        throw new IllegalArgumentException("Datatable as input expected");
    }
    final BufferedDataTable dataTable = (BufferedDataTable) inData[DATA_INPORT];
    if (dataTable.size() == 0) {
        throw new IllegalArgumentException("Input table is empty!");
    }
    final double[] meanVector = PCANodeModel.getMeanVector(dataTable, m_inputColumnIndices, m_failOnMissingValues.getBooleanValue(), exec.createSubExecutionContext(0.4));
    final double[][] m = new double[m_inputColumnIndices.length][m_inputColumnIndices.length];
    exec.checkCanceled();
    final int missingValues = PCANodeModel.getCovarianceMatrix(exec.createSubExecutionContext(0.4), dataTable, m_inputColumnIndices, meanVector, m);
    if (missingValues > 0) {
        if (m_failOnMissingValues.getBooleanValue()) {
            throw new IllegalArgumentException("missing, infinite or impossible values in table");
        }
        setWarningMessage(missingValues + " rows ignored because of missing, " + "infinite or impossible values");
    }
    exec.checkCanceled();
    final Matrix covarianceMatrix = new Matrix(m);
    exec.setProgress("calculation of spectral decomposition");
    final EigenvalueDecomposition evd = covarianceMatrix.eig();
    exec.checkCanceled();
    exec.setProgress(0.9);
    final Matrix d = evd.getD();
    final double[] evs = new double[d.getRowDimension()];
    for (int i = 0; i < evs.length; i++) {
        evs[i] = d.get(i, i);
    }
    exec.checkCanceled();
    return new PortObject[] { PCANodeModel.createCovarianceTable(exec, m, m_inputColumnNames), PCANodeModel.createDecompositionOutputTable(exec.createSubExecutionContext(0.1), evd, m_inputColumnNames), new PCAModelPortObject(evd.getV().getArray(), evs, m_inputColumnNames, meanVector) };
}
Also used : Matrix(Jama.Matrix) EigenvalueDecomposition(Jama.EigenvalueDecomposition) BufferedDataTable(org.knime.core.node.BufferedDataTable) PortObject(org.knime.core.node.port.PortObject)

Example 2 with EigenvalueDecomposition

use of Jama.EigenvalueDecomposition 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];
}
Also used : Matrix(Jama.Matrix) EigenvalueDecomposition(Jama.EigenvalueDecomposition)

Example 3 with EigenvalueDecomposition

use of Jama.EigenvalueDecomposition in project knime-core by knime.

the class PCANodeModel method execute.

/**
 * Performs the PCA.
 *
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    // remove all non-numeric columns from the input date
    // final DataTable filteredTable =
    // filterNonNumericalColumns(inData[DATA_INPORT]);
    final BufferedDataTable dataTable = (BufferedDataTable) inData[DATA_INPORT];
    if (dataTable.size() == 0) {
        throw new IllegalArgumentException("Input table is empty!");
    }
    if (dataTable.size() == 1) {
        throw new IllegalArgumentException("Input table has only one row!");
    }
    final double[] meanVector = getMeanVector(dataTable, m_inputColumnIndices, false, exec.createSubExecutionContext(0.2));
    final double[][] m = new double[m_inputColumnIndices.length][m_inputColumnIndices.length];
    final int missingValues = getCovarianceMatrix(exec.createSubExecutionContext(0.2), dataTable, m_inputColumnIndices, meanVector, m);
    final Matrix covarianceMatrix = new Matrix(m);
    if (missingValues > 0) {
        if (m_failOnMissingValues.getBooleanValue()) {
            throw new IllegalArgumentException("missing, infinite or impossible values in table");
        }
        setWarningMessage(missingValues + " rows ignored because of missing" + ", infinite or impossible values");
    }
    final ExecutionContext evdContext = exec.createSubExecutionContext(0.2);
    evdContext.setMessage("computing spectral decomposition");
    final EigenvalueDecomposition eig = covarianceMatrix.eig();
    exec.checkCanceled();
    evdContext.setProgress(0.8);
    final double[] evs = EigenValue.extractEVVector(eig);
    m_dimSelection.setEigenValues(evs);
    final int dimensions = m_dimSelection.getNeededDimensions();
    // don't remember these in case input changes
    m_dimSelection.setEigenValues(null);
    // adjust to selected numerical columns
    if (dimensions > m_inputColumnIndices.length || dimensions < 1) {
        throw new IllegalArgumentException("invalid number of dimensions to reduce to: " + dimensions);
    }
    exec.checkCanceled();
    evdContext.setProgress(0.9);
    final Matrix eigenvectors = EigenValue.getSortedEigenVectors(eig.getV().getArray(), evs, dimensions);
    exec.checkCanceled();
    evdContext.setProgress(1);
    exec.checkCanceled();
    final DataColumnSpec[] specs = createAddTableSpec((DataTableSpec) inData[DATA_INPORT].getSpec(), dimensions);
    final CellFactory fac = new CellFactory() {

        @Override
        public DataCell[] getCells(final DataRow row) {
            return convertInputRow(eigenvectors, row, meanVector, m_inputColumnIndices, dimensions, false);
        }

        @Override
        public DataColumnSpec[] getColumnSpecs() {
            return specs;
        }

        @Override
        public void setProgress(final int curRowNr, final int rowCount, final RowKey lastKey, final ExecutionMonitor texec) {
            texec.setProgress(curRowNr / (double) rowCount, "processing " + curRowNr + " of " + rowCount);
        }
    };
    final ColumnRearranger cr = new ColumnRearranger((DataTableSpec) inData[0].getSpec());
    cr.append(fac);
    if (m_removeOriginalCols.getBooleanValue()) {
        cr.remove(m_inputColumnIndices);
    }
    final BufferedDataTable result = exec.createColumnRearrangeTable((BufferedDataTable) inData[0], cr, exec.createSubProgress(0.4));
    final PortObject[] out = new PortObject[1];
    out[DATA_OUTPORT] = result;
    // m_inputColumnNames);
    return out;
}
Also used : RowKey(org.knime.core.data.RowKey) EigenvalueDecomposition(Jama.EigenvalueDecomposition) DataRow(org.knime.core.data.DataRow) Matrix(Jama.Matrix) ExecutionContext(org.knime.core.node.ExecutionContext) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) CellFactory(org.knime.core.data.container.CellFactory) PortObject(org.knime.core.node.port.PortObject)

Aggregations

EigenvalueDecomposition (Jama.EigenvalueDecomposition)3 Matrix (Jama.Matrix)3 BufferedDataTable (org.knime.core.node.BufferedDataTable)2 PortObject (org.knime.core.node.port.PortObject)2 DataCell (org.knime.core.data.DataCell)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataRow (org.knime.core.data.DataRow)1 RowKey (org.knime.core.data.RowKey)1 CellFactory (org.knime.core.data.container.CellFactory)1 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)1 ExecutionContext (org.knime.core.node.ExecutionContext)1 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)1