📄 algorithmpca2.java
字号:
// 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 // PCA2_d = W; } // get the samples from the first data set // size = set3_d.size(); // increment the variable count for the third data set // xsize3 += size; ysize3 += 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)set3_d.elementAt(i); xval3 += p.x; yval3 += 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); CPCA3_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 // eigVal3 = 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, eigVal3[i], eigVec); for (int j = 0; j < 2; j++) { W.Elem[j][i] = eigVec[j] / Math.sqrt(eigVal3[i]); } } // save the transformation matrix // PCA3_d = W; } // get the samples from the first data set // size = set4_d.size(); // increment the variable count for the forth data set // xsize4 += size; ysize4 += 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)set4_d.elementAt(i); xval4 += p.x; yval4 += 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); CPCA4_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 eigVal4 = 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, eigVal4[i], eigVec); for (int j = 0; j < 2; j++) { W.Elem[j][i] = eigVec[j] / Math.sqrt(eigVal4[i]); } } // save the transformation matrix // PCA4_d = W; } // determine the local mean of each data set // if (xsize1 > 0 && ysize1 > 0) { xmean1 = (double) (xval1 / xsize1); ymean1 = (double) (yval1 / ysize1); } if (xsize2 > 0 && ysize2 > 0) { xmean2 = (double) (xval2 / xsize2); ymean2 = (double) (yval2 / ysize2); } if (xsize3 > 0 && ysize3 > 0) { xmean3 = (double) (xval3 / xsize3); ymean3 = (double) (yval3 / ysize3); } if (xsize4 > 0 && ysize4 > 0) { xmean4 = (double) (xval4 / xsize4); ymean4 = (double) (yval4 / ysize4); } // determine the support regions data points // double val[][] = new double[2][1]; Matrix invT = new Matrix(); Matrix supp = new Matrix(); Matrix temp = new Matrix(); // determine if the second transformation matrix is computed // if (PCA1_d != null) { // set up the angle with which to rotate the axis // double theta = 0.0; double alpha = CPCA1_d.Elem[0][0] - CPCA1_d.Elem[1][1]; double beta = -2 * CPCA1_d.Elem[0][1]; if (eigVal1[0] > eigVal1[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 // PCA1_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)); // shift the points to the class mean // xval = xval + xmean1; yval = yval + ymean1; // add the point to the support region vector // MyPoint pt = new MyPoint(xval, yval); support_vectors_d.addElement(pt); } } // determine if the second transformation matrix is computed // if (PCA2_d != null) { // set up the angle with which to rotate the axis // double theta = 0.0; double alpha = CPCA2_d.Elem[0][0] - CPCA2_d.Elem[1][1]; double beta = -2 * CPCA2_d.Elem[0][1]; if (eigVal2[0] > eigVal2[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 // PCA2_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)); // shift the points to the class mean // xval = xval + xmean2; yval = yval + ymean2; // add the point to the support region vector // MyPoint pt = new MyPoint(xval, yval); support_vectors_d.addElement(pt); } } // determine if the third transformation matrix is computed // if (PCA3_d != null) { // set up the angle with which to rotate the axis // double theta = 0.0; double alpha = CPCA3_d.Elem[0][0] - CPCA3_d.Elem[1][1]; double beta = -2 * CPCA3_d.Elem[0][1]; if (eigVal3[0] > eigVal3[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 // PCA3_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)); // shift the points to the class mean // xval = xval + xmean3; yval = yval + ymean3; // add the point to the support region vector // MyPoint pt = new MyPoint(xval, yval); support_vectors_d.addElement(pt); } } // determine if the forth transformation matrix is computed // if (PCA4_d != null) { // set up the angle with which to rotate the axis // double theta = 0.0; double alpha = CPCA4_d.Elem[0][0] - CPCA4_d.Elem[1][1]; double beta = -2 * CPCA4_d.Elem[0][1]; if (eigVal4[0] > eigVal4[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 // PCA4_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)); // shift the points to the class mean // xval = xval + xmean4; yval = yval + ymean4; // add the point to the support region vector // MyPoint pt = new MyPoint(xval, yval); support_vectors_d.addElement(pt); } } } /** * Computes the line of discrimination for the classification * algorithms when the corresponding flags have been initialized * */ public void computeDecisionRegions() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -