📄 algorithmkmeans.java,v
字号:
{
// Debug
//
// System.out.println(algo_id + ": initialize()");
scale = output_panel_d.disp_area_d.getDisplayScale();
data_pool_d = new Vector();
decision_regions_d = new DecisionRegion();
point_means_d = new Vector();
// support_vectors_d = new Vector();
iterations = Classify.main_menu_d.iterations;
numguesses = Classify.main_menu_d.clusters;
description_d = new Vector();
step_count = 3;
currObject = 0;
// add the process description for the LBG algorithm
//
if (description_d.size() == 0)
{
String str = new String(" 0. Initialize the original data.");
description_d.addElement(str);
str = new String(" 1. Displaying the original data.");
description_d.addElement(str);
str = new String(" 2. Computing the means for each data set.");
description_d.addElement(str);
str = new String(" 3. Stepping through iterations ");
description_d.addElement(str);
str = new String("Iteration");
description_d.addElement(str);
}
d63 3a65 25 // append message to process box
//
pro_box_d.appendMessage("KMeans Analysis:" + "\n");
// set the data points for this algorithm
//
set1_d = (Vector)data_points_d.dset1.clone();
set2_d = (Vector)data_points_d.dset2.clone();
set3_d = (Vector)data_points_d.dset3.clone();
set4_d = (Vector)data_points_d.dset4.clone();
// advance to step 1
//
step_index_d = 0;
// append message to process box
//
pro_box_d.appendMessage((String)description_d.get(step_index_d));
// exit gracefully
//
return false;
}
boolean step1()
d67 2a68 9 // Debug
//
// System.out.println(algo_id + ": step1()");
// set up progress bar
//
pro_box_d.setProgressMin(0);
pro_box_d.setProgressMax(1);
pro_box_d.setProgressCurr(0);
d70 2a71 12 scaleToFitData();
// Display original data
//
output_panel_d.addOutput(set1_d, Classify.PTYPE_INPUT,
data_points_d.color_dset1);
output_panel_d.addOutput(set2_d, Classify.PTYPE_INPUT,
data_points_d.color_dset2);
output_panel_d.addOutput(set3_d, Classify.PTYPE_INPUT,
data_points_d.color_dset3);
output_panel_d.addOutput(set4_d, Classify.PTYPE_INPUT,
data_points_d.color_dset4);
d73 2a74 4 // step 1 completed
//
pro_box_d.setProgressCurr(1);
output_panel_d.repaint();
d76 2d79 2a80 1 return true;
d82 24d107 63a169 5 boolean step2()
{
// Debug
//
// System.out.println(algo_id + ": step2()");
d171 5a175 15 computeMeans();
generatePool();
generateMeans(numguesses);
initializeKmeans();
output_panel_d.addOutput(guesses,
Classify.PTYPE_OUTPUT_LARGE,
Color.black);
output_panel_d.repaint();
return true;
}
d181 1a181 1
d184 2a185 1 pro_box_d.appendMessage(" Iteration " + (currObject+1) + "\n");
d190 1a190 1
d304 4a313 1
d338 3a342 1
d371 1a371 1 // add all points from the forth data set
a379 1 *
a382 1 *
a385 1
d415 1a415 2 {
d453 1a453 2 /**
*
d456 2a457 2 * @@param DecisionRegion region - stored data sets from the classification
*
a460 1
a506 1 *
d509 2a510 2 * @@param DecisionRegion region - classified data sets
*
a513 1
a545 1 *
d551 2a552 2 *
*/
a554 1
a561 1
d636 11a646 1
a818 1 *
a823 1 *
d985 3a987 1
@1.5log@some minor changes@text@d351 1a351 1 * method generates random initial guesses (means) for the data set
d384 1a384 1 * method to initialize the kmean array with the original data sets
d429 1a429 1 * method classifies the data sets based on the k-means iterative algorithm
d484 1a484 1 * method computes the means of the data sets after each iteraion
d525 1a525 1 * method determines the closest data sets to the cluster
d791 1a791 1 * computes the k-mean decision region - nearest neighbor algorithm
@1.4log@changed a comment from * to // style.@text@d1 9a11 3/*
* Created on Jul 15, 2003
*/
a12 4/**
* @@author Phil Trasatti
*
*/
d37 2a38 1 //System.out.println(algo_id + ": initialize()");
d45 1a45 1 //support_vectors_d = new Vector();
d54 1d74 1d78 1d85 1d89 1d93 1d100 2a101 1 //System.out.println(algo_id + ": step1()");
d104 1d112 1d123 1d134 2a135 1 //System.out.println(algo_id + ": step2()");
d155 2a156 1 //System.out.println(algo_id + ": step3()");
d185 2a186 1 for (int i=0; i<numclusters; i++) {
d212 2a213 1 for (int j=0; j<cluster.size(); j++) {
d241 4a244 4 " " + c11
+ " " + c12 + "\n" +
" " + c21
+ " " + c22);
d255 1a255 2 error += (double)displayClusterError(closest,
cluster, i);
d263 4a266 4 " Total number of samples: " +
total + "\n" +
" Misclassified samples: " +
error + "\n" +
d282 2a283 1 //System.out.println(algo_id + ": run()");
a291 1
a297 1
d310 2a311 1 public void generatePool() {
d315 2a316 1 if (data_pool_d.size() > 0) {
d322 2a323 1 for (int i=0; i<set1_d.size(); i++) {
d329 2a330 1 for (int i=0; i<set2_d.size(); i++) {
d336 2a337 1 for (int i=0; i<set3_d.size(); i++) {
d343 2a344 1 for (int i=0; i<set4_d.size(); i++) {
d350 8a357 9 * method: generateMeans
*
* @@param numMeans number of mean points
*
* @@return none
*
* method generates random initial guesses (means) for the data set
*/
public void generateMeans(int numMeans) {
d383 7a389 9 * method: initializeKmeans
*
* @@param none
* @@return none
*
* method to initialize the kmean array with the original data sets
*
public void initializeKmeans() {
d392 2a393 1 if (set1_d.size() > 0) {
d399 2a400 1 if (set2_d.size() > 0) {
d406 2a407 1 if (set3_d.size() > 0) {
d413 2a414 1 if (set4_d.size() > 0) {
d424 1a424 1 //kmeans.addElement(region);
d428 8a435 10 * method: classify
*
* @@param DecisionRegion region - stored data sets from the classification
*
* @@return none
*
* method classifies the data sets based on the k-means iterative algorithm
*
*/
public void classify(DecisionRegion region) {
d439 2a440 1 for (int i=0; i<data_pool_d.size(); i++) {
d454 2a455 1 for (int j=0; j<guesses.size(); j++) {
d467 2a468 1 if(dist < smallestSoFar) {
d483 2a484 1 * method: computeMeans
a487 4 * @@return none
*
* method computes the means of the data sets after each iteraion
*
d489 2a490 1 public void computeMeans(DecisionRegion region) {
d502 2a503 1 for (int i=0; i<numsets; i++) {
d524 2a525 1 * method: getClosestSet
a530 2 * method determines the closest data sets to the cluster
*
d532 2a533 1 public int getClosestSet(MyPoint mean) {
d556 1a556 1 mean1.x, mean1.y);
d566 1a566 1 mean2.x, mean2.y);
d576 1a576 1 mean3.x, mean3.y);
d586 1a586 1 mean4.x, mean4.y);
d593 2a594 1 if (dist1 < dist2 && dist1 < dist3 && dist1 < dist4) {
d600 2a601 1 if (dist2 < dist1 && dist2 < dist3 && dist2 < dist4) {
d607 2a608 1 if (dist3 < dist1 && dist3 < dist2 && dist3 < dist4) {
d618 2a619 1 public int displayClusterError(int closest, Vector cluster, int id) {
d630 4a633 3 if (closest == 1) {
for (int i=0; i<cluster.size(); total++, i++) {
d643 2a644 1 for (int j=0; j<set1_d.size(); j++) {
d652 2a653 1 if (cst.x == pnt.x && cst.y == pnt.y) {
d660 2a661 1 if (!present) {
d669 4a672 3 if (closest == 2) {
for (int i=0; i<cluster.size(); total++, i++) {
d682 2a683 1 for (int j=0; j<set2_d.size(); j++) {
d691 2a692 1 if (cst.x == pnt.x && cst.y == pnt.y) {
d699 2a700 1 if (!present) {
d708 4a711 3 if (closest == 3) {
for (int i=0; i<cluster.size(); total++, i++) {
d721 2a722 1 for (int j=0; j<set3_d.size(); j++) {
d730 2a731 1 if (cst.x == pnt.x && cst.y == pnt.y) {
d738 2a739 1 if (!present) {
d747 4a750 3 if (closest == 4) {
for (int i=0; i<cluster.size(); total++, i++) {
d760 2a761 1 for (int j=0; j<set4_d.size(); j++) {
d769 2a770 1 if (cst.x == pnt.x && cst.y == pnt.y) {
d777 2a778 1 if (!present) {
d790 2a791 1 * method: getDecisionRegion
d793 1a793 1 * @@param vec vector of initial guesses
a796 2 * computes the k-mean decision region - nearest neighbor algorithm
*
d798 2a799 1 public Vector getDecisionRegion(Vector vec) {
d816 2a817 2 double incrementY = (scale.ymax-scale.ymin)/outputHeight;
double incrementX = (scale.xmax-scale.xmin)/outputWidth;
d828 2a829 1 for(int i = 0; i < outputWidth; i++) {
d836 2a837 1 for(int j = 0; j < outputHeight; j++) {
d845 2a846 1 for (int k = 0; k < vec.size(); k++) {
d851 2a852 1 dist = MathUtil.distance(pixel.x, pixel.y, point.x, point.y);
d854 2a855 1 if(dist < smallestSoFar) {
d862 4a865 2 if(set1_d.size() > 0) {
MyPoint mean1 = (MyPoint)point_means_d.elementAt(l);
d868 3a870 2 mean1.x, mean1.y);
if(cdist < smallest) {
d879 4a882 2 if(set2_d.size() > 0) {
MyPoint mean2 = (MyPoint)point_means_d.elementAt(l);
d885 3a887 2 mean2.x, mean2.y);
if(cdist < smallest) {
d896 4a899 2 if(set3_d.size() > 0) {
MyPoint mean3 = (MyPoint)point_means_d.elementAt(l);
d902 3a904 2 mean3.x, mean3.y);
if(cdist < smallest) {
d913 4a916 2 if(set4_d.size() > 0) {
MyPoint mean4 = (MyPoint)point_means_d.elementAt(l);
d919 3a921 2 mean4.x, mean4.y);
if(cdist < smallest) {
d945 2a946 1 associated != outputCanvas[i - 1][j]) {
d958 1a982 1
@1.3log@changed the comments to comply with Java Documentation.But, still needs to get it verified. A lot needs to be done@text@d513 1a513 1 * declare local variables
@1.2log@Again debug statements commented out.@text@d29 1a29 1 /* (non-Javadoc)
d328 9a336 9 // method: generateMeans
//
// arguments:
// int numMeans: number of mean points
//
// return : none
//
// method generates random initial guesses (means) for the data set
//
d362 8a369 7 // method: initializeKmeans
//
// arguments: none
// return : none
//
// method to initialize the kmean array with the original data sets
//
d405 10a414 9 // method: classify
//
// arguments:
// DecisionRegion region: stored data sets from the classification
//
// return : none
//
// method classifies the data sets based on the k-means iterative algorithm
//
d458 11a468 9 // method: computeMeans
//
// arguments:
// DecisionRegion region: classified data sets
//
// return : none
//
// method computes the means of the data sets after each iteraion
//
d501 10a510 9 // method: getClosestSet
//
// arguments:
// Point mean: mean of the cluster
//
// return : closest data set to the cluster
//
// method determines the closest data sets to the cluster
//
d513 1a513 1 // declare local variables
d747 10a756 9 // method: getDecisionRegion
//
// arguments:
// Vector vec: vector of initial guesses
//
// return : vector of desision region points
//
// computes the k-mean decision region - nearest neighbor algorithm
//
d925 1@1.1log@Initial revision@text@d35 1a35 1 System.out.println(algo_id + ": initialize()");
d91 1a91 1 System.out.println(algo_id + ": step1()");
d121 1a121 1 System.out.println(algo_id + ": step2()");
d141 1a141 1 System.out.println(algo_id + ": step3()");
d266 1a266 1 System.out.println(algo_id + ": run()");
@
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -