protected void computeDistances(double[] fullQuery, BoundedPriorityQueue<IntDoublePair> queue, IntDoublePair wp) { final double[][] distances = new double[pq.assigners.length][]; for (int j = 0, from = 0; j < this.pq.assigners.length; j++) { final DoubleNearestNeighbours nn = this.pq.assigners[j]; final int to = nn.numDimensions(); final int K = nn.size(); final double[][] qus = { Arrays.copyOfRange(fullQuery, from, from + to) }; final int[][] idx = new int[1][K]; final double[][] dst = new double[1][K]; nn.searchKNN(qus, K, idx, dst); distances[j] = new double[K]; for (int k = 0; k < K; k++) { distances[j][idx[0][k]] = dst[0][k]; } from += to; } for (int i = 0; i < data.length; i++) { wp.first = i; wp.second = 0; for (int j = 0; j < this.pq.assigners.length; j++) { final int centroid = this.data[i][j] + 128; wp.second += distances[j][centroid]; } wp = queue.offerItem(wp); } }
@Override public int[][] assign(double[][] data) { int [][] indices = new int [data.length][numNeighbours]; double [][] distances = new double [data.length][numNeighbours]; nn.searchKNN(data, numNeighbours, indices, distances); return indices; }
@Override public Boolean call() { try { int D = nno.numDimensions(); double [][] points = new double[stopRow-startRow][D]; ds.getData(startRow, stopRow, points); int [] argmins = new int[points.length]; double [] mins = new double[points.length]; nno.searchNN(points, argmins, mins); synchronized(centroids_accum){ for (int i=0; i < points.length; ++i) { int k = argmins[i]; for (int d=0; d < D; ++d) { centroids_accum[k][d] += points[i][d]; } counts[k] += 1; } } } catch(Exception e) { e.printStackTrace(); } return true; } }
@Override public int size() { return nn.size(); } }
@Override public int numDimensions() { return nn.numDimensions(); }
int ci = cur_inds[i]; if (!seen[ci]) { DoubleNearestNeighbours.distanceFunc(qu, new double[][] {pnts[ci]}, dsq);
@Override public void assignWeighted(double[][] data, int[][] assignments, double[][] weights) { nn.searchKNN(data, numNeighbours, assignments, weights); }
int ci = cur_inds[i]; if (!seen[ci]) { DoubleNearestNeighbours.distanceFunc(qu, new double[][] {pnts[ci]}, dsq);
protected void computeDistances(double[] fullQuery, BoundedPriorityQueue<IntDoublePair> queue, IntDoublePair wp) { final double[][] distances = new double[pq.assigners.length][]; for (int j = 0, from = 0; j < this.pq.assigners.length; j++) { final DoubleNearestNeighbours nn = this.pq.assigners[j]; final int to = nn.numDimensions(); final int K = nn.size(); final double[][] qus = { Arrays.copyOfRange(fullQuery, from, from + to) }; final int[][] idx = new int[1][K]; final double[][] dst = new double[1][K]; nn.searchKNN(qus, K, idx, dst); distances[j] = new double[K]; for (int k = 0; k < K; k++) { distances[j][idx[0][k]] = dst[0][k]; } from += to; } for (int i = 0; i < data.length; i++) { wp.first = i; wp.second = 0; for (int j = 0; j < this.pq.assigners.length; j++) { final int centroid = this.data[i][j] + 128; wp.second += distances[j][centroid]; } wp = queue.offerItem(wp); } }
@Override public List<IntDoublePair> regionQuery(int index) { List<IntDoublePair> res = nn.searchKNN(data[index], data.length); List<IntDoublePair> ret = new ArrayList<IntDoublePair>(); for (IntDoublePair intFloatPair : res) { if(intFloatPair.second<eps)ret.add(intFloatPair); else break; } return ret; }
/** * Static method to find a distance between * a query vector and a point. * * @param distance the distance measure * @param qu The query vector. * @param pnt The point to compare against. * @return The resultant distance. */ public static double distanceFunc(final DoubleFVComparator distance, final double [] qu, final double [] pnt) { if (distance == null) { return distanceFunc(qu, pnt); } if (distance.isDistance()) { return (double) distance.compare(qu, pnt); } else { return - (double) distance.compare(qu, pnt); } }
final int to = nn.numDimensions(); final int K = nn.size(); nn.searchKNN(qus, K, idx, dst);
@Override public IndependentPair<int[], double[]> assignWeighted(double[] data) { int [][] indices = new int [data.length][numNeighbours]; double [][] distances = new double [data.length][numNeighbours]; nn.searchKNN(new double[][] { data }, numNeighbours, indices, distances); return new IndependentPair<int[], double[]>(indices[0], distances[0]); }
/** * Static method to find a distance between * a query vector and a point. * * @param distance the distance measure * @param qu The query vector. * @param pnt The point to compare against. * @return The resultant distance. */ public static double distanceFunc(final DoubleFVComparator distance, final double [] qu, final double [] pnt) { if (distance == null) { return distanceFunc(qu, pnt); } if (distance.isDistance()) { return (double) distance.compare(qu, pnt); } else { return - (double) distance.compare(qu, pnt); } }
final int to = nn.numDimensions(); final int K = nn.size(); nn.searchKNN(qus, K, idx, dst);
/** * Classify the given image with the nearest neighbour classifier. * * @param img the character image to classify * @return The classified character. */ public char classify( FImage img ) { // Create the input vector double[][] v = new double[1][]; v[0] = getImageVector( img ); // Setup the output variables int k = 1; int[][] indices = new int[1][k]; double[][] dists = new double[1][k]; // Do the search nn.searchKNN( v, k, indices, dists ); // Work out what character the best index represents System.out.println( "Best index: "+indices[0][0] ); char c = (char)(48+indices[0][0]/nExamplesPerClass); System.out.println( "So, I think it's "+c ); return c; }
/** * Static method to find a distance between * a query vector and each of a set of points. Results are stored * in the dsq_out array, much must have the same length as the number * of points. * @param distance the distance measure * @param qu The query vector. * @param pnts The points to compare against. * @param dsq_out The resultant distances. */ public static void distanceFunc(final DoubleFVComparator distance, final double [] qu, final double [][] pnts, double [] dsq_out) { if (distance == null) { distanceFunc(qu, pnts, dsq_out); return; } final int N = pnts.length; if (distance.isDistance()) { for (int n=0; n < N; ++n) { dsq_out[n] = (double) distance.compare(qu, pnts[n]); } } else { for (int n=0; n < N; ++n) { dsq_out[n] = - (double) distance.compare(qu, pnts[n]); } } }
dnn.searchKNN( query, k, indexes, distances );
/** * Static method to find a distance between * a query vector and each of a set of points. Results are stored * in the dsq_out array, much must have the same length as the number * of points. * @param distance the distance measure * @param qu The query vector. * @param pnts The points to compare against. * @param dsq_out The resultant distances. */ public static void distanceFunc(final DoubleFVComparator distance, final double [] qu, final double [][] pnts, double [] dsq_out) { if (distance == null) { distanceFunc(qu, pnts, dsq_out); return; } final int N = pnts.length; if (distance.isDistance()) { for (int n=0; n < N; ++n) { dsq_out[n] = (double) distance.compare(qu, pnts[n]); } } else { for (int n=0; n < N; ++n) { dsq_out[n] = - (double) distance.compare(qu, pnts[n]); } } }