📄 algorithmldapca.1java
字号:
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);
}
}
public void run()
{
// Debug
System.out.println(algo_id + " run()");
if (step_index_d == 1)
{
disableControl();
step1();
enableControl();
}
else if (step_index_d == 2)
{
disableControl();
step2();
enableControl();
}
else if (step_index_d == 3)
{
disableControl();
step3();
pro_box_d.appendMessage(" Step Sequence Complete");
enableControl();
}
return;
}
// method dispalyMatrices
//
// arguments:
// Data d: input data point
//
// return : none
//
// display two matrices
//
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);
}
// method transformLDA
//
// arguments:
// Data d: input data point
// Matrix S: between class to within class ratio
//
// return : none
//
// this method transforms a given set of points to a new space
// using the class independent linear discrimination analysis algorithm
//
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++)
{
W.Elem[j][i] = eigVec[j];
}
}
// save the transformation matrix
//
LDA = W;
}
// method: computeDecisionRegions
//
// arguments: none
// return : none
//
// method computes the line of discrimination for for class independent LDA
//
public void computeDecisionRegions()
{
// Debug
System.out.println(algo_id + " computeDecisionRegions()");
// local variable
MyPoint pt = null;
double dist = 0.0;
int associated = 0;
double smallestSoFar = Double.MAX_VALUE;
int target = 0;
boolean set1flag = true;
boolean set2flag = true;
boolean set3flag = true;
int counter = 0;
DisplayScale scale = output_panel_d.disp_area_d.getDisplayScale();
double currentX = scale.xmin;
double currentY = scale.ymin;
// set precision
//
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;
// declare a 2D array to store the class associations
//
output_canvas_d = new int[outputWidth][outputHeight];
// loop through each and every point on the pixmap and
// determine which class each pixel is associated with
//
pro_box_d.setProgressMin(0);
pro_box_d.setProgressMax(outputWidth);
for (int i = 0; i < outputWidth; i++)
{
currentX += incrementX;
currentY = scale.ymin;
// set current status
//
pro_box_d.setProgressCurr(i);
for (int j = 0; j < outputHeight; counter++, j++)
{
// declare the current pixel point
//
currentY += incrementY;
MyPoint pixel = new MyPoint(currentX, currentY);
// convert the pixel to the time domain
//
double X[][] = new double[1][2];
X[0][0] = pixel.x;
X[0][1] = pixel.y;
smallestSoFar = Double.MAX_VALUE;
// reset the boolean flags
//
set1flag = true;
set2flag = true;
set3flag = true;
// find the closest point from the first class
//
for (int k = 0; k < point_means_d.size(); k++)
{
// classify the sample to the first set
//
if (set1_d.size() > 0 && set1flag)
{
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
//
pt = (MyPoint)point_means_d.elementAt(k);
// convert the mean point to the time domain
//
double Y[][] = new double[1][2];
Y[0][0] = pt.x;
Y[0][1] = pt.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(LDA, C);
B.multMatrix(LDA, 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;
}
}
// 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)
{
if (associated != output_canvas_d[i][j - 1] || associated != output_canvas_d[i - 1][j])
{
decision_regions_d.add(pixel);
}
}
}
}
}
public void computeErrors()
{
// declare local variables
//
String 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;
// compute the classification error for the first set
//
for (int i = 0; i < set1_d.size(); i++)
{
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++;
}
}
}
if (set1_d.size() > 0)
{
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);
}
// compute the classification error for the second set
//
for (int i = 0; i < set2_d.size(); i++)
{
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++;
}
}
}
if (set2_d.size() > 0)
{
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);
}
// compute the classification error for the third set
//
for (int i = 0; i < set3_d.size(); i++)
{
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++;
}
}
}
if (set3_d.size() > 0)
{
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);
}
// compute the classification error for the forth set
//
for (int i = 0; i < set4_d.size(); i++)
{
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++;
}
}
}
if (set4_d.size() > 0)
{
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);
}
// compute the overall classification error
//
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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -