📄 algorithmpca.java,v
字号:
currentX += incrementX; currentY = scale.ymin; pro_box_d.setProgressCurr(i); for (int j = 0; j < outputHeight; j++) { // declare the current pixel point // currentY += incrementY; MyPoint pixel = new MyPoint(currentX, currentY); smallestSoFar = Double.MAX_VALUE; // convert the pixel to the time domain // double X[][] = new double[1][2]; X[0][0] = pixel.x; X[0][1] = pixel.y; // reset the boolean flags // set1flag = true; set2flag = true; set3flag = true; // find the closest point from the first class // for (int k = 0; k < point_means_d.size(); k++) { // classify the sample to the first set // if (set1_d.size() > 0 && set1flag) { set1flag = false; target = 0; } // classify the sample to the second set // else if (set2_d.size() > 0 && set2flag) { set2flag = false; target = 1; } // classify the sample to the third set // else if (set3_d.size() > 0 && set3flag) { set3flag = false; target = 2; } // classify the sample to the forth set // else { target = 3; } // get the first mean point // point = (MyPoint)point_means_d.elementAt(k); // convert the mean point to the time domain // double Y[][] = new double[1][2]; Y[0][0] = point.x; Y[0][1] = point.y; // represent the pixel as a matrix // Matrix A = new Matrix(); A.initMatrix(X, 1, 2); // represent the mean point as a matrix // Matrix B = new Matrix(); B.initMatrix(Y, 1, 2); // transform the pixel and mean point to the // feature space // Matrix C = new Matrix(); Matrix D = new Matrix(); A.multMatrix(trans_matrix_d, C); B.multMatrix(trans_matrix_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 number of data points in classification error * */ 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; a11 = MathUtil.setDecimal(cov_matrix_d.Elem[0][0], 2); a12 = MathUtil.setDecimal(cov_matrix_d.Elem[0][1], 2); a21 = MathUtil.setDecimal(cov_matrix_d.Elem[1][0], 2); a22 = MathUtil.setDecimal(cov_matrix_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(trans_matrix_d.Elem[0][0], 2); a12 = MathUtil.setDecimal(trans_matrix_d.Elem[0][1], 2); a21 = MathUtil.setDecimal(trans_matrix_d.Elem[1][0], 2); a22 = MathUtil.setDecimal(trans_matrix_d.Elem[1][1], 2); text = new String(" Transformation matrix:\n" + " " + a11 + " " + a12 + "\n" + " " + a21 + " " + a22); pro_box_d.appendMessage(text + "\n"); }}@1.6log@fixed spelling of String on line 709.@text@d10 1d32 2a33 2 Vector support_vectors_d = new Vector(); Vector decision_regions_d = new Vector();d58 3a60 3 support_vectors_d = new Vector(); point_means_d = new Vector(); decision_regions_d = new Vector();d85 1a85 1 d88 9a96 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();@1.5log@Fixed javadoc errors.@text@d709 1a709 1 Sting text;@1.4log@minor changes.@text@d2 2a3 2 * @@(#) AlgorithmPCA.java v6.0 03/15/2005 * last edited Sanjay d15 3d35 2a36 1 // classification functionsd38 11d105 4d146 1d185 1d227 1d911 3@1.3log@Java Documentation style comments alongwith the for loops and if conditions@text@d2 5a6 1 * @@(#) AlgorithmPCA.java 1.10 02/09/03a7 1 * a14 6/** * This class defines the operation of the PCA Class-Independent Algorithm * ..... * ..... * @@version 1.00 */d37 2a38 1 //System.out.println(algo_id + ": initialize()");d49 1d66 3a68 1 pro_box_d.appendMessage("Class Independent Principal" + " Component Analysis:" + "\n");d78 1d82 1d86 1a89 2 // the Runnable method //d93 2a94 1 //System.out.println(algo_id + ": run()");a123 1 * method: step1 d125 1a125 4 * @@param none * @@return none * * step one of the algorithmd131 2a132 1 //System.out.println(algo_id + ": step1()");d141 1a161 4 * method: step2 * * @@param none * @@return noned163 1a163 1 * step two of the algorithmd184 2a185 1 output_panel_d.addOutput(point_means_d, Classify.PTYPE_OUTPUT_LARGE, Color.black);d189 2a190 1 output_panel_d.addOutput(support_vectors_d, Classify.PTYPE_INPUT, Color.cyan);a202 4 * method: step3 * * @@param none * @@return noned204 1a204 1 * step one of the algorithmd210 2a211 1 //System.out.println(algo_id + ": step3()");d229 2a230 1 output_panel_d.addOutput(decision_regions_d, Classify.PTYPE_INPUT, new Color(255, 200, 0));a231 2 //Color.black); //pro_box_d.setProgressCurr(20); d240 2a241 7 * method transformPCA * * @@param Data d: input data point * * @@return none * * this method transforms a given set of points to a new spaced248 2a249 1 //System.out.println(algo_id + ": transformPCA()");d270 2a271 1 // Since Eigen is a class of static member functions it is not correct to instantiate it - Phil T. 6-23-03 d307 2a308 1 int maxsize = set1_d.size() + set2_d.size() + set3_d.size() + set4_d.size();d449 2a450 1 theta = Math.atan2((alpha - Math.sqrt((alpha * alpha) + (beta * beta))), beta);d454 2a455 1 theta = Math.atan2((alpha + Math.sqrt((alpha * alpha) + (beta * beta))), beta);d484 4a487 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));d502 1a502 6 * method: computeDecisionRegions * * @@param none * @@return none * * method computes the line of discrimination for the classification d509 2a510 1 //System.out.println(algo_id + ": computeDecisionRegions()");d522 2a523 2 double incrementY = (scale.ymax-scale.ymin)/outputHeight; double incrementX = (scale.xmax-scale.xmin)/outputWidth;d641 2a642 1 dist = MathUtil.distance(C.Elem[0][0], C.Elem[0][1], D.Elem[0][0], D.Elem[0][1]);d666 2a667 1 if (associated != output_canvas_d[i][j - 1] || associated != output_canvas_d[i - 1][j])a677 1 * method computeErrorsd679 1a679 5 * @@param Data d: input data point * * @@return none * * display two matricesd701 2a702 1 DisplayScale scale = output_panel_d.disp_area_d.getDisplayScale(); d704 1d708 2a709 2 double incrementY = (scale.ymax-scale.ymin)/outputHeight; double incrementX = (scale.xmax-scale.xmin)/outputWidth;a714 1 d720 2a721 1 if (output_canvas_d[(int)((point.x-scale.xmin)/incrementX)][(int)((point.y-scale.ymin)/incrementY)] != 0)d759 2a760 1 if (output_canvas_d[(int)((point.x-scale.xmin)/incrementX)][(int)((point.y-scale.ymin)/incrementY)] != 1)d798 2a799 1 if (output_canvas_d[(int)((point.x-scale.xmin)/incrementX)][(int)((point.y-scale.ymin)/incrementY)] != 2)d837 2a838 1 if (output_canvas_d[(int)((point.x-scale.xmin)/incrementX)][(int)((point.y-scale.ymin)/incrementY)] != 3)a914 1a915 1@1.2log@Alignment and "Step Sequence Complete" replace by "Algorithm Complete".All the debug outputs commented out.@text@d1 1a1 1/*a3 4 * Copyright ***, All Rights Reserved. * * This software is the proprietary information of ******** * Use is subject to license terms.d20 35a54 21 //----------------------------------------------------------------- // // instance data members // //----------------------------------------------------------------- // vector of support region points // Matrix trans_matrix_d = new Matrix(); Matrix cov_matrix_d = new Matrix(); Vector support_vectors_d = new Vector(); Vector decision_regions_d = new Vector();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -