Search in sources :

Example 21 with NumberVector

use of de.lmu.ifi.dbs.elki.data.NumberVector in project elki by elki-project.

the class UKMeans method means.

/**
 * Returns the mean vectors of the given clusters in the given database.
 *
 * @param clusters the clusters to compute the means
 * @param means the recent means
 * @param database the database containing the vectors
 * @return the mean vectors of the given clusters in the given database
 */
protected List<double[]> means(List<? extends ModifiableDBIDs> clusters, List<double[]> means, Relation<DiscreteUncertainObject> database) {
    List<double[]> newMeans = new ArrayList<>(k);
    for (int i = 0; i < k; i++) {
        ModifiableDBIDs list = clusters.get(i);
        double[] mean = null;
        if (list.size() > 0) {
            DBIDIter iter = list.iter();
            // Initialize with first.
            mean = ArrayLikeUtil.toPrimitiveDoubleArray(database.get(iter).getCenterOfMass());
            iter.advance();
            // Update with remaining instances
            for (; iter.valid(); iter.advance()) {
                NumberVector vec = database.get(iter).getCenterOfMass();
                for (int j = 0; j < mean.length; j++) {
                    mean[j] += vec.doubleValue(j);
                }
            }
            timesEquals(mean, 1.0 / list.size());
        } else {
            // Keep degenerated means as-is for now.
            mean = means.get(i);
        }
        newMeans.add(mean);
    }
    return newMeans;
}
Also used : NumberVector(de.lmu.ifi.dbs.elki.data.NumberVector) ArrayList(java.util.ArrayList) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 22 with NumberVector

use of de.lmu.ifi.dbs.elki.data.NumberVector in project elki by elki-project.

the class AbstractUncertainObject method computeBounds.

/**
 * Compute the bounding box for some samples.
 *
 * @param samples Samples
 * @return Bounding box.
 */
protected static HyperBoundingBox computeBounds(NumberVector[] samples) {
    assert (samples.length > 0) : "Cannot compute bounding box of empty set.";
    // Compute bounds:
    final int dimensions = samples[0].getDimensionality();
    final double[] min = new double[dimensions];
    final double[] max = new double[dimensions];
    NumberVector first = samples[0];
    for (int d = 0; d < dimensions; d++) {
        min[d] = max[d] = first.doubleValue(d);
    }
    for (int i = 1; i < samples.length; i++) {
        NumberVector v = samples[i];
        for (int d = 0; d < dimensions; d++) {
            final double c = v.doubleValue(d);
            min[d] = c < min[d] ? c : min[d];
            max[d] = c > max[d] ? c : max[d];
        }
    }
    return new HyperBoundingBox(min, max);
}
Also used : NumberVector(de.lmu.ifi.dbs.elki.data.NumberVector) HyperBoundingBox(de.lmu.ifi.dbs.elki.data.HyperBoundingBox)

Example 23 with NumberVector

use of de.lmu.ifi.dbs.elki.data.NumberVector in project elki by elki-project.

the class ORCLUS method assign.

/**
 * Creates a partitioning of the database by assigning each object to its
 * closest seed.
 *
 * @param database the database holding the objects
 * @param clusters the array of clusters to which the objects should be
 *        assigned to
 */
private void assign(Relation<V> database, List<ORCLUSCluster> clusters) {
    NumberVectorDistanceFunction<? super V> distFunc = SquaredEuclideanDistanceFunction.STATIC;
    // clear the current clusters
    for (ORCLUSCluster cluster : clusters) {
        cluster.objectIDs.clear();
    }
    // projected centroids of the clusters
    List<NumberVector> projectedCentroids = new ArrayList<>(clusters.size());
    for (ORCLUSCluster c : clusters) {
        projectedCentroids.add(DoubleVector.wrap(project(c, c.centroid)));
    }
    // for each data point o do
    for (DBIDIter it = database.iterDBIDs(); it.valid(); it.advance()) {
        double[] o = database.get(it).toArray();
        double minDist = Double.POSITIVE_INFINITY;
        ORCLUSCluster minCluster = null;
        // determine projected distance between o and cluster
        for (int i = 0; i < clusters.size(); i++) {
            ORCLUSCluster c = clusters.get(i);
            NumberVector o_proj = DoubleVector.wrap(project(c, o));
            double dist = distFunc.distance(o_proj, projectedCentroids.get(i));
            if (dist < minDist) {
                minDist = dist;
                minCluster = c;
            }
        }
        // add p to the cluster with the least value of projected distance
        minCluster.objectIDs.add(it);
    }
    // recompute the seed in each clusters
    for (ORCLUSCluster cluster : clusters) {
        if (cluster.objectIDs.size() > 0) {
            cluster.centroid = Centroid.make(database, cluster.objectIDs).toArray();
        }
    }
}
Also used : NumberVector(de.lmu.ifi.dbs.elki.data.NumberVector) ArrayList(java.util.ArrayList) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 24 with NumberVector

use of de.lmu.ifi.dbs.elki.data.NumberVector in project elki by elki-project.

the class SpatialPrimitiveDistanceFunctionTest method testSpatialDistanceConsistency.

@Test
public void testSpatialDistanceConsistency() {
    final Random rnd = new Random(0);
    final int dim = 7;
    final int iters = 10000;
    List<SpatialPrimitiveDistanceFunction<? super NumberVector>> dists = new ArrayList<>();
    dists.add(EuclideanDistanceFunction.STATIC);
    dists.add(ManhattanDistanceFunction.STATIC);
    dists.add(MaximumDistanceFunction.STATIC);
    dists.add(MinimumDistanceFunction.STATIC);
    dists.add(new LPNormDistanceFunction(3));
    dists.add(new LPNormDistanceFunction(.5));
    dists.add(CanberraDistanceFunction.STATIC);
    // Histogram intersection distance isn't proper for negative values
    // dists.add(HistogramIntersectionDistanceFunction.STATIC);
    dists.add(SquaredEuclideanDistanceFunction.STATIC);
    dists.add(ArcCosineDistanceFunction.STATIC);
    dists.add(CosineDistanceFunction.STATIC);
    double[] d1 = new double[dim];
    double[] d2 = new double[dim];
    double[] d3 = new double[dim];
    double[] d4 = new double[dim];
    DoubleVector v1 = DoubleVector.wrap(d1);
    ModifiableHyperBoundingBox mbr = new ModifiableHyperBoundingBox(d2, d3);
    DoubleVector v2 = DoubleVector.wrap(d4);
    for (int i = 0; i < iters; i++) {
        for (int d = 0; d < dim; d++) {
            d1[d] = (rnd.nextDouble() - .5) * 2E4;
            d2[d] = (rnd.nextDouble() - .5) * 2E4;
            d3[d] = (rnd.nextDouble() - .5) * 2E4;
            if (d2[d] > d3[d]) {
                double t = d2[d];
                d2[d] = d3[d];
                d3[d] = t;
            }
            double m = rnd.nextDouble();
            d4[d] = m * d2[d] + (1 - m) * d3[d];
        }
        for (SpatialPrimitiveDistanceFunction<? super NumberVector> dis : dists) {
            compareDistances(v1, mbr, v2, dis);
        }
    }
}
Also used : LPNormDistanceFunction(de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.LPNormDistanceFunction) Random(java.util.Random) NumberVector(de.lmu.ifi.dbs.elki.data.NumberVector) ArrayList(java.util.ArrayList) DoubleVector(de.lmu.ifi.dbs.elki.data.DoubleVector) ModifiableHyperBoundingBox(de.lmu.ifi.dbs.elki.data.ModifiableHyperBoundingBox) Test(org.junit.Test)

Example 25 with NumberVector

use of de.lmu.ifi.dbs.elki.data.NumberVector in project elki by elki-project.

the class LPIntegerNormDistanceFunction method minDist.

@Override
public double minDist(SpatialComparable mbr1, SpatialComparable mbr2) {
    final int dim1 = mbr1.getDimensionality(), dim2 = mbr2.getDimensionality();
    final int mindim = (dim1 < dim2) ? dim1 : dim2;
    final NumberVector v1 = (mbr1 instanceof NumberVector) ? (NumberVector) mbr1 : null;
    final NumberVector v2 = (mbr2 instanceof NumberVector) ? (NumberVector) mbr2 : null;
    double agg = // 
    (v1 != null) ? // 
    (v2 != null) ? preDistance(v1, v2, 0, mindim) : preDistanceVM(v1, mbr2, 0, mindim) : (v2 != null) ? preDistanceVM(v2, mbr1, 0, mindim) : preDistanceMBR(mbr1, mbr2, 0, mindim);
    // first object has more dimensions.
    if (dim1 > mindim) {
        agg += (v1 != null) ? preNorm(v1, mindim, dim1) : preNormMBR(mbr1, mindim, dim1);
    }
    // second object has more dimensions.
    if (dim2 > mindim) {
        agg += (v2 != null) ? preNorm(v2, mindim, dim2) : preNormMBR(mbr2, mindim, dim2);
    }
    return FastMath.pow(agg, invp);
}
Also used : NumberVector(de.lmu.ifi.dbs.elki.data.NumberVector)

Aggregations

NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)85 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)40 ArrayList (java.util.ArrayList)16 LongStatistic (de.lmu.ifi.dbs.elki.logging.statistics.LongStatistic)9 DoubleVector (de.lmu.ifi.dbs.elki.data.DoubleVector)8 MultipleObjectsBundle (de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle)8 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)8 Database (de.lmu.ifi.dbs.elki.database.Database)7 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)7 DoubleStatistic (de.lmu.ifi.dbs.elki.logging.statistics.DoubleStatistic)7 Random (java.util.Random)7 Test (org.junit.Test)7 VectorFieldTypeInformation (de.lmu.ifi.dbs.elki.data.type.VectorFieldTypeInformation)5 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)5 MeanVariance (de.lmu.ifi.dbs.elki.math.MeanVariance)5 EvaluationResult (de.lmu.ifi.dbs.elki.result.EvaluationResult)5 MeasurementGroup (de.lmu.ifi.dbs.elki.result.EvaluationResult.MeasurementGroup)5 List (java.util.List)5 SparseNumberVector (de.lmu.ifi.dbs.elki.data.SparseNumberVector)4 RandomProjectionFamily (de.lmu.ifi.dbs.elki.data.projection.random.RandomProjectionFamily)4