Find K nearest neighbors, starting from a distance matrix

Try to use FastKNN CRAN package (although it is not well documented). It offers k.nearest.neighbors function where an arbitrary distance matrix can be given. Below you have an example that computes the matrix you need. # arbitrary data train <- matrix(sample(c(“a”,”b”,”c”),12,replace=TRUE), ncol=2) # n x 2 n = dim(train)[1] distMatrix <- matrix(runif(n^2,0,1),ncol=n) # n x … Read more

Changing shape of single point in JFreeChart XYPLot

ChartFactory.createScatterPlot() instantiates an XYLineAndShapeRenderer. You can replace the renderer with one that lets you selectively replace the Shape returned by getItemShape(), as shown below. xyPlot.setRenderer(new XYLineAndShapeRenderer(false, true) { @Override public Shape getItemShape(int row, int col) { if (row == 0 & col == N) { return ShapeUtilities.createDiagonalCross(5, 2); } else { return super.getItemShape(row, col); } … Read more

Millions of 3D points: How to find the 10 of them closest to a given point?

Million points is a small number. The most straightforward approach works here (code based on KDTree is slower (for querying only one point)). Brute-force approach (time ~1 second) #!/usr/bin/env python import numpy NDIM = 3 # number of dimensions # read points into array a = numpy.fromfile(‘million_3D_points.txt’, sep=’ ‘) a.shape = a.size / NDIM, NDIM … Read more