public Instances kNearestNeighbours ( Instance target , int k ) throws Exception { checkMissing ( target ) ; MyHeap heap = new MyHeap ( k ) ; findNearestNeighbours ( target , m_Root , k , heap , 0.0 ) ; Instances neighbours = new Instances ( m_Instances , ( heap . size ( ) + heap . noOfKthNearest ( ) ) ) ; m_DistanceList = new double [ heap . size ( ) + heap . noOfKthNearest ( ) ] ; int [ ] indices = new int [ heap . size ( ) + heap . noOfKthNearest ( ) ] ; int i = indices . length - 1 ; MyHeapElement h ; while ( heap . noOfKthNearest ( ) > 0 ) { h = heap . getKthNearest ( ) ; indices [ i ] = h . index ; m_DistanceList [ i ] = h . distance ; i -- ; } while ( heap . size ( ) > 0 ) { h = heap . get ( ) ; indices [ i ] = h . index ; m_DistanceList [ i ] = h . distance ; i -- ; } m_DistanceFunction . postProcessDistances ( m_DistanceList ) ; for ( int idx = 0 ; idx < indices . length ; idx ++ ) { neighbours . add ( m_Instances . instance ( indices [ idx ] ) ) ; } return neighbours ; }
Returns the k nearest neighbours of the supplied instance . &gt ; k neighbours are returned if there are more than one neighbours at the kth boundary .