📄 algorithmldapca.java
字号:
+ set3_d.size() + set4_d.size();
double pr1 = set1_d.size() / maxsamples;
double pr2 = set2_d.size() / maxsamples;
double pr3 = set3_d.size() / maxsamples;
double pr4 = set4_d.size() / maxsamples;
DisplayScale scale = output_panel_d.disp_area_d.getDisplayScale();
// initialize the between class matrix
//
M.row = M.col = 2;
M.Elem = new double[2][2];
M.resetMatrix();
int j = 0;
// obtain the means of each individual class
//
if (set1_d.size() > 0)
{
MyPoint p = (MyPoint)point_means_d.elementAt(j);
j++;
xmean1 = p.x;
ymean1 = p.y;
}
if (set2_d.size() > 0)
{
MyPoint p = (MyPoint)point_means_d.elementAt(j);
j++;
xmean2 = p.x;
ymean2 = p.y;
}
if (set3_d.size() > 0)
{
MyPoint p = (MyPoint)point_means_d.elementAt(j);
j++;
xmean3 = p.x;
ymean3 = p.y;
}
if (set4_d.size() > 0)
{
MyPoint p = (MyPoint)point_means_d.elementAt(j);
j++;
xmean4 = p.x;
ymean4 = p.y;
}
// compute the global mean of all data sets
//
int samples = 0;
for (int i = 0; i < set1_d.size(); samples++, i++)
{
MyPoint p2 = (MyPoint)set1_d.elementAt(i);
xmean += p2.x;
ymean += p2.y;
}
for (int i = 0; i < set2_d.size(); samples++, i++)
{
MyPoint p2 = (MyPoint)set2_d.elementAt(i);
xmean += p2.x;
ymean += p2.y;
}
for (int i = 0; i < set3_d.size(); samples++, i++)
{
MyPoint p2 = (MyPoint)set3_d.elementAt(i);
xmean += p2.x;
ymean += p2.y;
}
for (int i = 0; i < set4_d.size(); samples++, i++)
{
MyPoint p2 = (MyPoint)set4_d.elementAt(i);
xmean += p2.x;
ymean += p2.y;
}
xmean /= samples;
ymean /= samples;
// compute the between class scatter contribution of the first set
//
if (set1_d.size() > 0)
{
Matrix S = new Matrix();
mean[0][0] = xmean1 - xmean;
mean[0][1] = ymean1 - ymean;
M1.initMatrix(mean, 1, 2);
transpose[0][0] = xmean1 - xmean;
transpose[1][0] = ymean1 - ymean;
T1.initMatrix(transpose, 2, 1);
T1.multMatrix(M1, S);
M.addMatrix(S);
}
// compute the between class scatter contribution of the second set
//
if (set2_d.size() > 0)
{
Matrix S = new Matrix();
mean[0][0] = xmean2 - xmean;
mean[0][1] = ymean2 - ymean;
M2.initMatrix(mean, 1, 2);
transpose[0][0] = xmean2 - xmean;
transpose[1][0] = ymean2 - ymean;
T2.initMatrix(transpose, 2, 1);
T2.multMatrix(M2, S);
M.addMatrix(S);
}
// compute the between class scatter contribution of the third set
//
if (set3_d.size() > 0)
{
Matrix S = new Matrix();
mean[0][0] = xmean3 - xmean;
mean[0][1] = ymean3 - ymean;
M3.initMatrix(mean, 1, 2);
transpose[0][0] = xmean3 - xmean;
transpose[1][0] = ymean3 - ymean;
T3.initMatrix(transpose, 2, 1);
T3.multMatrix(M3, S);
M.addMatrix(S);
}
// compute the between class scatter contribution of the forth set
//
if (set4_d.size() > 0)
{
Matrix S = new Matrix();
mean[0][0] = xmean4 - xmean;
mean[0][1] = ymean4 - ymean;
M4.initMatrix(mean, 1, 2);
transpose[0][0] = xmean4 - xmean;
transpose[1][0] = ymean4 - ymean;
T4.initMatrix(transpose, 2, 1);
T4.multMatrix(M4, S);
M.addMatrix(S);
}
}
/**
* Display two matrices - covariance matrix and the transformation
* matrix in the text message window
*/
public void displayMatrices()
{
// declare local variables
//
double a11 = 0.0;
double a12 = 0.0;
double a21 = 0.0;
double a22 = 0.0;
String text;
a11 = MathUtil.setDecimal(CLDA.Elem[0][0], 2);
a12 = MathUtil.setDecimal(CLDA.Elem[0][1], 2);
a21 = MathUtil.setDecimal(CLDA.Elem[1][0], 2);
a22 = MathUtil.setDecimal(CLDA.Elem[1][1], 2);
text =
new String(
" Covariance matrix:\n"
+ " "
+ a11
+ " "
+ a12
+ "\n"
+ " "
+ a21
+ " "
+ a22);
// append message to process box
//
pro_box_d.appendMessage(text);
a11 = MathUtil.setDecimal(LDA.Elem[0][0], 2);
a12 = MathUtil.setDecimal(LDA.Elem[0][1], 2);
a21 = MathUtil.setDecimal(LDA.Elem[1][0], 2);
a22 = MathUtil.setDecimal(LDA.Elem[1][1], 2);
text =
new String(
" Transformation matrix:\n"
+ " "
+ a11
+ " "
+ a12
+ "\n"
+ " "
+ a21
+ " "
+ a22);
// append message to process box
//
pro_box_d.appendMessage(text);
}
/**
* Displays the caovariance and LDA transform data matrix
*/
public void printMatrices()
{
double a11, a12, a21, a22;
String text;
a11 = MathUtil.setDecimal(cov_matrix_d.Elem[0][0], 2);
a12 = MathUtil.setDecimal(cov_matrix_d.Elem[0][1], 2);
a21 = MathUtil.setDecimal(cov_matrix_d.Elem[1][0], 2);
a22 = MathUtil.setDecimal(cov_matrix_d.Elem[1][1], 2);
text = new String(" Covariance matrix:\n" +
" " + a11 + " " + a12 + "\n" +
" " + a21 + " " + a22);
pro_box_d.appendMessage(text + "\n");
a11 = MathUtil.setDecimal(trans_matrix_d.Elem[0][0], 2);
a12 = MathUtil.setDecimal(trans_matrix_d.Elem[0][1], 2);
a21 = MathUtil.setDecimal(trans_matrix_d.Elem[1][0], 2);
a22 = MathUtil.setDecimal(trans_matrix_d.Elem[1][1], 2);
text = new String(" Transformation matrix:\n" +
" " + a11 + " " + a12 + "\n" +
" " + a21 + " " + a22);
pro_box_d.appendMessage(text + "\n");
}
/**
* Transforms a given set of points to a new space
* using the class independent principal component analysis algorithm
*/
public void transformPCA()
{
// Debug
//System.out.println(algo_id + ": transformPCA()");
// declare local variables
//
int size = 0;
int xsize = 0;
int ysize = 0;
// declare variables to compute the global mean
//
double xval = 0.0;
double yval = 0.0;
double xmean = 0.0;
double ymean = 0.0;
// declare the covariance object
//
Covariance cov = new Covariance();
// declare the covariance matrix
//
Matrix covariance = new Matrix();
covariance.row = covariance.col = 2;
covariance.Elem = new double[2][2];
// declare arrays for the eigenvalues
//
double eigVal[] = null;
// declare an array to store the eigen vectors
//
double eigVec[] = new double[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];
// 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++)
{
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
eigVal = Eigen.compEigenVal(T);
// compute the eigen vectors
//
for (int i = 0; i < 2; i++)
{
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);
}
}
/** Transforms a given set of points to a new space
* using the class independent linear discrimination analysis algorithm
*
* @param d DataPoints to be transformed
* @param S transformed matrix
*/
public void transformLDA(DataPoints d, Matrix S)
{
// Debug
// System.out.println(algo_id + " transformLDA(Data d, Matrix S)");
// declare arrays for the eigenvalues
//
double eigVal[] = null;
// declare an array to store the eigen vectors
//
double eigVec[] = new double[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();
// initialize the matrix needed to compute the eigenvalues
//
T.initMatrix(S.Elem, 2, 2);
// make a copy of the original matrix
//
M.copyMatrix(T);
// compute the eigen values
//
eigVal = Eigen.compEigenVal(T);
// compute the eigen vectors
//
for (int i = 0; i < 2; i++)
{
Eigen.calcEigVec(M, eigVal[i], eigVec);
for (int j = 0; j < 2; j++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -