Search in sources :

Example 6 with SortedEigenPairs

use of de.lmu.ifi.dbs.elki.math.linearalgebra.pca.SortedEigenPairs in project elki by elki-project.

the class GlobalPrincipalComponentAnalysisTransform method prepareComplete.

@Override
protected void prepareComplete() {
    mean = covmat.getMeanVector();
    PCAResult pcares = (new PCARunner(null)).processCovarMatrix(covmat.destroyToPopulationMatrix());
    SortedEigenPairs eps = pcares.getEigenPairs();
    covmat = null;
    if (filter == null) {
        proj = new double[dim][dim];
        for (int d = 0; d < dim; d++) {
            EigenPair ep = eps.getEigenPair(d);
            double[] ev = ep.getEigenvector();
            double mult = 1. / FastMath.sqrt(ep.getEigenvalue());
            // Fill weighted and transposed:
            for (int i = 0; i < dim; i++) {
                proj[d][i] = ev[i] * mult;
            }
        }
    } else {
        final int pdim = filter.filter(eps.eigenValues());
        if (LOG.isVerbose()) {
            LOG.verbose("Reducing dimensionality from " + dim + " to " + pdim + " via PCA.");
        }
        proj = new double[pdim][dim];
        for (int d = 0; d < pdim; d++) {
            EigenPair ep = eps.getEigenPair(d);
            double[] ev = ep.getEigenvector();
            double mult = 1. / FastMath.sqrt(ep.getEigenvalue());
            // Fill weighted and transposed:
            for (int i = 0; i < dim; i++) {
                proj[d][i] = ev[i] * mult;
            }
        }
    }
    buf = new double[dim];
}
Also used : PCAResult(de.lmu.ifi.dbs.elki.math.linearalgebra.pca.PCAResult) PCARunner(de.lmu.ifi.dbs.elki.math.linearalgebra.pca.PCARunner) SortedEigenPairs(de.lmu.ifi.dbs.elki.math.linearalgebra.pca.SortedEigenPairs) EigenPair(de.lmu.ifi.dbs.elki.math.linearalgebra.pca.EigenPair)

Example 7 with SortedEigenPairs

use of de.lmu.ifi.dbs.elki.math.linearalgebra.pca.SortedEigenPairs in project elki by elki-project.

the class LinearDiscriminantAnalysisFilter method computeProjectionMatrix.

@Override
protected double[][] computeProjectionMatrix(List<V> vectorcolumn, List<? extends ClassLabel> classcolumn, int dim) {
    Map<ClassLabel, IntList> classes = partition(classcolumn);
    // Fix indexing of classes:
    List<ClassLabel> keys = new ArrayList<>(classes.keySet());
    // Compute centroids:
    List<Centroid> centroids = computeCentroids(dim, vectorcolumn, keys, classes);
    final double[][] sigmaB, sigmaI;
    // Between classes covariance:
    {
        CovarianceMatrix covmake = new CovarianceMatrix(dim);
        for (Centroid c : centroids) {
            covmake.put(c);
        }
        sigmaB = covmake.destroyToSampleMatrix();
    }
    {
        // (Average) within class variance:
        CovarianceMatrix covmake = new CovarianceMatrix(dim);
        int numc = keys.size();
        for (int i = 0; i < numc; i++) {
            double[] c = centroids.get(i).getArrayRef();
            // TODO: different weighting strategies? Sampling?
            for (IntIterator it = classes.get(keys.get(i)).iterator(); it.hasNext(); ) {
                covmake.put(minusEquals(vectorcolumn.get(it.nextInt()).toArray(), c));
            }
        }
        sigmaI = covmake.destroyToSampleMatrix();
        if (new LUDecomposition(sigmaI).det() == 0) {
            for (int i = 0; i < dim; i++) {
                sigmaI[i][i] += 1e-10;
            }
        }
    }
    double[][] sol = times(inverse(sigmaI), sigmaB);
    EigenvalueDecomposition decomp = new EigenvalueDecomposition(sol);
    SortedEigenPairs sorted = new SortedEigenPairs(decomp, false);
    return transpose(sorted.eigenVectors(tdim));
}
Also used : IntIterator(it.unimi.dsi.fastutil.ints.IntIterator) EigenvalueDecomposition(de.lmu.ifi.dbs.elki.math.linearalgebra.EigenvalueDecomposition) ArrayList(java.util.ArrayList) LUDecomposition(de.lmu.ifi.dbs.elki.math.linearalgebra.LUDecomposition) IntList(it.unimi.dsi.fastutil.ints.IntList) CovarianceMatrix(de.lmu.ifi.dbs.elki.math.linearalgebra.CovarianceMatrix) Centroid(de.lmu.ifi.dbs.elki.math.linearalgebra.Centroid) ClassLabel(de.lmu.ifi.dbs.elki.data.ClassLabel) SortedEigenPairs(de.lmu.ifi.dbs.elki.math.linearalgebra.pca.SortedEigenPairs)

Aggregations

SortedEigenPairs (de.lmu.ifi.dbs.elki.math.linearalgebra.pca.SortedEigenPairs)7 PCAFilteredResult (de.lmu.ifi.dbs.elki.math.linearalgebra.pca.PCAFilteredResult)5 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)3 HashSetModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs)2 ArrayList (java.util.ArrayList)2 PreDeConModel (de.lmu.ifi.dbs.elki.algorithm.clustering.gdbscan.PreDeConNeighborPredicate.PreDeConModel)1 ClassLabel (de.lmu.ifi.dbs.elki.data.ClassLabel)1 Cluster (de.lmu.ifi.dbs.elki.data.Cluster)1 CorrelationModel (de.lmu.ifi.dbs.elki.data.model.CorrelationModel)1 Model (de.lmu.ifi.dbs.elki.data.model.Model)1 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)1 DoubleDBIDList (de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDList)1 ModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)1 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)1 Centroid (de.lmu.ifi.dbs.elki.math.linearalgebra.Centroid)1 CovarianceMatrix (de.lmu.ifi.dbs.elki.math.linearalgebra.CovarianceMatrix)1 EigenvalueDecomposition (de.lmu.ifi.dbs.elki.math.linearalgebra.EigenvalueDecomposition)1 LUDecomposition (de.lmu.ifi.dbs.elki.math.linearalgebra.LUDecomposition)1 LinearEquationSystem (de.lmu.ifi.dbs.elki.math.linearalgebra.LinearEquationSystem)1 EigenPair (de.lmu.ifi.dbs.elki.math.linearalgebra.pca.EigenPair)1