private void setInitialPartitions ( double [ ] [ ] pointArray , int k ) { if ( pointArray . length < k ) { System . err . println ( "cannot cluster less than k points into k clusters..." ) ; System . exit ( 0 ) ; } // select randomly k initial means // TODO: choose more stable initialization (comment: this is run several times (with different random seeds), i.e. the worst case should not occur) Random random = new Random ( randomSeed ) ; TreeSet < Integer > usedPoints = new TreeSet < Integer > ( ) ; int nextIndex ; for ( int i = 0 ; i < k ; i ++ ) { nextIndex = ( ( Double ) ( Math . floor ( random . nextDouble ( ) * n ) ) ) . intValue ( ) ; if ( usedPoints . contains ( nextIndex ) ) { i -- ; continue ; } else { for ( int d = 0 ; d < dim ; d ++ ) { clusterMeans [ i ] [ d ] = pointArray [ nextIndex ] [ d ] ; } } } // set pCgivenX=1 for the closest C for each point int minDistIndex = 0 ; double minDist , currentDist ; for ( int x = 0 ; x < pointArray . length ; x ++ ) { minDist = Double . MAX_VALUE ; for ( int i = 0 ; i < k ; i ++ ) { currentDist = euclideanDistance ( clusterMeans [ i ] , pointArray [ x ] ) ; if ( currentDist < minDist ) { minDist = currentDist ; minDistIndex = i ; } } pCgivenX [ minDistIndex ] [ x ] = 1.0 ; } }
creates an initial partitioning