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

📄 algorithmlbg.java_2

📁 包含了模式识别中常用的一些分类器设计算法
💻 JAVA_2
📖 第 1 页 / 共 2 页
字号:
	    return;
	}

	// add all points from the first data set
	//
	for (int i=0; i<set1_d.size(); i++) {	    
	    data_pool_d.addElement((MyPoint)set1_d.elementAt(i));
	}

	// add all points from the second data set
	//
	for (int i=0; i<set2_d.size(); i++) {	    
	    data_pool_d.addElement((MyPoint)set2_d.elementAt(i));
	}

	// add all points from the third data set
	//
	for (int i=0; i<set3_d.size(); i++) {	    
	    data_pool_d.addElement((MyPoint)set3_d.elementAt(i));
	}

	// add all points from the forth data set
	//
	for (int i=0; i<set4_d.size(); i++) {	    
	    data_pool_d.addElement((MyPoint)set4_d.elementAt(i));
	}
    }

    
    // method: getClosestSet
    //
    // arguments: 
    //    Point mean: mean of the cluster
    // 
    // return   : closest data set to the cluster
    //
    // method determines the closest data sets to the cluster
    //
    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;
    }

    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;
    }

    // method: computeBinaryDeviates
    // 
    // arguments:
    //    DecisionRegion region: classified data sets
    //
    // return   : none
    //
    // method computes the binary deviates after each iteraion
    //
    public void computeBinaryDeviates(DecisionRegion region) {

       	// determine the number of classified data sets
	//
	int numsets = region.getNumRegions();

	// remove all previous guesses - mean points
	//
	guesses_d.removeAllElements();
	
	// iterate over the data sets to determine the new means
	//
	for (int i=0; i<numsets; i++) {

	    // retrieve the classified region
	    //
	    Vector dataset = (Vector)region.getRegion(i);
	    
	    // 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
	    //
om	    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_d.addElement(new MyPoint(xsplit1, ysplit1));
	    guesses_d.addElement(new MyPoint(xsplit2, ysplit2));
	}

	// add the newly determined guesses to the region
	//
	decision_regions_d.setGuesses((Vector)guesses_d.clone());
    }

    // method: clusterDeviation
    //
    // arguments:
    //    Vector cluster: cluster of data points
    //    Point: mean of the cluster
    //
    // return   : standard deviation of the cluster
    //
    // method to calculate the standard deviation of the cluster 
    //
    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));
    }

    // method: classify
    // 
    // arguments:
    //    DecisionRegion region: stored data sets from the classification
    //
    // return   : none
    //
    // method classifies the data sets based on the k-means iterative algorithm
    //
    public void classify(DecisionRegion region) {

	// 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);

	    guesses_d = (Vector)region.getGuesses().clone();

	    // iterate over all guesses - means
	    //
	    for (int j=0; j<guesses_d.size(); j++) {

		// retrieve one of the guesses
		//
		MyPoint mean = (MyPoint)guesses_d.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);
	}
    }

}

⌨️ 快捷键说明

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