📄 algorithmpca.java,v
字号:
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; }d656 17a672 14 // 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)d674 1a674 4 if (associated != output_canvas_d[i][j - 1] || associated != output_canvas_d[i - 1][j]) { decision_regions_d.add(pixel); }d677 4a680 3 } // end of the loop }d682 37a718 36 // // method computeErrors // // // // arguments: // // Data d: input data point // // // // return : none // // // // display two matrices // // public void computeErrors() { // declare local variables // Sting 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;d722 6d729 4a732 11 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++; } }d734 1d737 19a755 19 { 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); }d760 6d767 4a770 11 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++; } }d772 1d775 19a793 19 { 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); }d798 6d805 4a808 11 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++; } }d810 1d813 1a813 1 {d815 17a831 17 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); }d836 6d843 4a846 11 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++; } }d848 1d851 19a869 19 { 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); }a923 5@1.1log@Initial revision@text@d43 45a87 45 // Debug System.out.println(algo_id + ": initialize()"); 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"; // 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); } // append message to process box pro_box_d.appendMessage("Class Independent Principal" + " Component Analysis:" + "\n"); // 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;d95 1a95 1 System.out.println(algo_id + ": run()");d115 1a115 1 pro_box_d.appendMessage(" Step Sequence Complete");d134 1a134 1 System.out.println(algo_id + ": step1()");d172 1a172 1 System.out.println(algo_id + ": step2()");d213 1a213 1 System.out.println(algo_id + ": step3()");d254 80a333 2 // Debug System.out.println(algo_id + ": transformPCA()");d335 15a349 5 // declare local variables // int size = 0; int xsize = 0; int ysize = 0;d351 19a369 6 // declare variables to compute the global mean // double xval = 0.0; double yval = 0.0; double xmean = 0.0; double ymean = 0.0;d371 98a468 1 // declare the covariance objectd470 4a473 3 Covariance cov = new Covariance(); // declare an eigen objectd475 4a478 4 // 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 the covariance matrixd480 3a482 5 Matrix covariance = new Matrix(); covariance.row = covariance.col = 2; covariance.Elem = new double[2][2]; // declare arrays for the eigenvaluesd484 4a487 3 double eigVal[] = null; // declare an array to store the eigen vectorsd489 4a492 3 double eigVec[] = new double[2]; // declare matrix objectsd494 3a496 212 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]; // declare arrays to store the samples // double x[] = null; double y[] = null; // declare the maximum size of all data sets together // int maxsize = set1_d.size() + set2_d.size() + set3_d.size() + set4_d.size(); // initialize arrays to store the samples // x = new double[maxsize]; y = new double[maxsize]; // get the samples from the first data set // size = set1_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++) { /* if (data_a.type_d != DataPoints.DTYPE_USER_SELECTED) { MyPoint p = (MyPoint)data_a.dset1.elementAt(i); x[xsize++] = p.x; y[ysize++] = p.y; } else { */ 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(); // 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); xval += p.x; yval += p.y; x[xsize++] = p.x; y[ysize++] = p.y; } // 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); } // 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++) { // get the x and y co-ordinates // val[0][0] = 1.5 * Math.cos(i); val[1][0] = 1.5 * Math.sin(i); // set up the points as a matrix in order for multiplication // temp.initMatrix(val, 2, 1); // transform the points from the feature space back to the // original space to create the support region for the data set // invT.multMatrix(temp, supp); // rotate the points after transforming them to the new space // 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); }d511 1a511 1 System.out.println(algo_id + ": computeDecisionRegions()");d690 1a690 1 String text;@
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -