📄 algorithmpca2.java,v
字号:
B.initMatrix(Y, 1, 2); // transform the pixel and mean point to the // feature space // Matrix C = new Matrix(); Matrix D = new Matrix(); // use the transformation matrix of the // first data set // if (set1_d.size() > 0 && set1flag) { set1flag = false; target = 0; A.multMatrix(PCA1_d, C); B.multMatrix(PCA1_d, D); } // use the transformation matrix of the // second data set // else if (set2_d.size() > 0 && set2flag) { set2flag = false; target = 1; A.multMatrix(PCA2_d, C); B.multMatrix(PCA2_d, D); } // use the transformation matrix of the // third data set // else if (set3_d.size() > 0 && set3flag) { set3flag = false; target = 2; A.multMatrix(PCA3_d, C); B.multMatrix(PCA3_d, D); } // use the transformation matrix of the // forth data set // else { target = 3; A.multMatrix(PCA4_d, C); B.multMatrix(PCA4_d, D); } // find the distance between the pixel and // mean point // dist = MathUtil.distance(C.Elem[0][0], C.Elem[0][1], D.Elem[0][0], D.Elem[0][1]); if (dist < smallestSoFar) { associated = target; smallestSoFar = dist; } } // put and entry in the output canvas array to // indicate which class the current pixel is // closest to // output_canvas_d[i][j] = associated; // add a point to the vector of decision // region points if the class that the current // point is associated with is different for // the class what the previous point was // associated with i.e., a transition point // if (j > 0 && i > 0) { if (associated != output_canvas_d[i][j - 1] || associated != output_canvas_d[i - 1][j]) { decision_regions_d.add(pixel); } } } } // end of the loop } /** * * Computes the classification error for the data points given * */ public void computeErrors() { // declare local variables // String text; double error; int samples = 0; int samples1 = 0; int samples2 = 0; int samples3 = 0; int samples4 = 0; int incorrect = 0; int incorrect1 = 0; int incorrect2 = 0; int incorrect3 = 0; int incorrect4 = 0; DisplayScale scale = output_panel_d.disp_area_d.getDisplayScale(); // set scales int outputWidth = output_panel_d.disp_area_d.getXPrecision(); int outputHeight = output_panel_d.disp_area_d.getYPrecision(); double incrementY = (scale.ymax-scale.ymin)/outputHeight; double incrementX = (scale.xmax-scale.xmin)/outputWidth; // compute the classification error for the first set // for (int i = 0; i < set1_d.size(); i++) { MyPoint point = (MyPoint)set1_d.elementAt(i); samples1++; if ((point.x > scale.xmin && point.x < scale.xmax) && (point.y > scale.ymin && point.y < scale.ymax)) { if (output_canvas_d[(int)((point.x - scale.xmin) / incrementX)] [(int)((point.y - scale.ymin) / incrementY)] != 0) { incorrect1++; } } } if (set1_d.size() > 0) { error = ((double)incorrect1 / (double)samples1) * 100.0; text = new String( " Results for class 0:\n" + " Total number of samples: " + samples1 + "\n" + " Misclassified samples: " + incorrect1 + "\n" + " Classification error: " + MathUtil.setDecimal(error, 2) + "%"); pro_box_d.appendMessage(text); } // compute the classification error for the second set // for (int i = 0; i < set2_d.size(); i++) { MyPoint point = (MyPoint)set2_d.elementAt(i); samples2++; if ((point.x > scale.xmin && point.x < scale.xmax) && (point.y > scale.ymin && point.y < scale.ymax)) { if (output_canvas_d[(int)((point.x - scale.xmin) / incrementX)] [(int)((point.y - scale.ymin) / incrementY)] != 1) { incorrect2++; } } } if (set2_d.size() > 0) { error = ((double)incorrect2 / (double)samples2) * 100.0; text = new String( " Results for class 1:\n" + " Total number of samples: " + samples2 + "\n" + " Misclassified samples: " + incorrect2 + "\n" + " Classification error: " + MathUtil.setDecimal(error, 2) + "%"); pro_box_d.appendMessage(text); } // compute the classification error for the third set // for (int i = 0; i < set3_d.size(); i++) { MyPoint point = (MyPoint)set3_d.elementAt(i); samples3++; if ((point.x > scale.xmin && point.x < scale.xmax) && (point.y > scale.ymin && point.y < scale.ymax)) { if (output_canvas_d[(int)((point.x - scale.xmin) / incrementX)] [(int)((point.y - scale.ymin) / incrementY)] != 2) { incorrect3++; } } } if (set3_d.size() > 0) { error = ((double)incorrect3 / (double)samples3) * 100.0; text = new String( " Results for class 2:\n" + " Total number of samples: " + samples3 + "\n" + " Misclassified samples: " + incorrect3 + "\n" + " Classification error: " + MathUtil.setDecimal(error, 2) + "%"); pro_box_d.appendMessage(text); } // compute the classification error for the forth set // for (int i = 0; i < set4_d.size(); i++) { MyPoint point = (MyPoint)set4_d.elementAt(i); samples4++; if ((point.x > scale.xmin && point.x < scale.xmax) && (point.y > scale.ymin && point.y < scale.ymax)) { if (output_canvas_d[(int)((point.x - scale.xmin) / incrementX)] [(int)((point.y - scale.ymin) / incrementY)] != 3) { incorrect4++; } } } if (set4_d.size() > 0) { error = ((double)incorrect4 / (double)samples4) * 100.0; text = new String( " Results for class 3:\n" + " Total number of samples: " + samples4 + "\n" + " Misclassified samples: " + incorrect4 + "\n" + " Classification error: " + MathUtil.setDecimal(error, 2) + "%"); pro_box_d.appendMessage(text); } // compute the overall classification error // samples = samples1 + samples2 + samples3 + samples4; incorrect = incorrect1 + incorrect2 + incorrect3 + incorrect4; error = ((double)incorrect / (double)samples) * 100.0; text = new String( " Overall results:\n" + " Total number of samples: " + samples + "\n" + " Misclassified samples: " + incorrect + "\n" + " Classification error: " + MathUtil.setDecimal(error, 2) + "%"); pro_box_d.appendMessage(text); } /** * Appends messages to the pro_box_d variable */ public void printMatrices() { double a11, a12, a21, a22; String text; if (set1_d.size() > 0) { pro_box_d.appendMessage(" Set 1"); a11 = MathUtil.setDecimal(CPCA1_d.Elem[0][0], 2); a12 = MathUtil.setDecimal(CPCA1_d.Elem[0][1], 2); a21 = MathUtil.setDecimal(CPCA1_d.Elem[1][0], 2); a22 = MathUtil.setDecimal(CPCA1_d.Elem[1][1], 2); text = new String(" Covariance matrix:\n" + " " + a11 + " " + a12 + "\n" + " " + a21 + " " + a22); pro_box_d.appendMessage(text + "\n"); a11 = MathUtil.setDecimal(PCA1_d.Elem[0][0], 2); a12 = MathUtil.setDecimal(PCA1_d.Elem[0][1], 2); a21 = MathUtil.setDecimal(PCA1_d.Elem[1][0], 2); a22 = MathUtil.setDecimal(PCA1_d.Elem[1][1], 2); text = new String(" Transformation matrix:\n" + " " + a11 + " " + a12 + "\n" + " " + a21 + " " + a22); pro_box_d.appendMessage(text + "\n"); } if (set2_d.size() > 0) { pro_box_d.appendMessage(" Set 2"); a11 = MathUtil.setDecimal(CPCA2_d.Elem[0][0], 2); a12 = MathUtil.setDecimal(CPCA2_d.Elem[0][1], 2); a21 = MathUtil.setDecimal(CPCA2_d.Elem[1][0], 2); a22 = MathUtil.setDecimal(CPCA2_d.Elem[1][1], 2); text = new String(" Covariance matrix:\n" + " " + a11 + " " + a12 + "\n" + " " + a21 + " " + a22); pro_box_d.appendMessage(text + "\n"); a11 = MathUtil.setDecimal(PCA2_d.Elem[0][0], 2); a12 = MathUtil.setDecimal(PCA2_d.Elem[0][1], 2); a21 = MathUtil.setDecimal(PCA2_d.Elem[1][0], 2); a22 = MathUtil.setDecimal(PCA2_d.Elem[1][1], 2); text = new String(" Transformation matrix:\n" + " " + a11 + " " + a12 + "\n" + " " + a21 + " " + a22); pro_box_d.appendMessage(text + "\n"); } if (set3_d.size() > 0) { pro_box_d.appendMessage(" Set 3"); a11 = MathUtil.setDecimal(CPCA3_d.Elem[0][0], 2); a12 = MathUtil.setDecimal(CPCA3_d.Elem[0][1], 2); a21 = MathUtil.setDecimal(CPCA3_d.Elem[1][0], 2); a22 = MathUtil.setDecimal(CPCA3_d.Elem[1][1], 2); text = new String(" Covariance matrix:\n" + " " + a11 + " " + a12 + "\n" + " " + a21 + " " + a22); pro_box_d.appendMessage(text + "\n"); a11 = MathUtil.setDecimal(PCA3_d.Elem[0][0], 2); a12 = MathUtil.setDecimal(PCA3_d.Elem[0][1], 2); a21 = MathUtil.setDecimal(PCA3_d.Elem[1][0], 2); a22 = MathUtil.setDecimal(PCA3_d.Elem[1][1], 2); text = new String(" Transformation matrix:\n" + " " + a11 + " " + a12 + "\n" + " " + a21 + " " + a22); pro_box_d.appendMessage(text + "\n"); } if (set4_d.size() > 0) { pro_box_d.appendMessage(" Set 4"); a11 = MathUtil.setDecimal(CPCA4_d.Elem[0][0], 2); a12 = MathUtil.setDecimal(CPCA4_d.Elem[0][1], 2); a21 = MathUtil.setDecimal(CPCA4_d.Elem[1][0], 2); a22 = MathUtil.setDecimal(CPCA4_d.Elem[1][1], 2); text = new String(" Covariance matrix:\n" + " " + a11 + " " + a12 + "\n" + " " + a21 + " " + a22); pro_box_d.appendMessage(text + "\n"); a11 = MathUtil.setDecimal(PCA4_d.Elem[0][0], 2); a12 = MathUtil.setDecimal(PCA4_d.Elem[0][1], 2); a21 = MathUtil.setDecimal(PCA4_d.Elem[1][0], 2); a22 = MathUtil.setDecimal(PCA4_d.Elem[1][1], 2); text = new String(" Transformation matrix:\n" + " " + a11 + " " + a12 + "\n" + " " + a21 + " " + a22); pro_box_d.appendMessage(text + "\n"); } }}@1.5log@fixed javadoc errors.@text@d7 1d36 2a37 2 Vector support_vectors_d = new Vector(); Vector decision_regions_d = new Vector();d60 3a62 3 support_vectors_d = new Vector(); point_means_d = new Vector(); decision_regions_d = new Vector();d106 9a114 4 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();d118 2a119 2 support_vectors_d = new Vector(); decision_regions_d = new Vector();@1.4log@minor changes@text@d2 2a3 2 * AlgorithmPCA2.java last edited Sanjay V6.0 * @@(#) AlgorithmPCA2.java 1.10 02/09/03d12 3d40 3a42 1 // classification functionsd44 9d128 5a132 3 // the Runnable method //d170 1d208 3a210 1 * step one of the algorithmd246 1a246 1 * step one of the algorithmd248 1d278 2a279 2 /** *d1416 3a1418 1 @1.3log@comments modified to comply with Java Documentation Style.And made changes in for loop and If statements to suit IFC style.@text@d2 1a4 1 * a11 6/** * This interface is designed to be the base for all algorithms * ..... * ..... * @@version 1.00 */a13 1d42 3a44 1 //System.out.println("AlgorithmPCA2 : initialize()");d120 2a121 1 //System.out.println(algo_id + ": run()");a150 4 * method: step1 * * @@param none * @@return noned159 2a160 1 //System.out.println(algo_id + ": step1()");d169 1a186 1 // Debuga189 4 * method: step2 * * @@param: none * @@return : noned197 3a199 3 //System.out.println(algo_id + " : step2()"); //sequentialMinimalOptimization(); d211 2a212 1 output_panel_d.addOutput(point_means_d, Classify.PTYPE_OUTPUT_LARGE, Color.black);d216 2a217 1 output_panel_d.addOutput(support_vectors_d, Classify.PTYPE_INPUT, Color.cyan);a225 4 * method: step3 * * @@param: none * @@return : noned233 2a234 1 //System.out.println(algo_id + " : step3()");d250 2a251 1 output_panel_d.addOutput(decision_regions_d, Classify.PTYPE_INPUT, new Color(255, 200, 0));a252 2 //Color.black); //pro_box_d.setProgressCurr(20); d259 2a260 7 * method transformPCA2 * * @@param Data d: input data point * * @@return none * * this method transforms a given set of points to a new spaced267 2a268 1 //System.out.println(algo_id + " : transformPCA2()");d588 1d597 1d691 1d751 2a752 1 theta = Math.atan2((alpha - Math.sqrt((alpha * alpha) + (beta * beta))), beta);d756 2a757 1 theta = Math.atan2((alpha + Math.sqrt((alpha * alpha) + (beta * beta))), beta);d785 4a788 2 xval = (supp.Elem[0][0] * Math.cos(theta)) - (supp.Elem[1][0] * Math.sin(theta)); yval = (supp.Elem[0][0] * Math.sin(theta)) + (supp.Elem[1][0] * Math.cos(theta));d815 2a816 1 theta = Math.atan2((alpha - Math.sqrt((alpha * alpha) + (beta * beta))), beta);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -