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

📄 algorithmlbg.java

📁 包含了模式识别中常用的一些分类器设计算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	// 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 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();
    }    
}

⌨️ 快捷键说明

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