Search in sources :

Example 1 with LinearEquationSystem

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

the class CorrelationAnalysisSolution method getNormalizedLinearEquationSystem.

/**
 * Returns the linear equation system for printing purposes. If normalization
 * is null the linear equation system is returned, otherwise the linear
 * equation system will be transformed according to the normalization.
 *
 * @param normalization the normalization, can be null
 * @return the linear equation system for printing purposes
 * @throws NonNumericFeaturesException if the linear equation system is not
 *         compatible with values initialized during normalization
 */
public LinearEquationSystem getNormalizedLinearEquationSystem(Normalization<?> normalization) throws NonNumericFeaturesException {
    if (normalization != null) {
        LinearEquationSystem lq = normalization.transform(linearEquationSystem);
        lq.solveByTotalPivotSearch();
        return lq;
    } else {
        return linearEquationSystem;
    }
}
Also used : LinearEquationSystem(de.lmu.ifi.dbs.elki.math.linearalgebra.LinearEquationSystem)

Example 2 with LinearEquationSystem

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

the class CorrelationAnalysisSolution method writeToText.

/**
 * Text output of the equation system
 */
@Override
public void writeToText(TextWriterStream out, String label) {
    if (label != null) {
        out.commentPrintLn(label);
    }
    out.commentPrintLn("Model class: " + this.getClass().getName());
    try {
        if (getNormalizedLinearEquationSystem(null) != null) {
            // TODO: more elegant way of doing normalization here?
            /*
         * if(out instanceof TextWriterStreamNormalizing) {
         * TextWriterStreamNormalizing<V> nout =
         * (TextWriterStreamNormalizing<V>) out; LinearEquationSystem lq =
         * getNormalizedLinearEquationSystem(nout.getNormalization());
         * out.commentPrint("Linear Equation System: ");
         * out.commentPrintLn(lq.equationsToString(nf)); } else {
         */
            LinearEquationSystem lq = getNormalizedLinearEquationSystem(null);
            out.commentPrint("Linear Equation System: ");
            out.commentPrintLn(lq.equationsToString(nf));
        // }
        }
    } catch (NonNumericFeaturesException e) {
        LoggingUtil.exception(e);
    }
}
Also used : NonNumericFeaturesException(de.lmu.ifi.dbs.elki.datasource.filter.normalization.NonNumericFeaturesException) LinearEquationSystem(de.lmu.ifi.dbs.elki.math.linearalgebra.LinearEquationSystem)

Example 3 with LinearEquationSystem

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

the class CASH method doRun.

/**
 * Runs the CASH algorithm on the specified database, this method is
 * recursively called until only noise is left.
 *
 * @param relation the Relation to run the CASH algorithm on
 * @param progress the progress object for verbose messages
 * @return a mapping of subspace dimensionalities to clusters
 */
private Clustering<Model> doRun(Relation<ParameterizationFunction> relation, FiniteProgress progress) {
    Clustering<Model> res = new Clustering<>("CASH clustering", "cash-clustering");
    final int dim = dimensionality(relation);
    // init heap
    ObjectHeap<IntegerPriorityObject<CASHInterval>> heap = new ComparableMinHeap<>();
    ModifiableDBIDs noiseIDs = DBIDUtil.newHashSet(relation.getDBIDs());
    initHeap(heap, relation, dim, noiseIDs);
    if (LOG.isVerbose()) {
        LOG.verbose(new StringBuilder().append("dim ").append(dim).append(" database.size ").append(relation.size()).toString());
    }
    // get the ''best'' d-dimensional intervals at max level
    while (!heap.isEmpty()) {
        CASHInterval interval = determineNextIntervalAtMaxLevel(heap);
        if (LOG.isVerbose()) {
            LOG.verbose("next interval in dim " + dim + ": " + interval);
        }
        // only noise left
        if (interval == null) {
            break;
        }
        // do a dim-1 dimensional run
        ModifiableDBIDs clusterIDs = DBIDUtil.newHashSet();
        if (dim > minDim + 1) {
            ModifiableDBIDs ids;
            double[][] basis_dim_minus_1;
            if (adjust) {
                ids = DBIDUtil.newHashSet();
                basis_dim_minus_1 = runDerivator(relation, dim, interval, ids);
            } else {
                ids = interval.getIDs();
                basis_dim_minus_1 = determineBasis(SpatialUtil.centroid(interval));
            }
            if (ids.size() != 0) {
                MaterializedRelation<ParameterizationFunction> db = buildDB(dim, basis_dim_minus_1, ids, relation);
                // add result of dim-1 to this result
                Clustering<Model> res_dim_minus_1 = doRun(db, progress);
                for (Cluster<Model> cluster : res_dim_minus_1.getAllClusters()) {
                    res.addToplevelCluster(cluster);
                    noiseIDs.removeDBIDs(cluster.getIDs());
                    clusterIDs.addDBIDs(cluster.getIDs());
                    processedIDs.addDBIDs(cluster.getIDs());
                }
            }
        } else // dim == minDim
        {
            LinearEquationSystem les = runDerivator(relation, dim - 1, interval.getIDs());
            Cluster<Model> c = new Cluster<Model>(interval.getIDs(), new LinearEquationModel(les));
            res.addToplevelCluster(c);
            noiseIDs.removeDBIDs(interval.getIDs());
            clusterIDs.addDBIDs(interval.getIDs());
            processedIDs.addDBIDs(interval.getIDs());
        }
        // Rebuild heap
        ArrayList<IntegerPriorityObject<CASHInterval>> heapVector = new ArrayList<>(heap.size());
        for (ObjectHeap.UnsortedIter<IntegerPriorityObject<CASHInterval>> iter = heap.unsortedIter(); iter.valid(); iter.advance()) {
            heapVector.add(iter.get());
        }
        heap.clear();
        for (IntegerPriorityObject<CASHInterval> pair : heapVector) {
            CASHInterval currentInterval = pair.getObject();
            currentInterval.removeIDs(clusterIDs);
            if (currentInterval.getIDs().size() >= minPts) {
                heap.add(new IntegerPriorityObject<>(currentInterval.priority(), currentInterval));
            }
        }
        if (progress != null) {
            progress.setProcessed(processedIDs.size(), LOG);
        }
    }
    // put noise to clusters
    if (!noiseIDs.isEmpty()) {
        if (dim == noiseDim) {
            res.addToplevelCluster(new Cluster<Model>(noiseIDs, true, ClusterModel.CLUSTER));
            processedIDs.addDBIDs(noiseIDs);
        } else if (noiseIDs.size() >= minPts) {
            LinearEquationSystem les = runDerivator(fulldatabase, dim - 1, noiseIDs);
            res.addToplevelCluster(new Cluster<Model>(noiseIDs, true, new LinearEquationModel(les)));
            processedIDs.addDBIDs(noiseIDs);
        }
    }
    if (LOG.isDebugging()) {
        StringBuilder msg = new StringBuilder();
        msg.append("noise fuer dim ").append(dim).append(": ").append(noiseIDs.size());
        for (Cluster<Model> c : res.getAllClusters()) {
            if (c.getModel() instanceof LinearEquationModel) {
                msg.append("\n Cluster: Dim: ").append(((LinearEquationModel) c.getModel()).getLes().subspacedim());
            } else {
                msg.append("\n Cluster: ").append(c.getModel().getClass().getName());
            }
            msg.append(" size: ").append(c.size());
        }
        LOG.debugFine(msg.toString());
    }
    if (progress != null) {
        progress.setProcessed(processedIDs.size(), LOG);
    }
    return res;
}
Also used : CASHInterval(de.lmu.ifi.dbs.elki.algorithm.clustering.correlation.cash.CASHInterval) ComparableMinHeap(de.lmu.ifi.dbs.elki.utilities.datastructures.heap.ComparableMinHeap) ArrayList(java.util.ArrayList) ObjectHeap(de.lmu.ifi.dbs.elki.utilities.datastructures.heap.ObjectHeap) LinearEquationModel(de.lmu.ifi.dbs.elki.data.model.LinearEquationModel) ClusterModel(de.lmu.ifi.dbs.elki.data.model.ClusterModel) Model(de.lmu.ifi.dbs.elki.data.model.Model) IntegerPriorityObject(de.lmu.ifi.dbs.elki.utilities.datastructures.heap.IntegerPriorityObject) ParameterizationFunction(de.lmu.ifi.dbs.elki.algorithm.clustering.correlation.cash.ParameterizationFunction) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) LinearEquationSystem(de.lmu.ifi.dbs.elki.math.linearalgebra.LinearEquationSystem) LinearEquationModel(de.lmu.ifi.dbs.elki.data.model.LinearEquationModel)

Example 4 with LinearEquationSystem

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

the class CASH method runDerivator.

/**
 * Runs the derivator on the specified interval and assigns all points having
 * a distance less then the standard deviation of the derivator model to the
 * model to this model.
 *
 * @param relation the database containing the parameterization functions
 * @param ids the ids to build the model
 * @param dimensionality the dimensionality of the subspace
 * @return a basis of the found subspace
 */
private LinearEquationSystem runDerivator(Relation<ParameterizationFunction> relation, int dimensionality, DBIDs ids) {
    try {
        // build database for derivator
        Database derivatorDB = buildDerivatorDB(relation, ids);
        PCARunner pca = new PCARunner(new StandardCovarianceMatrixBuilder());
        EigenPairFilter filter = new FirstNEigenPairFilter(dimensionality);
        DependencyDerivator<DoubleVector> derivator = new DependencyDerivator<>(null, FormatUtil.NF4, pca, filter, 0, false);
        CorrelationAnalysisSolution<DoubleVector> model = derivator.run(derivatorDB);
        LinearEquationSystem les = model.getNormalizedLinearEquationSystem(null);
        return les;
    } catch (NonNumericFeaturesException e) {
        throw new IllegalStateException("Error during normalization" + e);
    }
}
Also used : EigenPairFilter(de.lmu.ifi.dbs.elki.math.linearalgebra.pca.filter.EigenPairFilter) FirstNEigenPairFilter(de.lmu.ifi.dbs.elki.math.linearalgebra.pca.filter.FirstNEigenPairFilter) DependencyDerivator(de.lmu.ifi.dbs.elki.algorithm.DependencyDerivator) FirstNEigenPairFilter(de.lmu.ifi.dbs.elki.math.linearalgebra.pca.filter.FirstNEigenPairFilter) PCARunner(de.lmu.ifi.dbs.elki.math.linearalgebra.pca.PCARunner) NonNumericFeaturesException(de.lmu.ifi.dbs.elki.datasource.filter.normalization.NonNumericFeaturesException) Database(de.lmu.ifi.dbs.elki.database.Database) ProxyDatabase(de.lmu.ifi.dbs.elki.database.ProxyDatabase) LinearEquationSystem(de.lmu.ifi.dbs.elki.math.linearalgebra.LinearEquationSystem) StandardCovarianceMatrixBuilder(de.lmu.ifi.dbs.elki.math.linearalgebra.pca.StandardCovarianceMatrixBuilder)

Example 5 with LinearEquationSystem

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

the class DependencyDerivator method generateModel.

/**
 * Runs the pca on the given set of IDs and for the given centroid.
 *
 * @param relation the database
 * @param ids the set of ids
 * @param centroid the centroid
 * @return a matrix of equations describing the dependencies
 */
public CorrelationAnalysisSolution<V> generateModel(Relation<V> relation, DBIDs ids, double[] centroid) {
    CorrelationAnalysisSolution<V> sol;
    if (LOG.isDebuggingFine()) {
        LOG.debugFine("PCA...");
    }
    SortedEigenPairs epairs = pca.processIds(ids, relation).getEigenPairs();
    int numstrong = filter.filter(epairs.eigenValues());
    PCAFilteredResult pcares = new PCAFilteredResult(epairs, numstrong, 1., 0.);
    // Matrix weakEigenvectors =
    // pca.getEigenvectors().times(pca.selectionMatrixOfWeakEigenvectors());
    double[][] weakEigenvectors = pcares.getWeakEigenvectors();
    // Matrix strongEigenvectors =
    // pca.getEigenvectors().times(pca.selectionMatrixOfStrongEigenvectors());
    double[][] strongEigenvectors = pcares.getStrongEigenvectors();
    // TODO: what if we don't have any weak eigenvectors?
    if (weakEigenvectors[0].length == 0) {
        sol = new CorrelationAnalysisSolution<>(null, relation, strongEigenvectors, weakEigenvectors, pcares.similarityMatrix(), centroid);
    } else {
        double[][] transposedWeakEigenvectors = transpose(weakEigenvectors);
        if (LOG.isDebugging()) {
            StringBuilder msg = new StringBuilder(1000);
            formatTo(msg.append("Strong Eigenvectors:\n"), pcares.getStrongEigenvectors(), " [", "]\n", ", ", nf);
            formatTo(msg.append("\nTransposed weak Eigenvectors:\n"), transposedWeakEigenvectors, " [", "]\n", ", ", nf);
            formatTo(msg.append("\nEigenvalues:\n"), pcares.getEigenvalues(), ", ", nf);
            LOG.debugFine(msg.toString());
        }
        double[] b = times(transposedWeakEigenvectors, centroid);
        if (LOG.isDebugging()) {
            StringBuilder msg = new StringBuilder(1000);
            formatTo(msg.append("Centroid:\n"), centroid, ", ", nf);
            formatTo(msg.append("\ntEV * Centroid\n"), b, ", ", nf);
            LOG.debugFine(msg.toString());
        }
        // +1 == + B[0].length
        double[][] gaussJordan = new double[transposedWeakEigenvectors.length][transposedWeakEigenvectors[0].length + 1];
        setMatrix(gaussJordan, 0, transposedWeakEigenvectors.length, 0, transposedWeakEigenvectors[0].length, transposedWeakEigenvectors);
        setCol(gaussJordan, transposedWeakEigenvectors[0].length, b);
        if (LOG.isDebuggingFiner()) {
            LOG.debugFiner("Gauss-Jordan-Elimination of " + format(gaussJordan, " [", "]\n", ", ", nf));
        }
        LinearEquationSystem lq = new LinearEquationSystem(copy(transposedWeakEigenvectors), b);
        lq.solveByTotalPivotSearch();
        sol = new CorrelationAnalysisSolution<>(lq, relation, strongEigenvectors, pcares.getWeakEigenvectors(), pcares.similarityMatrix(), centroid);
        if (LOG.isDebuggingFine()) {
            LOG.debugFine(// 
            new StringBuilder().append("Solution:\n").append("Standard deviation ").append(// 
            sol.getStandardDeviation()).append(lq.equationsToString(nf.getMaximumFractionDigits())).toString());
        }
    }
    return sol;
}
Also used : SortedEigenPairs(de.lmu.ifi.dbs.elki.math.linearalgebra.pca.SortedEigenPairs) PCAFilteredResult(de.lmu.ifi.dbs.elki.math.linearalgebra.pca.PCAFilteredResult) LinearEquationSystem(de.lmu.ifi.dbs.elki.math.linearalgebra.LinearEquationSystem)

Aggregations

LinearEquationSystem (de.lmu.ifi.dbs.elki.math.linearalgebra.LinearEquationSystem)6 NonNumericFeaturesException (de.lmu.ifi.dbs.elki.datasource.filter.normalization.NonNumericFeaturesException)2 DependencyDerivator (de.lmu.ifi.dbs.elki.algorithm.DependencyDerivator)1 CASHInterval (de.lmu.ifi.dbs.elki.algorithm.clustering.correlation.cash.CASHInterval)1 ParameterizationFunction (de.lmu.ifi.dbs.elki.algorithm.clustering.correlation.cash.ParameterizationFunction)1 ClusterModel (de.lmu.ifi.dbs.elki.data.model.ClusterModel)1 LinearEquationModel (de.lmu.ifi.dbs.elki.data.model.LinearEquationModel)1 Model (de.lmu.ifi.dbs.elki.data.model.Model)1 Database (de.lmu.ifi.dbs.elki.database.Database)1 ProxyDatabase (de.lmu.ifi.dbs.elki.database.ProxyDatabase)1 ModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)1 PCAFilteredResult (de.lmu.ifi.dbs.elki.math.linearalgebra.pca.PCAFilteredResult)1 PCARunner (de.lmu.ifi.dbs.elki.math.linearalgebra.pca.PCARunner)1 SortedEigenPairs (de.lmu.ifi.dbs.elki.math.linearalgebra.pca.SortedEigenPairs)1 StandardCovarianceMatrixBuilder (de.lmu.ifi.dbs.elki.math.linearalgebra.pca.StandardCovarianceMatrixBuilder)1 EigenPairFilter (de.lmu.ifi.dbs.elki.math.linearalgebra.pca.filter.EigenPairFilter)1 FirstNEigenPairFilter (de.lmu.ifi.dbs.elki.math.linearalgebra.pca.filter.FirstNEigenPairFilter)1 ComparableMinHeap (de.lmu.ifi.dbs.elki.utilities.datastructures.heap.ComparableMinHeap)1 IntegerPriorityObject (de.lmu.ifi.dbs.elki.utilities.datastructures.heap.IntegerPriorityObject)1 ObjectHeap (de.lmu.ifi.dbs.elki.utilities.datastructures.heap.ObjectHeap)1