@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 void assignWeighted(double[][] data, int[][] assignments, double[][] weights) { nn.searchKNN(data, numNeighbours, assignments, weights); }
@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; }
@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]); }
/** * 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; }
dnn.searchKNN( query, k, indexes, distances );
final int[][] idx = new int[1][K]; final double[][] dst = new double[1][K]; nn.searchKNN(qus, K, idx, dst);
final int[][] idx = new int[1][K]; final double[][] dst = new double[1][K]; nn.searchKNN(qus, K, idx, dst);
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); } }
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); } }