/** * Returns Hamming distance between the two bytes. */ public static int d(byte x, byte y) { return d((int)x, (int)y); }
/** * Constructor. Damerau-Levenshtein distance. * @param maxStringLength the maximum length of strings that will be * feed to this algorithm. * @param damerau if true, calculate Damerau-Levenshtein distance instead * of plain Levenshtein distance. */ public EditDistance(int maxStringLength, boolean damerau) { FKP = new int[2*maxStringLength+1][maxStringLength+2]; damerauDistance = damerau; if (damerau) brf = new BRF2(); else brf = new BRF1(); }
/** * Cluster a new instance. * @param x a new instance. * @return the cluster label, which is the index of nearest medoid. */ @Override public int predict(T x) { double minDist = Double.MAX_VALUE; int bestCluster = 0; for (int i = 0; i < k; i++) { double dist = distance.d(x, medoids[i]); if (dist < minDist) { minDist = dist; bestCluster = i; } } return bestCluster; }
/** * Edit distance between two strings. O(mn) time and O(n) space for weighted * edit distance. O(ne) time and O(mn) space for unit cost edit distance. * For weighted edit distance, this method is multi-thread safe. However, * it is NOT multi-thread safe for unit cost edit distance. */ public double d(char[] x, char[] y) { if (weight != null) return weightedEdit(x, y); else if (x.length == 1 || y.length == 1) if (damerauDistance) return damerau(x, y); else return levenshtein(x, y); else return br(x, y); }
@Override public double predict(T x) { double sum = 0.0, sumw = 0.0; for (int i = 0; i < rbf.length; i++) { double f = rbf[i].f(distance.d(x, centers[i])); sumw += w[i] * f; sum += f; } return normalized ? sumw / sum : sumw; } }
/** * Learn the K-NN classifier from data of type double[]. * @param k the number of neighbors for classification. * @param x training samples. * @param y training labels in [0, c), where c is the number of classes. */ public static KNN<double[]> learn(double[][] x, int[] y, int k) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("The sizes of X and Y don't match: %d != %d", x.length, y.length)); } if (k < 1) { throw new IllegalArgumentException("Illegal k = " + k); } KNNSearch<double[], double[]> knn = null; if (x[0].length < 10) { knn = new KDTree<>(x, x); } else { knn = new CoverTree<>(x, new EuclideanDistance()); } return new KNN<>(knn, y, k); }
private Recognizer() { this.view = new HashSet<>(); this.gestureListener = new HashSet<>(); this.intToStringGestureMapping = new HashMap<>(); this.userManager = new UserManager(); this.userDataset = null; this.lastGestureTime = 0; this.recognitionSettings = new RecognitionSettingsImpl(UpdateRate.FPS_10, 0, 0, 0, 0, 0); this.dtw = new DynamicTimeWarping<Vector2D>((a, b) -> a.distance(b), this.recognitionSettings.getDtwRadius()); RecognitionScreenView.startFxThread(); }
/** * Edit distance between two strings. O(mn) time and O(n) space for weighted * edit distance. O(ne) time and O(mn) space for unit cost edit distance. * For weighted edit distance, this method is multi-thread safe. However, * it is NOT multi-thread safe for unit cost edit distance. */ @Override public double d(String x, String y) { if (weight != null) return weightedEdit(x, y); else if (x.length() == 1 || y.length() == 1) if (damerauDistance) return damerau(x, y); else return levenshtein(x, y); else return br(x, y); }
@Override public Neighbor<T,T> nearest(T q) { T neighbor = null; int index = -1; double dist = Double.MAX_VALUE; for (int i = 0; i < data.length; i++) { if (q == data[i] && identicalExcluded) { continue; } double d = distance.d(q, data[i]); if (d < dist) { neighbor = data[i]; index = i; dist = d; } } return new SimpleNeighbor<>(neighbor, index, dist); }
/** * Returns Hamming distance between the two shorts. */ public static int d(short x, short y) { return d((int)x, (int)y); }
/** * Moves all the points in pointSet covered by (the ball of) newPoint * into newPointSet, based on the given scale/level. * * @param pointSet the supplied set of instances from which * all points covered by newPoint will be removed. * @param newPointSet the set in which all points covered by * newPoint will be put into. * @param newPoint the given new point. * @param maxScale the scale based on which distances are * judged (radius of cover ball is calculated). */ private void distSplit(ArrayList<DistanceSet> pointSet, ArrayList<DistanceSet> newPointSet, E newPoint, int maxScale) { double fmax = getCoverRadius(maxScale); ArrayList<DistanceSet> newSet = new ArrayList<>(); for (int i = 0; i < pointSet.size(); i++) { DistanceSet n = pointSet.get(i); double newDist = distance.d(newPoint, n.getObject()); if (newDist <= fmax) { pointSet.get(i).dist.add(newDist); newPointSet.add(n); } else { newSet.add(n); } } pointSet.clear(); pointSet.addAll(newSet); }
double cost = distance.d(x1[i-1], x2[j-1]);
public void range(AbstractSentence q, double radius, List<Neighbor<AbstractSentence, E>> neighbors) { if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } long fpq = simhash64(q.tokens); Set<Integer> candidates = obtainCandidates(q.tokens); for (int index : candidates) { double distance = HammingDistance.d(fpq, signs.get(index)); if (distance <= radius) { if (keys.get(index).line != q.line || !identicalExcluded) { neighbors.add(new Neighbor<>(keys.get(index), data.get(index), index, distance)); } } } }
/** * Builds the cover tree. */ private void buildCoverTree() { ArrayList<DistanceSet> pointSet = new ArrayList<>(); ArrayList<DistanceSet> consumedSet = new ArrayList<>(); E point = data[0]; int idx = 0; double maxDist = -1; for (int i = 1; i < data.length; i++) { DistanceSet set = new DistanceSet(i); double dist = distance.d(point, data[i]); set.dist.add(dist); pointSet.add(set); if (dist > maxDist) { maxDist = dist; } } root = batchInsert(idx, getScale(maxDist), getScale(maxDist), pointSet, consumedSet); }
@Override public void range(T q, double radius, List<Neighbor<T,T>> neighbors) { if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } for (int i = 0; i < data.length; i++) { if (q == data[i] && identicalExcluded) { continue; } double d = distance.d(q, data[i]); if (d <= radius) { neighbors.add(new SimpleNeighbor<>(data[i], i, d)); } } } }
double f = rbf[i].f(distance.d(x, centers[i])); sum += f; for (int j = 0; j < k; j++) {
for (int i = 0; i < k; i++) { for (int j = 0; j < i; j++) { double d = distance.d(centers[i], centers[j]); if (r0 < d) { r0 = d;