📄 algorithmpca2.java,v
字号:
d820 2a821 1 theta = Math.atan2((alpha + Math.sqrt((alpha * alpha) + (beta * beta))), beta);d850 4a853 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));d880 2a881 1 theta = Math.atan2((alpha - Math.sqrt((alpha * alpha) + (beta * beta))), beta);d885 2a886 1 theta = Math.atan2((alpha + Math.sqrt((alpha * alpha) + (beta * beta))), beta);d915 4a918 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));d945 2a946 1 theta = Math.atan2((alpha - Math.sqrt((alpha * alpha) + (beta * beta))), beta);d950 2a951 1 theta = Math.atan2((alpha + Math.sqrt((alpha * alpha) + (beta * beta))), beta);d980 4a983 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));d999 1a999 6 * method: computeDecisionRegions * * @@param none * @@return none * * method computes the line of discrimination for the classification d1006 2a1007 1 //System.out.println(algo_id + " : computeDecisionRegions()");d1149 2a1150 1 dist = MathUtil.distance(C.Elem[0][0], C.Elem[0][1], D.Elem[0][0], D.Elem[0][1]);d1173 2a1174 1 if (associated != output_canvas_d[i][j - 1] || associated != output_canvas_d[i - 1][j])d1185 2a1186 7 * method computeErrors * * @@param Data d: input data point * * @@return none * * display two matricesd1208 2a1209 1 DisplayScale scale = output_panel_d.disp_area_d.getDisplayScale(); d1227 2a1228 1 if (output_canvas_d[(int)((point.x-scale.xmin)/incrementX)][(int)((point.y-scale.ymin)/incrementY)] != 0)d1266 2a1267 1 if (output_canvas_d[(int)((point.x-scale.xmin)/incrementX)][(int)((point.y-scale.ymin)/incrementY)] != 1)d1305 2a1306 1 if (output_canvas_d[(int)((point.x-scale.xmin)/incrementX)][(int)((point.y-scale.ymin)/incrementY)] != 2)d1344 2a1345 1 if (output_canvas_d[(int)((point.x-scale.xmin)/incrementX)][(int)((point.y-scale.ymin)/incrementY)] != 3)a1510 1@1.2log@Algorithm Complete and debug statements commented@text@a3 4 * Copyright ***, All Rights Reserved. * * This software is the proprietary information of ******** * Use is subject to license terms.d21 36a56 1 //-----------------------------------------------------------------d58 23a80 1 // instance data membersd82 1a82 1 //-----------------------------------------------------------------d84 4a87 4 Matrix PCA1_d; Matrix PCA2_d; Matrix PCA3_d; Matrix PCA4_d;d89 4a92 4 Matrix CPCA1_d; Matrix CPCA2_d; Matrix CPCA3_d; Matrix CPCA4_d;d94 1a94 1 // vector of support region pointsd96 4a99 4 Vector support_vectors_d = new Vector(); Vector decision_regions_d = new Vector(); int output_canvas_d[][]; String algo_id = "AlgorithmPCA";d101 1a101 1 // classification functionsd103 2a104 8 public boolean initialize() { // Debug //System.out.println("AlgorithmPCA2 : initialize()"); support_vectors_d = new Vector(); point_means_d = new Vector(); decision_regions_d = new Vector(); step_count = 3;d106 3d110 3a112 6 // check the data points // if (output_panel_d == null) { return false; }d114 4a117 6 // add the process description for the PCA2 algorithm // if (description_d.size() == 0) { String str = new String(" 0. Initialize the original data."); description_d.addElement(str);a118 2 str = new String(" 1. Displaying the original data."); description_d.addElement(str);d120 45a164 2 str = new String(" 2. Computing the means and support regions."); description_d.addElement(str);d166 53a218 3 str = new String(" 3. Computing the decision regions."); description_d.addElement(str); }d220 13a232 3 // append message to process box // pro_box_d.appendMessage("Class Dependent PCA :" + "\n");d234 13a246 4 PCA1_d = null; PCA2_d = null; PCA3_d = null; PCA4_d = null;d248 19a266 4 CPCA1_d = null; CPCA2_d = null; CPCA3_d = null; CPCA4_d = null;d268 2a269 6 // 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();d271 65a335 4 // reset values // support_vectors_d = new Vector(); decision_regions_d = new Vector();d337 3a339 3 // set the step index // step_index_d = 0;d341 4a344 3 // append message to process box // pro_box_d.appendMessage((String)description_d.get(step_index_d));d346 3a348 4 // exit gracefully // return true; }d350 4d355 1a355 1 // the Runnable methodd357 2a358 31 public void run() { // Debug //System.out.println(algo_id + ": run()"); if (step_index_d == 1) { disableControl(); step1(); enableControl(); } else if (step_index_d == 2) { disableControl(); step2(); enableControl(); } else if (step_index_d == 3) { disableControl(); step3(); pro_box_d.appendMessage(" Algorithm Complete"); enableControl(); } // exit gracefully // return; }d360 2a361 1 // method: step1 d363 1a363 6 // arguments: none // return : none // // step one of the algorithm // boolean step1()d365 8a372 3 // Debug //System.out.println(algo_id + ": step1()");d374 38a411 3 pro_box_d.setProgressMin(0); pro_box_d.setProgressMax(1); pro_box_d.setProgressCurr(0);d413 9a421 1 scaleToFitData();d423 1a423 11 // 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); // step 1 completedd425 10a434 4 pro_box_d.setProgressCurr(1); output_panel_d.repaint(); // exit gracefullyd436 1a436 2 return true; // Debugd438 6a443 2 // method: step2 d445 4a448 2 // arguments: none // return : noned450 5a454 1 // step one of the algorithmd456 10a465 1 boolean step2()a466 3 // Debug //System.out.println(algo_id + " : step2()"); //sequentialMinimalOptimization();d468 5a472 3 pro_box_d.setProgressMin(0); pro_box_d.setProgressMax(20); pro_box_d.setProgressCurr(0);d474 5a478 3 transformPCA2(); printMatrices(); computeMeans();d480 10a489 4 // display means // output_panel_d.addOutput(point_means_d, Classify.PTYPE_OUTPUT_LARGE, Color.black);d491 3a493 3 // display support vectors // output_panel_d.addOutput(support_vectors_d, Classify.PTYPE_INPUT, Color.cyan);d495 4a498 2 pro_box_d.setProgressCurr(20); output_panel_d.repaint();d500 1a500 20 return true; } // method: step3 // // arguments: none // return : none // // step one of the algorithm // boolean step3() { // Debug //System.out.println(algo_id + " : step3()"); pro_box_d.setProgressMin(0); pro_box_d.setProgressMax(20); pro_box_d.setProgressCurr(0); // compute the decision regisionsd502 3a504 3 computeDecisionRegions(); // compute errorsd506 3a508 3 computeErrors(); // display support vectorsd510 5a514 1 // display support vectorsd516 10a525 1 output_panel_d.addOutput(decision_regions_d, Classify.PTYPE_INPUT, new Color(255, 200, 0));d527 3a529 5 //Color.black); //pro_box_d.setProgressCurr(20); output_panel_d.repaint(); return true;d531 4d536 1a536 1 // method transformPCA2d538 4a541 2 // arguments: // Data d: input data pointd543 5a547 1 // return : noned549 10a558 4 // this method transforms a given set of points to a new space // using the class dependent principal component analysis algorithm // public void transformPCA2()d560 12a571 2 // Debug //System.out.println(algo_id + " : transformPCA2()");d573 1a573 1 // declare local variablesd575 3a577 9 int size = 0; int xsize1 = 0; int ysize1 = 0; int xsize2 = 0; int ysize2 = 0; int xsize3 = 0; int ysize3 = 0; int xsize4 = 0; int ysize4 = 0;d579 4a582 8 double xval1 = 0.0; double yval1 = 0.0; double xval2 = 0.0; double yval2 = 0.0; double xval3 = 0.0; double yval3 = 0.0; double xval4 = 0.0; double yval4 = 0.0;d584 3a586 8 double xmean1 = 0.0; double ymean1 = 0.0; double xmean2 = 0.0; double ymean2 = 0.0; double xmean3 = 0.0; double ymean3 = 0.0; double xmean4 = 0.0; double ymean4 = 0.0;d588 6a593 4 double xval = 0.0; double yval = 0.0; // declare the covariance objectd595 3a597 19 Covariance cov = new Covariance(); // declare an eigen object // // Since Eigen is a class of static member functions it is not correct to instantiate it - Phil T. 6-23-03 //Eigen eigen = new Eigen(); // declare arrays for the eigenvalues // double eigVal1[] = null; double eigVal2[] = null; double eigVal3[] = null; double eigVal4[] = null; // declare an array to store the eigen vectors // double eigVec[] = new double[2]; // declare arrays to store the samplesd599 3a601 4 double x[] = null; double y[] = null; // get the samples from the first data setd603 3a605 25 size = set1_d.size(); // increment the variable count for the first data set // xsize1 += size; ysize1 += size; // initialize arrays to store the samples // x = new double[size]; y = new double[size]; // set up the initial random vectors i.e., the vectors of // X and Y coordinate points form the display // for (int i = 0; i < size; i++) { MyPoint p = (MyPoint)set1_d.elementAt(i); xval1 += p.x; yval1 += p.y; x[i] = p.x; y[i] = p.y; }d607 3a609 1 if (size > 0)d611 4a614 49 // declare the covariance matrix // Matrix covariance = new Matrix(); covariance.row = covariance.col = 2; covariance.Elem = new double[2][2]; // declare matrix objects // Matrix T = new Matrix(); Matrix M = new Matrix(); Matrix W = new Matrix(); // allocate memory for the matrix elements // T.Elem = new double[2][2]; M.Elem = new double[2][2]; W.Elem = new double[2][2]; // initialize the transformation matrix dimensions // W.row = 2; W.col = 2; // reset the matrices // W.resetMatrix(); // compute the covariance matrix of the first data set // covariance.Elem = cov.computeCovariance(x, y); CPCA1_d = covariance; // initialize the matrix needed to compute the eigenvalues // T.initMatrix(covariance.Elem, 2, 2); // make a copy of the original matrix // M.copyMatrix(T); // compute the eigen values // // Changed eigen to Eigen since member function is static - Phil T. 6-23-03 eigVal1 = Eigen.compEigenVal(T); // compute the eigen vectors // for (int i = 0; i < 2; i++)d616 1a616 5 Eigen.calcEigVec(M, eigVal1[i], eigVec); for (int j = 0; j < 2; j++) { W.Elem[j][i] = eigVec[j] / Math.sqrt(eigVal1[i]); }a617 4 // save the transformation matrix // PCA1_d = W;d620 1a620 1 // get the samples from the first data setd622 2a623 1 size = set2_d.size();d625 8a632 4 // increment the variable count for the second data set // xsize2 += size; ysize2 += size;d634 4a637 4 // initialize arrays to store the samples // x = new double[size]; y = new double[size];d639 11a649 11 // set up the initial random vectors i.e., the vectors of // X and Y coordinate points form the display // for (int i = 0; i < size; i++) { MyPoint p = (MyPoint)set2_d.elementAt(i); xval2 += p.x; yval2 += p.y; x[i] = p.x;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -