Search in sources :

Example 6 with ClassificationTrainingRow

use of org.knime.base.node.mine.regression.logistic.learner4.data.ClassificationTrainingRow in project knime-core by knime.

the class IrlsLearner method irlsRls.

/**
 * Do an irls step. The result is stored in beta.
 *
 * @param data over trainings data.
 * @param beta parameter vector
 * @param rC regressors count
 * @param tcC target category count
 * @throws CanceledExecutionException when method is cancelled
 */
private void irlsRls(final TrainingData<ClassificationTrainingRow> data, final RealMatrix beta, final int rC, final int tcC, final ExecutionMonitor exec) throws CanceledExecutionException {
    long rowCount = 0;
    int dim = (rC + 1) * (tcC - 1);
    RealMatrix xTwx = MatrixUtils.createRealMatrix(dim, dim);
    RealMatrix xTyu = MatrixUtils.createRealMatrix(dim, 1);
    double[] eBetaTx = new double[tcC - 1];
    double[] pi = new double[tcC - 1];
    final long totalRowCount = data.getRowCount();
    for (ClassificationTrainingRow row : data) {
        rowCount++;
        exec.checkCanceled();
        exec.setProgress(rowCount / (double) totalRowCount, "Row " + rowCount + "/" + totalRowCount);
        for (int k = 0; k < tcC - 1; k++) {
            double z = 0.0;
            for (FeatureIterator iter = row.getFeatureIterator(); iter.next(); ) {
                double featureVal = iter.getFeatureValue();
                int featureIdx = iter.getFeatureIndex();
                z += featureVal * beta.getEntry(0, k * (rC + 1) + featureIdx);
            }
            eBetaTx[k] = Math.exp(z);
        }
        double sumEBetaTx = 0;
        for (int k = 0; k < tcC - 1; k++) {
            sumEBetaTx += eBetaTx[k];
        }
        for (int k = 0; k < tcC - 1; k++) {
            double pik = eBetaTx[k] / (1 + sumEBetaTx);
            pi[k] = pik;
        }
        // fill xTwx (aka the hessian of the loglikelihood)
        for (FeatureIterator outer = row.getFeatureIterator(); outer.next(); ) {
            int i = outer.getFeatureIndex();
            double outerVal = outer.getFeatureValue();
            for (FeatureIterator inner = outer.spawn(); inner.next(); ) {
                int ii = inner.getFeatureIndex();
                double innerVal = inner.getFeatureValue();
                for (int k = 0; k < tcC - 1; k++) {
                    for (int kk = k; kk < tcC - 1; kk++) {
                        int o1 = k * (rC + 1);
                        int o2 = kk * (rC + 1);
                        double v = xTwx.getEntry(o1 + i, o2 + ii);
                        if (k == kk) {
                            double w = pi[k] * (1 - pi[k]);
                            v += outerVal * w * innerVal;
                            assert o1 == o2;
                        } else {
                            double w = -pi[k] * pi[kk];
                            v += outerVal * w * innerVal;
                        }
                        xTwx.setEntry(o1 + i, o2 + ii, v);
                        xTwx.setEntry(o1 + ii, o2 + i, v);
                        if (k != kk) {
                            xTwx.setEntry(o2 + ii, o1 + i, v);
                            xTwx.setEntry(o2 + i, o1 + ii, v);
                        }
                    }
                }
            }
        }
        int g = row.getCategory();
        // fill matrix xTyu
        for (FeatureIterator iter = row.getFeatureIterator(); iter.next(); ) {
            int idx = iter.getFeatureIndex();
            double val = iter.getFeatureValue();
            for (int k = 0; k < tcC - 1; k++) {
                int o = k * (rC + 1);
                double v = xTyu.getEntry(o + idx, 0);
                double y = k == g ? 1 : 0;
                v += (y - pi[k]) * val;
                xTyu.setEntry(o + idx, 0, v);
            }
        }
    }
    // currently not used but could become interesting in the future
    // if (m_penaltyTerm > 0.0) {
    // RealMatrix stdError = getStdErrorMatrix(xTwx);
    // // do not penalize the constant terms
    // for (int i = 0; i < tcC - 1; i++) {
    // stdError.setEntry(i * (rC + 1), i * (rC + 1), 0);
    // }
    // xTwx = xTwx.add(stdError.scalarMultiply(-0.00001));
    // }
    exec.checkCanceled();
    b = xTwx.multiply(beta.transpose()).add(xTyu);
    A = xTwx;
    if (rowCount < A.getColumnDimension()) {
        // but it's important to ensure this property
        throw new IllegalStateException("The dataset must have at least " + A.getColumnDimension() + " rows, but it has only " + rowCount + " rows. It is recommended to use a " + "larger dataset in order to increase accuracy.");
    }
    DecompositionSolver solver = new SingularValueDecomposition(A).getSolver();
    RealMatrix betaNew = solver.solve(b);
    beta.setSubMatrix(betaNew.transpose().getData(), 0, 0);
}
Also used : FeatureIterator(org.knime.base.node.mine.regression.logistic.learner4.data.TrainingRow.FeatureIterator) ClassificationTrainingRow(org.knime.base.node.mine.regression.logistic.learner4.data.ClassificationTrainingRow) RealMatrix(org.apache.commons.math3.linear.RealMatrix) DecompositionSolver(org.apache.commons.math3.linear.DecompositionSolver) SingularValueDecomposition(org.apache.commons.math3.linear.SingularValueDecomposition)

Aggregations

ClassificationTrainingRow (org.knime.base.node.mine.regression.logistic.learner4.data.ClassificationTrainingRow)3 FeatureIterator (org.knime.base.node.mine.regression.logistic.learner4.data.TrainingRow.FeatureIterator)3 RealMatrix (org.apache.commons.math3.linear.RealMatrix)2 DecompositionSolver (org.apache.commons.math3.linear.DecompositionSolver)1 SingularValueDecomposition (org.apache.commons.math3.linear.SingularValueDecomposition)1 Test (org.junit.Test)1 LogRegLearnerResult (org.knime.base.node.mine.regression.logistic.learner4.LogRegLearnerResult)1 SparseClassificationTrainingRowBuilder (org.knime.base.node.mine.regression.logistic.learner4.data.SparseClassificationTrainingRowBuilder)1 SagLogRegLearner (org.knime.base.node.mine.regression.logistic.learner4.sg.SagLogRegLearner)1 BufferedDataTable (org.knime.core.node.BufferedDataTable)1 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)1