All pastes #744661 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-10-21 18:10 UTC
rendered paste body
--- Algorithm to optimize clusters ---

Step 1: Get new centers
    // Get new center values
           for(int j=0; j < kMeans; j++)
                listOfCenters.add(makeCenter(j));

 private DataPoint makeCenter(int j){

                DataPoint center  = new DataPoint("Center");
                String currentKey = "";

                double scaledSum                = 0;
                double tempHolder               = 0;
                double currentScalar    = 0;

                /* We're going to work the bottom of the fomula first
                 *sum the weights and scale them
                 */

                for(int i=0; i < degrees.size(); i++){
                        tempHolder = Math.pow(degrees.get(i).get(j), scalar);
                        scaledSum += tempHolder;
                } // end for

                /* OK - now that we have the "bottom" of the formula we can figure out the top
                 * Apply the weight to each key in the data point
                 */
 while(e.hasMoreElements()){

                                // Get the current key
                                currentKey = e.nextElement().toString();

                                // Get the value for this key
                                tempHolder = Double.parseDouble(pointList.get(i).getData().get(currentKey).toString());
                                tempHolder = tempHolder * currentScalar;
                                tempHolder = tempHolder / scaledSum;

                                // Now put that into the hash table for center
                                        if(center.getData().containsKey(currentKey)){
                                                // If there is something there - get it back!
                                                tempHolder += Double.parseDouble(center.getData().get(currentKey).toString());
                                                center.getData().remove(currentKey);
                                                center.getData().put(currentKey, tempHolder);
                                        } // end if
                                        else
                                                center.getData().put(currentKey, tempHolder);
                        } // end while
                } // end for

                // We're done

                return center;
        } // end makeCenter