📄 algorithmldapca.java,v
字号:
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);
}
d1055 1d1057 6a1062 12 // 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
//
d1065 2a1066 2 // Debug
//System.out.println(algo_id + " transformLDA(Data d, Matrix S)");
d1125 1d1304 15a1318 1 // method: computeDecisionRegions for PCA
d1320 2a1321 10 // arguments: none
// return : none
//
// method computes the line of discrimination for the classification
// algorithms when the corresponding flags have been initialized
//
public void computeDecisionRegions()
{
// Debug
//System.out.println(algo_id + ": computeDecisionRegions()");
d1323 2a1324 1 DisplayScale scale = output_panel_d.disp_area_d.getDisplayScale();
d1326 3a1328 2 double currentX = scale.xmin;
double currentY = scale.ymin;
d1330 7a1336 4 // set precision
//
int outputWidth = output_panel_d.disp_area_d.getXPrecision();
int outputHeight = output_panel_d.disp_area_d.getYPrecision();
d1338 14a1351 6 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];
d1353 1a1353 18 // loop through each and every point on the pixmap and
// determine which class each pixel is associated with
//
MyPoint point;
double dist = 0.0;
int associated = 0;
double smallestSoFar = Double.MAX_VALUE;
int target = 0;
boolean set1flag = true;
boolean set2flag = true;
boolean set3flag = true;
pro_box_d.setProgressMin(0);
pro_box_d.setProgressMax(outputWidth);
pro_box_d.setProgressCurr(0);
for (int i = 0; i < outputWidth; i++)
a1354 3 currentX += incrementX;
currentY = scale.ymin;
pro_box_d.setProgressCurr(i);
d1356 21a1376 1 for (int j = 0; j < outputHeight; j++)
d1379 1a1379 1 // declare the current pixel point
d1381 5a1385 3 currentY += incrementY;
MyPoint pixel = new MyPoint(currentX, currentY);
smallestSoFar = Double.MAX_VALUE;
d1387 1a1387 1 // convert the pixel to the time domain
d1389 5a1393 3 double X[][] = new double[1][2];
X[0][0] = pixel.x;
X[0][1] = pixel.y;
d1395 1a1395 1 // reset the boolean flags
d1397 5a1401 3 set1flag = true;
set2flag = true;
set3flag = true;
d1403 1a1403 1 // find the closest point from the first class
d1405 1a1405 1 for (int k = 0; k < point_means_d.size(); k++)
d1407 12d1420 9a1428 72 // 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
//
point = (MyPoint)point_means_d.elementAt(k);
// convert the mean point to the time domain
//
double Y[][] = new double[1][2];
Y[0][0] = point.x;
Y[0][1] = point.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(trans_matrix_d, C);
B.multMatrix(trans_matrix_d, 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;
}
}
d1430 2a1431 3 // put and entry in the output canvas array to
// indicate which class the current pixel is
// closest to
d1433 8a1440 7 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
d1442 26a1467 1 if (j > 0 && i > 0)
d1469 1a1469 4 if (associated != output_canvas_d[i][j - 1] || associated != output_canvas_d[i - 1][j])
{
decision_regions_d.add(pixel);
}
d1472 9a1480 4 } // end of the loop
}
a1482 1
d1499 2a1500 1 DisplayScale scale = output_panel_d.disp_area_d.getDisplayScale();
d1511 5d1517 5a1521 11
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++;
}
}
d1523 1d1526 18a1543 19 {
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);
}
d1548 5d1554 5a1558 11
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++;
}
}
d1560 1d1563 18a1580 19 {
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);
}
d1585 5d1591 5a1595 11
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++;
}
}
d1597 1d1600 18a1617 19 {
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);
}
d1622 6d1629 5a1633 11
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++;
}
}
d1635 1d1638 18a1655 19 {
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);
}
d1677 2a1678 2
}
a1679 2
}
@1.2log@Algorithm Complete and debug statements commented@text@d1 5a7 3/*
* Created on Jul 15, 2003
*/
a8 4/**
* @@author Phil Trasatti
*
*/
d11 21a31 18 // Public Data Members
Vector decision_regions_d;
Vector support_vectors_d;
int output_canvas_d[][];
// declare local Matrix objects
Matrix W;
Matrix LDA;
Matrix CLDA; // covariance matrix for CLDA1
Matrix B;
Matrix S;
Matrix invW;
//for PCA declare
Matrix trans_matrix_d = new Matrix();
Matrix cov_matrix_d = new Matrix();
// declare LDA Class objects
d33 15d49 4a52 3 /* (non-Javadoc)
* @@see IFAlgorithm#initialize()
*/
d54 15a68 1 public boolean initialize()
d70 2a71 30 algo_id = "AlgorithmLDAPCA";
//Debug
//System.out.println(algo_id + " initialize()");
step_count = 4;
point_means_d = new Vector();
decision_regions_d = new Vector();
support_vectors_d = new Vector();
description_d = new Vector();
// for PCA
trans_matrix_d = new Matrix();
cov_matrix_d = new Matrix();
// Initialize local Matrix objects
W = new Matrix();
LDA = new Matrix();
CLDA = new Matrix();
B = new Matrix();
S = new Matrix();
invW = new Matrix();
// Initialize LDA class object
// ldaobject = new Algorithm();
// Add the process description for the LDA algorithm
if (description_d.size() == 0)
{
String str = new String(" 0. Initialize the original data.");
description_d.addElement(str);
d73 2a74 2 str = new String(" 1. Displaying the original data.");
description_d.addElement(str);
d76 2a77 2 str = new String(" 2. Computing the means and covariance for LDA.");
description_d.addElement(str);
d79 2a80 2 str = new String(" 3. Computing the means and covaricance for PCA followed by LDA algorithm.");
description_d.addElement(str);
d82 3a84 3 str = new String(" 4. Computing the decision regions based on the LDA followed by PCA class independent principal component analysis algorithm.");
description_d.addElement(str);
}
d86 3a88 2 // append message to process box
pro_box_d.appendMessage("Class Independent LDA Analysis:" + "\n");
d90 6a95 5 // set the data points for this algorithm
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();
d97 3a99 2 // set the step index
step_index_d = 0;
d101 3a103 2 // append message to process box
pro_box_d.appendMessage((String)description_d.get(step_index_d));
d105 4a108 3 // exit initialize
return true;
}
d113 2a114 1 //System.out.println(algo_id + " run()");
d150 2a151 1 //System.out.println(algo_id + " step1()");
d158 1d169 1d179 2a180 1 //System.out.println(algo_id + " step2()");
d193 1a193 1 //
d209 1a209 1 //----
d213 1a213 1 //----
d224 14a237 9 /* boolean step3()
{
// Debug
System.out.println(algo_id + " step3()");
pro_box_d.setProgressMin(0);
pro_box_d.setProgressMax(20);
pro_box_d.setProgressCurr(0);
d239 3a241 3 // compute the decision regisions
//----
computeDecisionRegions();
d243 5a247 3 // compute errors
//
com
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -