public Multinomial(Iterable<WeightedThing<T>> things) { this(); for (WeightedThing<T> thing : things) { add(thing.getValue(), thing.getWeight()); } }
/** * Adds up the distances from each point to its closest cluster and returns the sum. * @param datapoints iterable of datapoints. * @param centroids searcher of Centroids. * @return the total cost described above. */ public static double totalClusterCost(Iterable<? extends Vector> datapoints, Searcher centroids) { double totalCost = 0; for (Vector vector : datapoints) { totalCost += centroids.searchFirst(vector, false).getWeight(); } return totalCost; }
/** * Adds up the distances from each point to its closest cluster and returns the sum. * @param datapoints iterable of datapoints. * @param centroids searcher of Centroids. * @return the total cost described above. */ public static double totalClusterCost(Iterable<? extends Vector> datapoints, Searcher centroids) { double totalCost = 0; for (Vector vector : datapoints) { totalCost += centroids.searchFirst(vector, false).getWeight(); } return totalCost; }
/** * Adds up the distances from each point to its closest cluster and returns the sum. * @param datapoints iterable of datapoints. * @param centroids searcher of Centroids. * @return the total cost described above. */ public static double totalClusterCost(Iterable<? extends Vector> datapoints, Searcher centroids) { double totalCost = 0; for (Vector vector : datapoints) { totalCost += centroids.searchFirst(vector, false).getWeight(); } return totalCost; }
public Multinomial(Iterable<WeightedThing<T>> things) { this(); for (WeightedThing<T> thing : things) { add(thing.getValue(), thing.getWeight()); } }
@Override public List<WeightedThing<Vector>> search(Vector query, int limit) { PriorityQueue<WeightedThing<Vector>> top = searchInternal(query); List<WeightedThing<Vector>> results = Lists.newArrayListWithExpectedSize(top.size()); while (top.size() != 0) { WeightedThing<Vector> wv = top.pop(); results.add(new WeightedThing<Vector>(((HashedVector) wv.getValue()).getVector(), wv.getWeight())); } Collections.reverse(results); if (limit < results.size()) { results = results.subList(0, limit); } return results; }
@Override public List<WeightedThing<Vector>> search(Vector query, int limit) { PriorityQueue<WeightedThing<Vector>> top = searchInternal(query); List<WeightedThing<Vector>> results = Lists.newArrayListWithExpectedSize(top.size()); while (top.size() != 0) { WeightedThing<Vector> wv = top.pop(); results.add(new WeightedThing<Vector>(((HashedVector) wv.getValue()).getVector(), wv.getWeight())); } Collections.reverse(results); if (limit < results.size()) { results = results.subList(0, limit); } return results; }
@Override public List<WeightedThing<Vector>> search(Vector query, int limit) { PriorityQueue<WeightedThing<Vector>> top = searchInternal(query); List<WeightedThing<Vector>> results = Lists.newArrayListWithExpectedSize(top.size()); while (top.size() != 0) { WeightedThing<Vector> wv = top.pop(); results.add(new WeightedThing<>(((HashedVector) wv.getValue()).getVector(), wv.getWeight())); } Collections.reverse(results); if (limit < results.size()) { results = results.subList(0, limit); } return results; }
protected static WeightedThing<Vector> removeHash(WeightedThing<Vector> input) { return new WeightedThing<Vector>(((HashedVector) input.getValue()).getVector(), input.getWeight()); }
protected static WeightedThing<Vector> removeHash(WeightedThing<Vector> input) { return new WeightedThing<>(((HashedVector) input.getValue()).getVector(), input.getWeight()); }
protected static WeightedThing<Vector> removeHash(WeightedThing<Vector> input) { return new WeightedThing<Vector>(((HashedVector) input.getValue()).getVector(), input.getWeight()); }
@Override public boolean remove(Vector vector, double epsilon) { WeightedThing<Vector> toRemove = searchFirst(vector, false); if (toRemove.getWeight() < epsilon) { Iterator<? extends Vector> basisVectors = basisMatrix.iterator(); for (TreeMultiset<WeightedThing<Vector>> projection : scalarProjections) { if (!projection.remove(new WeightedThing<Vector>(vector, vector.dot(basisVectors.next())))) { throw new RuntimeException("Internal inconsistency in ProjectionSearch"); } } return true; } else { return false; } }
@Override public boolean remove(Vector vector, double epsilon) { WeightedThing<Vector> toRemove = searchFirst(vector, false); if (toRemove.getWeight() < epsilon) { Iterator<? extends Vector> basisVectors = basisMatrix.iterator(); for (TreeMultiset<WeightedThing<Vector>> projection : scalarProjections) { if (!projection.remove(new WeightedThing<Vector>(vector, vector.dot(basisVectors.next())))) { throw new RuntimeException("Internal inconsistency in ProjectionSearch"); } } return true; } else { return false; } }
@Override public boolean remove(Vector vector, double epsilon) { WeightedThing<Vector> toRemove = searchFirst(vector, false); if (toRemove.getWeight() < epsilon) { Iterator<? extends Vector> basisVectors = basisMatrix.iterator(); for (TreeMultiset<WeightedThing<Vector>> projection : scalarProjections) { if (!projection.remove(new WeightedThing<>(vector, vector.dot(basisVectors.next())))) { throw new RuntimeException("Internal inconsistency in ProjectionSearch"); } } return true; } else { return false; } }
@Test public void testNearMatch() { searcher.clear(); List<MatrixSlice> queries = Lists.newArrayList(Iterables.limit(dataPoints, 100)); searcher.addAllMatrixSlicesAsWeightedVectors(dataPoints); MultiNormal noise = new MultiNormal(0.01, new DenseVector(20)); for (MatrixSlice slice : queries) { Vector query = slice.vector(); final Vector epsilon = noise.sample(); List<WeightedThing<Vector>> r = searcher.search(query, 2); query = query.plus(epsilon); assertEquals("Distance has to be small", epsilon.norm(2), r.get(0).getWeight(), 1.0e-1); assertEquals("Answer must be substantially the same as query", epsilon.norm(2), r.get(0).getValue().minus(query).norm(2), 1.0e-1); assertTrue("Wrong answer must be further away", r.get(1).getWeight() > r.get(0).getWeight()); } }
@Test public void testSearchFirst() { searcher.clear(); searcher.addAll(dataPoints); for (Vector datapoint : dataPoints) { WeightedThing<Vector> first = searcher.searchFirst(datapoint, false); WeightedThing<Vector> second = searcher.searchFirst(datapoint, true); List<WeightedThing<Vector>> firstTwo = searcher.search(datapoint, 2); assertEquals("First isn't self", 0, first.getWeight(), 0); assertEquals("First isn't self", datapoint, first.getValue()); assertEquals("First doesn't match", first, firstTwo.get(0)); assertEquals("Second doesn't match", second, firstTwo.get(1)); } }
@Test public void testOrdering() { searcher.clear(); Matrix queries = new DenseMatrix(100, 20); MultiNormal gen = new MultiNormal(20); for (int i = 0; i < 100; i++) { queries.viewRow(i).assign(gen.sample()); } searcher.addAllMatrixSlices(dataPoints); for (MatrixSlice query : queries) { List<WeightedThing<Vector>> r = searcher.search(query.vector(), 200); double x = 0; for (WeightedThing<Vector> thing : r) { assertTrue("Scores must be monotonic increasing", thing.getWeight() >= x); x = thing.getWeight(); } } }