📄 algorithmpca2.java,v
字号:
y[i] = p.y; }d651 2a652 2 if (size > 0) {d654 5a658 5 // declare the covariance matrix // Matrix covariance = new Matrix(); covariance.row = covariance.col = 2; covariance.Elem = new double[2][2];d660 5a664 5 // declare matrix objects // Matrix T = new Matrix(); Matrix M = new Matrix(); Matrix W = new Matrix();d666 5a670 10 // 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;d672 13a684 3 // reset the matrices // W.resetMatrix();d686 3a688 4 // compute the covariance matrix of the first data set // covariance.Elem = cov.computeCovariance(x, y); CPCA2_d = covariance;d690 9a698 3 // initialize the matrix needed to compute the eigenvalues // T.initMatrix(covariance.Elem, 2, 2);d700 8a707 12 // 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++)d709 1a709 6 // 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]); }d711 1d713 4a716 4 // save the transformation matrix // PCA2_d = W; }d718 7a724 3 // get the samples from the first data set // size = set3_d.size();d726 5a730 4 // increment the variable count for the third data set // xsize3 += size; ysize3 += size;d732 5a736 4 // initialize arrays to store the samples // x = new double[size]; y = new double[size];d738 30a767 4 // 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++)d769 1a769 5 MyPoint p = (MyPoint)set3_d.elementAt(i); xval3 += p.x; yval3 += p.y; x[i] = p.x; y[i] = p.y;d771 9a779 2 if (size > 0)d781 1a781 2 // declare the covariance matrixd783 4a786 11 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 elementsd788 4a791 14 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 setd793 1a793 2 covariance.Elem = cov.computeCovariance(x, y); CPCA3_d = covariance;d795 1a795 1 // initialize the matrix needed to compute the eigenvaluesd797 4a800 7 T.initMatrix(covariance.Elem, 2, 2); // make a copy of the original matrix // M.copyMatrix(T); // compute the eigen valuesd802 4a805 16 // 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 d807 2a808 1 PCA3_d = W;d810 6d817 1a817 1 // get the samples from the first data setd819 3a821 1 size = set4_d.size();d823 8a830 4 // increment the variable count for the forth data set // xsize4 += size; ysize4 += size;d832 1a832 1 // initialize arrays to store the samplesd834 1a834 2 x = new double[size]; y = new double[size];d836 2a837 2 // set up the initial random vectors i.e., the vectors of // X and Y coordinate points form the displayd839 1a839 10 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)d842 1a842 1 // declare the covariance matrixd844 2a845 3 Matrix covariance = new Matrix(); covariance.row = covariance.col = 2; covariance.Elem = new double[2][2];d847 1a847 1 // declare matrix objectsd849 1a849 3 Matrix T = new Matrix(); Matrix M = new Matrix(); Matrix W = new Matrix();d851 2a852 1 // allocate memory for the matrix elementsd854 1a854 3 T.Elem = new double[2][2]; M.Elem = new double[2][2]; W.Elem = new double[2][2];d856 1a856 1 // initialize the transformation matrix dimensionsd858 4a861 4 W.row = 2; W.col = 2; // reset the matricesd863 2a864 1 W.resetMatrix();d866 1a866 1 // compute the covariance matrix of the first data setd868 34a901 2 covariance.Elem = cov.computeCovariance(x, y); CPCA4_d = covariance;d903 1a903 1 // initialize the matrix needed to compute the eigenvaluesd905 2a906 1 T.initMatrix(covariance.Elem, 2, 2);d908 1a908 1 // make a copy of the original matrixd910 4a913 3 M.copyMatrix(T); // compute the eigen valuesd915 1a915 2 // Changed eigen to Eigen since member function is static - Phil T. 6-23-03 eigVal4 = Eigen.compEigenVal(T);d917 1a917 1 // compute the eigen vectorsd919 2a920 9 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]); } }d922 1a922 1 // save the transformation matrix d924 7a930 1 PCA4_d = W;d932 6d939 1a939 1 // determine the local mean of each data setd941 5a945 6 if (xsize1 > 0 && ysize1 > 0) { xmean1 = (double) (xval1 / xsize1); ymean1 = (double) (yval1 / ysize1); } if (xsize2 > 0 && ysize2 > 0)d947 1a947 2 xmean2 = (double) (xval2 / xsize2); ymean2 = (double) (yval2 / ysize2);d949 1a949 1 if (xsize3 > 0 && ysize3 > 0)d951 1a951 7 xmean3 = (double) (xval3 / xsize3); ymean3 = (double) (yval3 / ysize3); } if (xsize4 > 0 && ysize4 > 0) { xmean4 = (double) (xval4 / xsize4); ymean4 = (double) (yval4 / ysize4);d954 1a954 1 // determine the support regions data pointsd956 1a956 1 double val[][] = new double[2][1];d958 2a959 5 Matrix invT = new Matrix(); Matrix supp = new Matrix(); Matrix temp = new Matrix(); // determine if the second transformation matrix is computedd961 1a961 1 if (PCA1_d != null)d963 2a964 2 // set up the angle with which to rotate the axisd966 4a969 14 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 matrixd971 1a971 42 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) {d973 4a976 5 // 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];d978 4a981 8 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); }d983 1a983 1 // compute the inverse of the transformation matrixd985 2a986 1 PCA2_d.invertMatrix(invT);d988 1a988 2 // loop through all points on the circumference of the // gaussian sphere in the transformed spaced990 2a991 32 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); }a992 122 // 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -