⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 algorithmlbg.java,v

📁 包含了模式识别中常用的一些分类器设计算法
💻 JAVA,V
📖 第 1 页 / 共 3 页
字号:
		    // 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 binary deviates after each iteraion
    * 
    * @@param  decisionRegions region: classified data sets
    */
    public void computeBinaryDeviates(Vector decisionRegions) 
    {

       	// determine the number of classified data sets
	//
	int numsets = decisionRegions.size();

	// remove all previous guesses - mean points
	//
	Vector<MyPoint> guesses = new Vector<MyPoint>(2, 2);
	
	// iterate over the data sets to determine the new means
	//
	for (int i = 0; i < numsets; i++) 
	{
	    // retrieve the classified region
	    //
	    Vector dataset = (Vector)
                            ((DataSet)
                             decisionRegions.elementAt(i)).getDataSet();
	    
	    // determine the mean of the classified region
	    //
	    MyPoint mean = MathUtil.computeClusterMean(dataset);

	    // determine the standard deviation of the pool
	    //
	    MyPoint deviation = clusterDeviation(dataset, mean);

	    // generate the initial guesses for the data set
	    //
	    double xsplit1 = mean.x - deviation.x;
	    double ysplit1 = mean.y + deviation.y;
	    double xsplit2 = mean.x + deviation.x;
	    double ysplit2 = mean.y - deviation.y;
	    
	    // set the initial guesses for the data set
	    //
	    guesses.addElement(new MyPoint(xsplit1, ysplit1));
	    guesses.addElement(new MyPoint(xsplit2, ysplit2));
	}

	// add the newly determined guesses to the region
	//
	decision_regions_d.setGuesses(guesses);
				      //(Vector<MyPoint>)guesses.clone());
    }
    
    /**
     * Calculates the standard deviation of the cluster 
     *
     * @@param  cluster cluster of data points
     * @@param  mean mean of the cluster
     *
     * @@return MYPoint standard deviation of the cluster along
     *         x direction and y direction.
     * @@see    MyPoint
     */
    public MyPoint clusterDeviation(Vector cluster, MyPoint mean) 
    {

	// declare local variables
	//
	double xval = 0.0;
	double yval = 0.0;

	// iterate over all points in the cluster
	//
	for (int i = 0; i < cluster.size(); i++) 
	{
	    // retrieve a point from the cluster
	    //
	    MyPoint point = (MyPoint)cluster.elementAt(i);

	    // compute the sum of the individual points
	    //
	    xval += ((point.x - mean.x) * (point.x - mean.x));
	    yval += ((point.y - mean.y) * (point.y - mean.y));
	}

	// determine the variance of the cluster
	//
	xval = (double)xval / (double)(cluster.size() - 1);
	yval = (double)yval / (double)(cluster.size() - 1);

	// determine the standard deviation of the cluster
	//
	xval = Math.sqrt((double)xval);
	yval = Math.sqrt((double)yval);

	// return the standard deviation of the cluster
	//
	return (new MyPoint(xval, yval));
    }

    /**
     * Classifies the data sets based on the k-means iterative algorithm
     * 
     * @@param guesses  stored data sets from the classification
     */
    public void classify(Vector guesses) 
    {

	// iterate over all points in the current data pool
	//
	for (int i = 0; i < data_pool_d.size(); i++) 
	{
	    // varables to determine the closest point to the mean
	    //
	    double dist = 0.0;
	    int associated = 0;
	    double smallestSoFar = Double.MAX_VALUE;

	    // retrieve a point from the pool
	    //
	    MyPoint point = (MyPoint)data_pool_d.elementAt(i);

	    // iterate over all guesses - means
	    //
	    for (int j = 0; j < guesses.size(); j++) 
	    {
		// retrieve one of the guesses
		//
		MyPoint mean = (MyPoint)guesses.elementAt(j);

		// determine the distance of the point from the mean
		//
		dist = MathUtil.distance(point.x, point.y, mean.x, mean.y); 

		// store the smallest distance
		//
		if(dist < smallestSoFar) 
		{
		    associated = j;
		    smallestSoFar = dist;
		}
	    }
		
	    // store the point based on the classification
	    //
	    String name = new String("cluster");
	    name = name + associated;
	    decision_regions_d.addPoint(point, name);
	}
    }

    /**
     * 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 regoin on output panel
     * @@see OutputPanel
     */
    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> guesses = decision_regions_d.getGuesses();

	Vector<MyPoint> decision = getDecisionRegion(guesses);

  	output_panel_d.addOutput(decision, 
				 Classify.PTYPE_INPUT, 
				 Color.black);

	output_panel_d.repaint();
    }    
}
@1.8log@Fixed Javadoc comments.@text@d27 3a29 3    Vector support_vectors_d;
    Vector data_pool_d;
    Vector cbinary_d;
d46 1a46 1	data_pool_d = new Vector();
d48 2a49 2	point_means_d = new Vector();
	support_vectors_d = new Vector();
d51 1a51 1	description_d = new Vector();
d54 1a54 1	cbinary_d = new Vector();
d83 9a91 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();
d97 1a97 1	// append message to process box
d100 2a101 1		
d161 2a162 1	decision_regions_d.addRegion((Vector)data_pool_d.clone());
d164 1a164 1	Vector guesses = new Vector(2, 2);
d172 2a173 1	decision_regions_d.setGuesses((Vector)guesses.clone());
d209 1a209 1	    Vector decisionRegions = decision_regions_d.getRegions();
d217 1a217 1	    Vector guesses = decision_regions_d.getGuesses();
d407 1a407 1    * @@param  Point mean: mean of the cluster
d685 1a685 1	Vector guesses = new Vector(2, 2);
d720 2a721 1	decision_regions_d.setGuesses((Vector)guesses.clone());
d829 1a829 1    public Vector getDecisionRegion(Vector vec) 
d852 1a852 1	Vector decision = new Vector(100, 10);
d983 1a983 1	return (Vector)decision;
d1007 1a1007 1	Vector guesses = decision_regions_d.getGuesses();
d1009 1a1009 1	Vector decision = getDecisionRegion(guesses);
@1.7log@*** empty log message ***@text@d37 1a37 1    * @@return   boolean
d105 1a105 1    * @@return boolean
d144 1a144 1     * @@return   boolean
d183 1a183 1     * @@return boolean
d490 8d718 2a719 2     * @@param  Vector cluster: cluster of data points
     * @@param  Point: mean of the cluster
d816 1a816 1     * @@param   Vector vec: vector of initial guesses
@1.6log@some minor changes related Java Documentation style.@text@d17 1a17 1 * implements the K nearest neighbor algorithms
@1.5log@minor changes to adhere to Java Documentation Style.@text@d1 8a8 6/**
* AlgorithmLBG.java v6.0 03/15/2005
*
* Author: Phil Trasatti, last edited Daniel May Created: 7/15/03 
* 
*/
d16 3a18 1
d22 1a22 13    //-----------------------------------------------------------------
    //
    // static data members
    //
    //-----------------------------------------------------------------

    //-----------------------------------------------------------------
    //
    // primitive data members
    //
    //-----------------------------------------------------------------

    int output_canvas_d[][];   
a25 6    //-----------------------------------------------------------------
    //
    // instance data members
    //
    //-----------------------------------------------------------------
        
a31 7    //---------------------------------------------------------------
    //
    // class methods
    //
    //---------------------------------------------------------------


a32 1    *
a37 1    *
a102 1    *
a105 1    * 
a140 1     *
a144 2     *
     * 
a180 1     * 
a183 1     * 
d310 1a310 2	    pro_box_d.appendMessage(message + "\n");
	    
a318 1    *
a320 1    *
d329 5a333 5	    {
		disableControl();
		step1();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -