📄 algorithmkmeans.java,v
字号:
// determine the mean of the classified region
//
MyPoint mean = MathUtil.computeClusterMean(dataset);
// add the computed mean as one of the new guesses
//
guesses.addElement(new MyPoint(mean.x, mean.y));
}
// add the newly determined guesses to the region
//
region.setGuesses(guesses);
}
/**
* Determines the closest data sets to the cluster
*
* @@param mean mean point of the cluster
*
* @@return closest data set to the cluster
* @@see MyPoint
*/
public int getClosestSet(MyPoint mean)
{
// declare local variables
//
double dist1 = 0.0;
double dist2 = 0.0;
double dist3 = 0.0;
double dist4 = 0.0;
MyPoint mean1;
MyPoint mean2;
MyPoint mean3;
MyPoint mean4;
int i=0;
// determine the distance for the cluster to each data set
//
if (set1_d.size() > 0)
{
mean1 = (MyPoint)point_means_d.elementAt(i);
i++;
dist1 = MathUtil.distance(mean.x, mean.y,
mean1.x, mean1.y);
}
else
dist1 = Float.MAX_VALUE;
if (set2_d.size() > 0)
{
mean2 = (MyPoint)point_means_d.elementAt(i);
i++;
dist2 = MathUtil.distance(mean.x, mean.y,
mean2.x, mean2.y);
}
else
dist2 = Float.MAX_VALUE;
if (set3_d.size() > 0)
{
mean3 = (MyPoint)point_means_d.elementAt(i);
i++;
dist3 = MathUtil.distance(mean.x, mean.y,
mean3.x, mean3.y);
}
else
dist3 = Float.MAX_VALUE;
if (set4_d.size() > 0)
{
mean4 = (MyPoint)point_means_d.elementAt(i);
i++;
dist4 = MathUtil.distance(mean.x, mean.y,
mean4.x, mean4.y);
}
else
dist4 = Float.MAX_VALUE;
// the first data set is the closest one
//
if (dist1 < dist2 && dist1 < dist3 && dist1 < dist4)
{
return 1;
}
// the second data set is the closest one
//
if (dist2 < dist1 && dist2 < dist3 && dist2 < dist4)
{
return 2;
}
// the third data set is the closest one
//
if (dist3 < dist1 && dist3 < dist2 && dist3 < dist4)
{
return 3;
}
// the forth data set is the closest one
//
return 4;
}
/**
* determines the number of points in error, i.e, not classified
* by finding the distance of the datapoints from the closest of
* the vector set
*
* @@param closest the data specifying which of the dataset is the
* closest.
* @@param cluster the data points which form a closest cluster
* @@param id unused in current implementation
* @@return error number of data points which lie out of the
* classification
*/
public int displayClusterError(int closest, Vector cluster, int id)
{
// declare local vaiables
//
int total = 0;
int error = 0;
boolean present = false;
double classification = 0.0;
// the first set is the closest one - determine the error
//
if (closest == 1)
{
for (int i = 0; i < cluster.size(); total++, i++)
{
// reset the present flag
//
present = false;
// retrieve the next cluster point
//
MyPoint cst = (MyPoint)cluster.elementAt(i);
for (int j = 0; j < set1_d.size(); j++)
{
// retrieve the next point from the data set
//
MyPoint pnt = (MyPoint)set1_d.elementAt(j);
// check if the point is present
//
if (cst.x == pnt.x && cst.y == pnt.y)
{
present = true;
}
}
// error if the point is not found in the set
//
if (!present)
{
error++;
}
}
}
// the second set is the closest one - determine the error
//
if (closest == 2)
{
for (int i = 0; i < cluster.size(); total++, i++)
{
// reset the present flag
//
present = false;
// retrieve the next cluster point
//
MyPoint cst = (MyPoint)cluster.elementAt(i);
for (int j = 0; j < set2_d.size(); j++)
{
// retrieve the next point from the data set
//
MyPoint pnt = (MyPoint)set2_d.elementAt(j);
// check if the point is present
//
if (cst.x == pnt.x && cst.y == pnt.y)
{
present = true;
}
}
// error if the point is not found in the set
//
if (!present)
{
error++;
}
}
}
// the third set is the closest one - determine the error
//
if (closest == 3)
{
for (int i = 0; i < cluster.size(); total++, i++)
{
// reset the present flag
//
present = false;
// retrieve the next cluster point
//
MyPoint cst = (MyPoint)cluster.elementAt(i);
for (int j = 0; j < set3_d.size(); j++)
{
// retrieve the next point from the data set
//
MyPoint pnt = (MyPoint)set3_d.elementAt(j);
// check if the point is present
//
if (cst.x == pnt.x && cst.y == pnt.y)
{
present = true;
}
}
// error if the point is not found in the set
//
if (!present)
{
error++;
}
}
}
// the forth set is the closest one - determine the error
//
if (closest == 4)
{
for (int i = 0; i < cluster.size(); total++, i++)
{
// reset the present flag
//
present = false;
// retrieve the next cluster point
//
MyPoint cst = (MyPoint)cluster.elementAt(i);
for (int j = 0; j < set4_d.size(); j++)
{
// retrieve the next point from the data set
//
MyPoint pnt = (MyPoint)set4_d.elementAt(j);
// check if the point is present
//
if (cst.x == pnt.x && cst.y == pnt.y)
{
present = true;
}
}
// error if the point is not found in the set
//
if (!present)
{
error++;
}
}
}
// return the misclassified samples
//
return (int)error;
}
/**
* Computes the k-mean decision region - nearest neighbor algorithm
*
* @@param vec vector of initial guesses
*
* @@return vector of desision region points
*/
public Vector<MyPoint> getDecisionRegion(Vector<MyPoint> vec)
{
// declare local variables
//
double currentX = scale.xmin;
double currentY = scale.ymin;
MyPoint point;
int outputHeight = output_panel_d.disp_area_d.getYPrecision();
int outputWidth = output_panel_d.disp_area_d.getXPrecision();
double dist = 0.0;
double cdist = 0.0;
double smallestSoFar = Double.MAX_VALUE;
double incrementY = (scale.ymax-scale.ymin) / outputHeight;
double incrementX = (scale.xmax-scale.xmin) / outputWidth;
int associated = 0;
int outputCanvas[][] = new int[outputWidth][outputHeight];
Vector<MyPoint> decision = new Vector<MyPoint>(100, 10);
pro_box_d.setProgressMin(0);
pro_box_d.setProgressMax(outputWidth);
pro_box_d.setProgressCurr(0);
for(int i = 0; i < outputWidth; i++)
{
currentX += incrementX;
currentY = scale.ymin;
pro_box_d.setProgressCurr(i);
for(int j = 0; j < outputHeight; j++)
{
currentY += incrementY;
MyPoint pixel = new MyPoint(currentX, currentY); smallestSoFar = Double.MAX_VALUE;
// find the closest point from the guesses
//
for (int k = 0; k < vec.size(); k++)
{
double smallest = Double.MAX_VALUE;
point = (MyPoint)vec.elementAt(k);
dist = MathUtil.distance(pixel.x, pixel.y,
point.x, point.y);
if(dist < smallestSoFar)
{
int l = 0;
// find the distance between the cluster mean
// and the global mean of the first class
//
if(set1_d.size() > 0)
{
MyPoint mean1 = (MyPoint)
point_means_d.elementAt(l);
l++;
cdist = MathUtil.distance(point.x, point.y,
mean1.x, mean1.y);
if(cdist < smallest)
{
associated = 0;
smallest = cdist;
}
}
// find the distance between the cluster mean
// and the global mean of the second class
//
if(set2_d.size() > 0)
{
MyPoint mean2 = (MyPoint)
point_means_d.elementAt(l);
l++;
cdist = MathUtil.distance(point.x, point.y,
mean2.x, mean2.y);
if(cdist < smallest)
{
associated = 1;
smallest = cdist;
}
}
// find the distance between the cluster mean
// and the global mean of the third class
//
if(set3_d.size() > 0)
{
MyPoint mean3 = (MyPoint)
point_means_d.elementAt(l);
l++;
cdist = MathUtil.distance(point.x, point.y,
mean3.x, mean3.y);
if(cdist < smallest)
{
associated = 2;
smallest = cdist;
}
}
// find the distance between the cluster mean
// and the global mean of the forth class
//
if(set4_d.size() > 0)
{
MyPoint mean4 = (MyPoint)
point_means_d.elementAt(l);
l++;
cdist = MathUtil.distance(point.x, point.y,
mean4.x, mean4.y);
if(cdist < smallest)
{
associated = 3;
smallest = cdist;
}
}
// set the smallest distance so far
//
smallestSoFar = dist;
}
}
// put and entry in the output canvas array to
// indicate which class the current pixel is closest to
//
outputCanvas[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 != outputCanvas[i][j - 1] ||
associated != outputCanvas[i - 1][j])
{
decision.addElement(pixel);
}
}
}
}
// return the vector of decision region points
//
return decision;
}
/**
* displays the decision region on the output panel
*/
public void outputDecisionRegion()
{
output_panel_d.clear();
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);
output_panel_d.addOutput(decision_regions_d.getGuesses(),
Classify.PTYPE_OUTPUT_LARGE,
Color.black);
Vector<MyPoint> decision = getDecisionRegion(guesses);
output_panel_d.addOutput(decision,
Classify.PTYPE_INPUT,
Color.black);
output_panel_d.repaint();
}
}
@1.10log@Trying to establish new copy of AlgorithmKMeans.java in RCS, having problems.@text@d149 1a149 1 // append message to process box
d152 2a153 1
@1.9log@revision of warnings with vector declarations.@text@d11 1@1.8log@Comments are up to Javadoc standards.@text@d37 1a37 1 Vector guesses;
d42 1a42 1 Vector data_pool_d;
d97 1a97 1 data_pool_d = new Vector();
d99 1a99 1 point_means_d = new Vector();
d103 1a103 1 description_d = new Vector();
d134 10a143 5 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();
d443 1a443 1 guesses = new Vector(numMeans, 10);
d468 1a468 1 decision_regions_d.addRegion((Vector)set1_d.clone());
d475 1a475 1 decision_regions_d.addRegion((Vector)set2_d.clone());
d482 1a482 1 decision_regions_d.addRegion((Vector)set3_d.clone());
d489 1a489 1 decision_regions_d.addRegion((Vector)set4_d.clone());
d494 1a494 1 decision_regions_d.setGuesses((Vector)guesses.clone());
d590 1a590 1 region.setGuesses((Vector)guesses.clone());
d596 1a596 1 * @@param Point mean of the cluster
d691 2a692 1 * cluster the data points which form a closest cluster
d874 1a874 1 public Vector getDecisionRegion(Vector vec)
d898 1a898 1 Vector decision = new Vector(100, 10);
d1031 1a1031 1 return (Vector)decision;
d1054 1a1054 1 Vector decision = getDecisionRegion(guesses);
@1.7log@finer cosmetic changes.@text@d4 2a5 1// Last Edited : Sanjay Patil
d22 3d26 6d33 4d38 4d44 3d49 3a51 2 // declare the random number generator
//
d53 4d59 3d63 4d68 4d74 8d87 1a87 1 * @@return boolean
d155 1a155 1 * @@return boolean
d194 1a194 1 * @@return boolean
d219 1a219 1 * @@return boolean
a454 1 *
a455 1 *
d595 1a595 1 */
@1.6log@minor changes@text@d1 12a12 9/*
* AlgorithmKMeans.java V6.0 03/15/2005
*
* Author Phil Trasatti, Created on Jul 15, 2003
* Describes the K nearest neighbor algorithms
*/
d16 3d34 16a49 2 int iterations = 10;
int numguesses = 4;
d51 11a61 41 /** (non-Javadoc)
* @@see IFAlgorithm#initialize()
*/
public boolean initialize()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -