📄 algorithmldapca.java,v
字号:
samples = samples1 + samples2 + samples3 + samples4;
incorrect = incorrect1 + incorrect2 + incorrect3 + incorrect4;
error = ((double)incorrect / (double)samples) * 100.0;
text =
new String(
" Overall results:\n"
+ " Total number of samples: "
+ samples
+ "\n"
+ " Misclassified samples: "
+ incorrect
+ "\n"
+ " Classification error: "
+ MathUtil.setDecimal(error, 2)
+ "%");
pro_box_d.appendMessage(text);
}
}
@1.6log@Removed large comment block that was causing errors.@text@d11 1d26 2a27 2 Vector decision_regions_d;
Vector support_vectors_d;
d62 4a65 4 point_means_d = new Vector();
decision_regions_d = new Vector();
support_vectors_d = new Vector();
description_d = new Vector();
d110 9a118 4 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();
@1.5log@Fixed javadoc comments.@text@d1067 1a1067 1 *
@1.4log@Java Documentation style comments.@text@d4 2a5 1// Last Edited : Sanjay Patil
d51 1a51 1 * @@return boolean
d171 1a171 1 * @@return boolean
d206 2d263 1a263 1 * @@return boolean
d305 2d1065 2a1066 2 * @@param d : DataPoints to be transformed
* @@param S : transformed matrix
d1131 3a1133 7 /* // method: computeDecisionRegions
//
// arguments: none
// return : none
//
// method computes the line of discrimination for for class independent LDA
//
@1.3log@did not bother to do much and this is not a complete version of class.@text@d1 12a12 5/*
* AlgorithmLDAPCA.java v6.0 03/15/2005
* Author - Phil Trasatti last edited Sanjay Created on Jul 15, 2003
*/
d16 4d29 1a29 1 /
d32 1a32 1 Matrix CLDA; // covariance matrix for CLDA1
d45 6a50 2 /* (non-Javadoc)
* @@see IFAlgorithm#initialize()
d58 1d126 11a136 1 public void run()
d138 11a148 10 // Debug
//
// System.out.println(algo_id + " run()");
if (step_index_d == 1)
{
disableControl();
step1();
enableControl();
}
d150 6a155 6 else if (step_index_d == 2)
{
disableControl();
step2();
enableControl();
}
d157 9a165 16 else if (step_index_d == 3)
{
disableControl();
step3();
enableControl();
}
else if (step_index_d == 4)
{
disableControl();
step4();
pro_box_d.appendMessage(" Algorithm Complete");
enableControl();
}
return;
}
d167 33a199 5 boolean step1()
{
// Debug
//
// System.out.println(algo_id + " step1()");
d201 41a241 3 pro_box_d.setProgressMin(0);
pro_box_d.setProgressMax(20);
pro_box_d.setProgressCurr(0);
d243 12a254 15 // append message to process box
//
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(20);
output_panel_d.repaint();
d256 6a261 59 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);
computeMeans();
// determine the within class scatter matrix
//
withinClass(W);
// determine the between class scatter matrix
//
betweenClass(B);
// determine the ratio of the between class scatter matrix
// to the within class scatter matrix
//
W.invertMatrix(invW);
invW.multMatrix(B, S);
// transform the samples from all data sets
//
transformLDA(data_points_d, S);
displayMatrices();
// 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.black );
// display support vectors
//
pro_box_d.setProgressCurr(20);
output_panel_d.repaint();
return true;
}
// method: step3 PCA algorithm
//
// arguments: none
// return : none
//
// step two of the algorithm
// for PCA algorithm, transformPCA, pritnMatrices Methods are added
//
d299 5a303 8 // method: step4
//
// arguments: none
// return : none
//
// step one of the algorithm for PCA decision Region and Errors
//
boolean step4()
d333 7a339 11 // method: withinClass
//
// arguments:
// Data d: input data points
// Matrix M: within class scatter matrix
//
// return : none
//
// this method determines the within class scatter matrix
//
public void withinClass(Matrix M)
d503 7a509 11 // method: betweenClass
//
// arguments:
// Data d: input data points
// Matrix M: between class scatter matrix
//
// return : none
//
// this method determines the between class scatter matrix for
// the class independent linear discrimination algorithm
//
d715 4a718 11
// method dispalyMatrices
//
// arguments:
// Data d: input data point
//
// return : none
//
// display two matrices
//
d777 3a779 9 // method printMatrices for PCA
//
// arguments:
// Data d: input data point --- this should be LDA Transformed data point
//
// return : none
//
// display two matrices
//
d809 10a818 1 // method transformPCA
d820 5a824 2 // arguments:
// Data d: input data point
d826 6a831 1 // return : none
d833 3a835 2 // this method transforms a given set of points to a new space
// using the class independent principal component analysis algorithm
d837 3a839 4 public void transformPCA()
{
// Debug
//System.out.println(algo_id + ": transformPCA()");
d841 3a843 5 // declare local variables
//
int size = 0;
int xsize = 0;
int ysize = 0;
d845 3a847 6 // declare variables to compute the global mean
//
double xval = 0.0;
double yval = 0.0;
double xmean = 0.0;
double ymean = 0.0;
d849 5a853 3 // declare the covariance object
//
Covariance cov = new Covariance();
d855 5a859 4 // 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();
d861 4a864 5 // declare the covariance matrix
//
Matrix covariance = new Matrix();
covariance.row = covariance.col = 2;
covariance.Elem = new double[2][2];
d866 4a869 3 // declare arrays for the eigenvalues
//
double eigVal[] = null;
d871 4a874 3 // declare an array to store the eigen vectors
//
double eigVec[] = new double[2];
d876 3a878 5 // declare matrix objects
//
Matrix T = new Matrix();
Matrix M = new Matrix();
Matrix W = new Matrix();
d880 62a941 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];
d943 4a946 4 // declare arrays to store the samples
//
double x[] = null;
double y[] = null;
d948 3a950 3 // declare the maximum size of all data sets together
//
int maxsize = set1_d.size() + set2_d.size() + set3_d.size() + set4_d.size();
d952 4a955 4 // initialize arrays to store the samples
//
x = new double[maxsize];
y = new double[maxsize];
d957 3a959 3 // get the samples from the first data set
//
size = set1_d.size();
d961 3a963 5 // 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++)
{
d965 2a966 15 /*
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;
//}
}
d968 6a973 8 // 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++)
d975 1a975 5 MyPoint p = (MyPoint)set2_d.elementAt(i);
xval += p.x;
yval += p.y;
x[xsize++] = p.x;
y[ysize++] = p.y;
d977 11d989 3a991 3 // get the samples from the third data set
//
size = set3_d.size();
d993 3a995 11 // 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;
}
d997 5a1001 3 // get the samples from the first data set
//
size = set4_d.size();
d1003 10a1012 11 // 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;
}
d1014 3a1016 2 if (maxsize > 0)
{
d1018 5a1022 38 // 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]);
}
}
d1024 4a1027 4 // save the transformation matrix
//
trans_matrix_d = W;
}
d1029 3a1031 4 // compute the global mean of the data sets
//
xmean = xval / xsize;
ymean = yval / ysize;
d1033 4a1036 3 // determine points for the support regions
//
double val[][] = new double[2][1];
d1038 6a1043 3 Matrix invT = new Matrix();
Matrix supp = new Matrix();
Matrix temp = new Matrix();
d1045 4a1048 5 // 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];
d1050 4a1053 48 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
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -