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;
}
}
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);
}
}
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;
}
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);
}
}
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;
}
Aggregations