📄 algorithmpca2.java,v
字号:
head 1.6;access;symbols;locks; strict;comment @# @;1.6date 2005.06.10.18.25.20; author rirwin; state Exp;branches;next 1.5;1.5date 2005.05.23.19.33.21; author rirwin; state Exp;branches;next 1.4;1.4date 2005.03.17.17.45.40; author patil; state Exp;branches;next 1.3;1.3date 2005.03.08.04.21.35; author patil; state Exp;branches;next 1.2;1.2date 2005.01.20.02.41.42; author patil; state Exp;branches;next 1.1;1.1date 2004.12.28.00.04.32; author patil; state Exp;branches;next ;desc@No changes made.@1.6log@Esablishing RCS vesion.@text@/* * AlgorithmPCA2.java last edited Ryan Irwin V6.0 * @@(#) AlgorithmPCA2.java 1.10 03/15/03 * */// import java packages//import java.awt.*;import java.util.*;/** * Operation of the PCA Class-Independent Algorithm */public class AlgorithmPCA2 extends Algorithm{ //----------------------------------------------------------------- // // instance data members // //----------------------------------------------------------------- Matrix PCA1_d; Matrix PCA2_d; Matrix PCA3_d; Matrix PCA4_d; Matrix CPCA1_d; Matrix CPCA2_d; Matrix CPCA3_d; Matrix CPCA4_d; // vector of support region points // Vector<MyPoint> support_vectors_d = new Vector<MyPoint>(); Vector<MyPoint> decision_regions_d = new Vector<MyPoint>(); int output_canvas_d[][]; String algo_id = "AlgorithmPCA"; //------------------------------------------------------------------ // // public class methods // //------------------------------------------------------------------ /** * Overrides the initialize() method in the base class. Initializes * member data and prepares for execution of first step. This method * "resets" the algorithm. * * @@return true */ public boolean initialize() { // Debug // // System.out.println("AlgorithmPCA2 : initialize()"); support_vectors_d = new Vector<MyPoint>(); point_means_d = new Vector<MyPoint>(); decision_regions_d = new Vector<MyPoint>(); step_count = 3; // check the data points // if (output_panel_d == null) { return false; } // 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); str = new String(" 1. Displaying the original data."); description_d.addElement(str); str = new String(" 2. Computing the means and support regions."); description_d.addElement(str); str = new String(" 3. Computing the decision regions."); description_d.addElement(str); } // append message to process box // pro_box_d.appendMessage("Class Dependent PCA :" + "\n"); PCA1_d = null; PCA2_d = null; PCA3_d = null; PCA4_d = null; CPCA1_d = null; CPCA2_d = null; CPCA3_d = null; CPCA4_d = null; // 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(); // set1_d = data_points_d.dset1; set2_d = data_points_d.dset2; set3_d = data_points_d.dset3; set4_d = data_points_d.dset4; // reset values // support_vectors_d = new Vector<MyPoint>(); decision_regions_d = new Vector<MyPoint>(); // 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; } /** * Implementation of the run function from the Runnable interface. * Determines what the current step is and calls the appropriate method. */ 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; } /** * * step one of the algorithm * * @@return true */ boolean step1() { // Debug // // System.out.println(algo_id + ": step1()"); pro_box_d.setProgressMin(0); pro_box_d.setProgressMax(1); pro_box_d.setProgressCurr(0); 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); // step 1 completed // pro_box_d.setProgressCurr(1); output_panel_d.repaint(); // exit gracefully // return true; } /** * * step two of the algorithm * * @@return true * */ boolean step2() { // Debug // // System.out.println(algo_id + " : step2()"); pro_box_d.setProgressMin(0); pro_box_d.setProgressMax(20); pro_box_d.setProgressCurr(0); transformPCA2(); printMatrices(); computeMeans(); // display means // output_panel_d.addOutput(point_means_d, Classify.PTYPE_OUTPUT_LARGE, Color.black); // display support vectors // output_panel_d.addOutput(support_vectors_d, Classify.PTYPE_INPUT, Color.cyan); pro_box_d.setProgressCurr(20); output_panel_d.repaint(); return true; } /** * * step three of the algorithm * * @@return true */ 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 regisions // computeDecisionRegions(); // compute errors // computeErrors(); // display support vectors // output_panel_d.addOutput(decision_regions_d, Classify.PTYPE_INPUT, new Color(255, 200, 0)); output_panel_d.repaint(); return true; } /** * * Transforms a given set of points to a new space * using the class dependent principal component analysis algorithm * */ public void transformPCA2() { // Debug // // System.out.println(algo_id + " : transformPCA2()"); // declare local variables // 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; 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; 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; double xval = 0.0; double yval = 0.0; // declare the covariance object // 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 samples // double x[] = null; double y[] = null; // get the samples from the first data set // 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; } if (size > 0) { // 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++) { Eigen.calcEigVec(M, eigVal1[i], eigVec); for (int j = 0; j < 2; j++) { W.Elem[j][i] = eigVec[j] / Math.sqrt(eigVal1[i]); } } // save the transformation matrix // PCA1_d = W; } // get the samples from the first data set // size = set2_d.size(); // increment the variable count for the second data set // xsize2 += size; ysize2 += 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)set2_d.elementAt(i); xval2 += p.x; yval2 += p.y; x[i] = p.x; y[i] = p.y; } if (size > 0) { // 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); CPCA2_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 eigVal2 = 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, eigVal2[i], eigVec); for (int j = 0; j < 2; j++) { W.Elem[j][i] = eigVec[j] / Math.sqrt(eigVal2[i]); } } // save the transformation matrix
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -