📄 algorithmpca.java,v
字号:
int output_canvas_d[][]; // classification functions // public boolean initialize() { // Debug //System.out.println(algo_id + ": initialize()");d56 2a57 7 trans_matrix_d = new Matrix(); cov_matrix_d = new Matrix(); support_vectors_d = new Vector(); point_means_d = new Vector(); decision_regions_d = new Vector(); step_count = 3; algo_id = "AlgorithmPCA";d59 2a60 15 // add the process description for the PCA 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 support regions."); description_d.addElement(str); str = new String(" 3. Computing the decision regions based on the class independent principal component analysis algorithm."); description_d.addElement(str); }d62 3a64 2 // append message to process box pro_box_d.appendMessage("Class Independent Principal" + " Component Analysis:" + "\n");d66 4a69 18 // 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(); // set the step index step_index_d = 0; // append message to process box pro_box_d.appendMessage((String)description_d.get(step_index_d)); // exit gracefully return true; } // the Runnable methodd71 23a93 1 public void run()d95 4a98 2 // Debug //System.out.println(algo_id + ": run()");d100 6a105 13 if (step_index_d == 1) { disableControl(); step1(); enableControl(); } else if (step_index_d == 2) { disableControl(); step2(); enableControl(); }d107 6a112 11 else if (step_index_d == 3) { disableControl(); step3(); pro_box_d.appendMessage(" Algorithm Complete"); enableControl(); } // exit gracefully // return;d114 45d160 26a185 1 // method: step1 d187 7a193 2 // arguments: none // return : noned195 4a198 1 // step one of the algorithmd200 2a201 8 boolean step1() { // Debug //System.out.println(algo_id + ": step1()"); pro_box_d.setProgressMin(0); pro_box_d.setProgressMax(1); pro_box_d.setProgressCurr(0);d203 21a223 11 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);d225 5a229 11 // step 1 completed // pro_box_d.setProgressCurr(1); output_panel_d.repaint(); // exit gracefully // return true; } // method: step2 d231 1a231 2 // arguments: none // return : noned233 7a239 1 // step two of the algorithmd241 18a258 4 boolean step2() { // Debug //System.out.println(algo_id + ": step2()");d260 5d266 6a271 3 pro_box_d.setProgressMin(0); pro_box_d.setProgressMax(20); pro_box_d.setProgressCurr(0);d273 3a275 5 // append message to process box // transformPCA(); printMatrices(); computeMeans();d277 10a286 3 // display means // output_panel_d.addOutput(point_means_d, Classify.PTYPE_OUTPUT_LARGE, Color.black);d288 3a290 3 // display support vectors // output_panel_d.addOutput(support_vectors_d, Classify.PTYPE_INPUT, Color.cyan);d292 3a294 4 // display support vectors // pro_box_d.setProgressCurr(20); output_panel_d.repaint();d296 1a296 6 // exit gracefully // return true; } // method: step3 d298 5a302 2 // arguments: none // return : noned304 5a308 1 // step one of the algorithmd310 2a311 4 boolean step3() { // Debug //System.out.println(algo_id + ": step3()");d313 3a315 3 pro_box_d.setProgressMin(0); pro_box_d.setProgressMax(20); pro_box_d.setProgressCurr(0);d317 4a320 3 // compute the decision regisions // computeDecisionRegions();d322 3a324 3 // compute errors // computeErrors();d326 26a351 13 // display support vectors // // display support vectors // output_panel_d.addOutput(decision_regions_d, Classify.PTYPE_INPUT, new Color(255, 200, 0)); //Color.black); //pro_box_d.setProgressCurr(20); output_panel_d.repaint(); // exit gracefully // return true;d353 2a354 2 // method transformPCAd356 4a359 2 // arguments: // Data d: input data pointd361 10a370 1 // return : noned372 4a375 2 // this method transforms a given set of points to a new space // using the class independent principal component analysis algorithmd377 10a386 1 public void transformPCA()a387 2 // Debug //System.out.println(algo_id + ": transformPCA()");d389 1a389 1 // declare local variablesd391 2a392 3 int size = 0; int xsize = 0; int ysize = 0;d394 1a394 1 // declare variables to compute the global meand396 1a396 4 double xval = 0.0; double yval = 0.0; double xmean = 0.0; double ymean = 0.0;d398 1a398 1 // declare the covariance objectd400 2a401 1 Covariance cov = new Covariance();d403 1a403 1 // declare an eigen objectd405 1a405 2 // 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();d407 1a407 1 // declare the covariance matrixd409 1a409 3 Matrix covariance = new Matrix(); covariance.row = covariance.col = 2; covariance.Elem = new double[2][2];d411 1a411 1 // declare arrays for the eigenvaluesd413 3a415 1 double eigVal[] = null;d417 1a417 1 // declare an array to store the eigen vectorsd419 10a428 1 double eigVec[] = new double[2];d430 1a430 1 // declare matrix objectsd432 21a452 3 Matrix T = new Matrix(); Matrix M = new Matrix(); Matrix W = new Matrix();d454 12a465 5 // allocate memory for the matrix elements // T.Elem = new double[2][2]; M.Elem = new double[2][2]; W.Elem = new double[2][2];d467 7a473 1 // declare arrays to store the samplesd475 2a476 2 double x[] = null; double y[] = null;d478 1a478 1 // declare the maximum size of all data sets togetherd480 1a480 1 int maxsize = set1_d.size() + set2_d.size() + set3_d.size() + set4_d.size();d482 2a483 1 // initialize arrays to store the samplesd485 1a485 2 x = new double[maxsize]; y = new double[maxsize];d487 1a487 1 // get the samples from the first data setd489 2a490 1 size = set1_d.size();d492 1a492 2 // set up the initial random vectors i.e., the vectors of // X and Y coordinate points form the displayd494 2a495 12 for (int i = 0; i < size; i++) { MyPoint p = (MyPoint)set1_d.elementAt(i); xval += p.x; yval += p.y; x[xsize++] = p.x; y[ysize++] = p.y; } // get the samples from the second data set // size = set2_d.size();d497 1a497 2 // set up the initial random vectors i.e., the vectors of // X and Y coordinate points form the displayd499 4a502 8 for (int i = 0; i < size; i++) { MyPoint p = (MyPoint)set2_d.elementAt(i); xval += p.x; yval += p.y; x[xsize++] = p.x; y[ysize++] = p.y; }d504 54a557 106 // get the samples from the third data set // size = set3_d.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)set3_d.elementAt(i); xval += p.x; yval += p.y; x[xsize++] = p.x; y[ysize++] = p.y; } // get the samples from the first data set // size = set4_d.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)set4_d.elementAt(i); xval += p.x; yval += p.y; x[xsize++] = p.x; y[ysize++] = p.y; } if (maxsize > 0) { // 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); cov_matrix_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 eigVal = Eigen.compEigenVal(T); // compute the eigen vectors // for (int i = 0; i < 2; i++) { // Changed eigen to Eigen since member function is static - Phil T. 6-23-03 Eigen.calcEigVec(M, eigVal[i], eigVec); for (int j = 0; j < 2; j++) { W.Elem[j][i] = eigVec[j] / Math.sqrt(eigVal[i]); } } // save the transformation matrix // trans_matrix_d = W; } // compute the global mean of the data sets // xmean = xval / xsize; ymean = yval / ysize; // determine points for the support regions // double val[][] = new double[2][1]; Matrix invT = new Matrix(); Matrix supp = new Matrix(); Matrix temp = new Matrix(); // set up the angle with which to rotate the axis // double theta = 0.0; double alpha = cov_matrix_d.Elem[0][0] - cov_matrix_d.Elem[1][1]; double beta = -2 * cov_matrix_d.Elem[0][1]; if (eigVal[0] > eigVal[1]) { theta = Math.atan2((alpha - Math.sqrt((alpha * alpha) + (beta * beta))), beta); } else { theta = Math.atan2((alpha + Math.sqrt((alpha * alpha) + (beta * beta))), beta); }d559 1a559 8 // compute the inverse of the transformation matrix // trans_matrix_d.invertMatrix(invT); // loop through all points on the circumference of the // gaussian sphere in the transformed space // for (int i = 0; i < 360; i++)d562 1a562 1 // get the x and y co-ordinatesd564 3a566 2 val[0][0] = 1.5 * Math.cos(i); val[1][0] = 1.5 * Math.sin(i);d568 1a568 1 // set up the points as a matrix in order for multiplicationd570 3a572 1 temp.initMatrix(val, 2, 1);d574 1a574 2 // transform the points from the feature space back to the // original space to create the support region for the data setd576 3a578 1 invT.multMatrix(temp, supp);d580 1a580 1 // rotate the points after transforming them to the new spaced582 18a599 14 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)); // time shift the co-ordinates to the global mean // xval = xval + xmean; yval = yval + ymean; // add the point to the support region vector // MyPoint pt = new MyPoint(xval, yval); support_vectors_d.addElement(pt); } }d601 24a624 56 // method: computeDecisionRegions // // arguments: none // return : none // // method computes the line of discrimination for the classification // algorithms when the corresponding flags have been initialized // public void computeDecisionRegions() { // Debug //System.out.println(algo_id + ": computeDecisionRegions()"); DisplayScale scale = output_panel_d.disp_area_d.getDisplayScale(); double currentX = scale.xmin; double currentY = scale.ymin; // set precision // 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; // declare a 2D array to store the class associations // output_canvas_d = new int[outputWidth][outputHeight]; // loop through each and every point on the pixmap and // determine which class each pixel is associated with // MyPoint point; double dist = 0.0; int associated = 0; double smallestSoFar = Double.MAX_VALUE; int target = 0; boolean set1flag = true; boolean set2flag = true; boolean set3flag = true; pro_box_d.setProgressMin(0); pro_box_d.setProgressMax(outputWidth); pro_box_d.setProgressCurr(0); for (int i = 0; i < outputWidth; i++) { currentX += incrementX; currentY = scale.ymin; pro_box_d.setProgressCurr(i); for (int j = 0; j < outputHeight; j++) {d626 1a626 1 // declare the current pixel pointd628 2a629 3 currentY += incrementY; MyPoint pixel = new MyPoint(currentX, currentY); smallestSoFar = Double.MAX_VALUE;d631 1a631 1 // convert the pixel to the time domaind633 2a634 3 double X[][] = new double[1][2]; X[0][0] = pixel.x; X[0][1] = pixel.y;d636 2a637 1 // reset the boolean flagsd639 5a643 3 set1flag = true; set2flag = true; set3flag = true;d645 2a646 1 // find the closest point from the first classd648 3a650 1 for (int k = 0; k < point_means_d.size(); k++)d653 2a654 71 // classify the sample to the first set // if (set1_d.size() > 0 && set1flag) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -