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

📄 algorithmlda2.java

📁 完整的模式识别库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	    M4.resetMatrix();
	}

	int j = 0;
	
	// obtain the means of each individual class
	//
	if (set1_d.size() > 0)
       	{
	    MyPoint p = (MyPoint)point_means_d.elementAt(j);
	    j++;
	    xmean1 = p.x;
	    ymean1 = p.y;
	}
	
	if (set2_d.size() > 0)
	{
	    MyPoint p = (MyPoint)point_means_d.elementAt(j);
	    j++;
	    xmean2 = p.x;
	    ymean2 = p.y;
	}

       	if (set3_d.size() > 0)
       	{
	    MyPoint p = (MyPoint)point_means_d.elementAt(j);
	    j++;
	    xmean3 = p.x;
	    ymean3 = p.y;
	}
	
	if (set4_d.size() > 0)
	{
	    MyPoint p = (MyPoint)point_means_d.elementAt(j);
	    j++;
	    xmean4 = p.x;
	    ymean4 = p.y;
	}
	
	// compute the mean of all points outside the first set
	//
	if ((set2_d.size() > 0) || (set3_d.size() > 0) || (set4_d.size() > 0))
       	{
	    int samples = 0;
	    
	    for (int i = 0; i < set2_d.size(); samples++, i++)
	    {
		MyPoint p2 = (MyPoint)set2_d.elementAt(i);
		xout1 += p2.x;
		yout1 += p2.y;
	    }
	    for (int i = 0; i < set3_d.size(); samples++, i++)
	    {
		MyPoint p2 = (MyPoint)set3_d.elementAt(i);
		xout1 += p2.x;
		yout1 += p2.y;
	    }
	    for (int i = 0; i < set4_d.size(); samples++, i++)
	    {
		MyPoint p2 = (MyPoint)set4_d.elementAt(i);
		xout1 += p2.x;
		yout1 += p2.y;
	    }
	    xout1 /= samples;
	    yout1 /= samples;
	}
	
	// compute the mean of all points outside the second set
	//
	if ((set1_d.size() > 0) || (set3_d.size() > 0) || (set4_d.size() > 0))
        {
	    
	    int samples = 0;
	    
	    for (int i = 0; i < set1_d.size(); samples++, i++)
	    {			
		MyPoint p2 = (MyPoint)set1_d.elementAt(i);
		xout2 += p2.x;
		yout2 += p2.y;
	    }
	    for (int i = 0; i < set3_d.size(); samples++, i++)
            {
		MyPoint p2 = (MyPoint)set3_d.elementAt(i);
		xout2 += p2.x;
		yout2 += p2.y;
	    }
	    for (int i = 0; i < set4_d.size(); samples++, i++)
	    {
		MyPoint p2 = (MyPoint)set4_d.elementAt(i);
		xout2 += p2.x;
		yout2 += p2.y;
	    }
	    xout2 /= samples;
	    yout2 /= samples;
	}
	
	// compute the mean of all points outside the third set
	//
	if ((set1_d.size() > 0) || (set2_d.size() > 0) || (set4_d.size() > 0))
       	{
	    int samples = 0;
	    
	    for (int i = 0; i < set1_d.size(); samples++, i++)
	    {
		MyPoint p2 = (MyPoint)set1_d.elementAt(i);
		xout3 += p2.x;
		yout3 += p2.y;
	    }
	    for (int i = 0; i < set2_d.size(); samples++, i++)
	    {
		MyPoint p2 = (MyPoint)set2_d.elementAt(i);
		xout3 += p2.x;
		yout3 += p2.y;
	    }
	    for (int i = 0; i < set4_d.size(); samples++, i++)
	    {
		MyPoint p2 = (MyPoint)set4_d.elementAt(i);
		xout3 += p2.x;
		yout3 += p2.y;
	    }
	    xout3 /= samples;
	    yout3 /= samples;
	}

	// compute the mean of all points outside the forth set
	//
	if ((set1_d.size() > 0) || (set2_d.size() > 0) || (set3_d.size() > 0))
       	{   
	    int samples = 0;
	    
	    for (int i = 0; i < set1_d.size(); samples++, i++)
	    {
		
		MyPoint p2 = (MyPoint)set1_d.elementAt(i);
		xout4 += p2.x;
		yout4 += p2.y;
		
	    }
	    for (int i = 0; i < set2_d.size(); samples++, i++)
	    {
		MyPoint p2 = (MyPoint)set2_d.elementAt(i);
		xout4 += p2.x;
		yout4 += p2.y;
		
	    }
	    for (int i = 0; i < set3_d.size(); samples++, i++)
	    {
		MyPoint p2 = (MyPoint)set3_d.elementAt(i);
		xout4 += p2.x;
		yout4 += p2.y;
	    }
	    xout4 /= samples;
	    yout4 /= samples;
	}
	
	// compute the between class scatter of the first set
	//
	if (set1_d.size() > 0)
	{   
	    Matrix S = new Matrix();
	    Matrix M = new Matrix();
	    Matrix T = new Matrix();
	    
	    mean[0][0] = xmean1 - xout1;
	    mean[0][1] = ymean1 - yout1;
	    M.initMatrix(mean, 1, 2);
	    
	    transpose[0][0] = xmean1 - xout1;
	    transpose[1][0] = ymean1 - yout1;
	    T.initMatrix(transpose, 2, 1);
	    
	    T.multMatrix(M, S);
	    M1.addMatrix(S);
	}
	
	// compute the between class scatter contribution of the second set
	//
	if (set2_d.size() > 0)
       	{   
	    Matrix S = new Matrix();
	    Matrix M = new Matrix();
	    Matrix T = new Matrix();
	    
	    mean[0][0] = xmean2 - xout2;
	    mean[0][1] = ymean2 - yout2;
	    M.initMatrix(mean, 1, 2);
	    
	    transpose[0][0] = xmean2 - xout2;
	    transpose[1][0] = ymean2 - yout2;
	    T.initMatrix(transpose, 2, 1);
	    
	    T.multMatrix(M, S);
	    M2.addMatrix(S);
	}
	
	// compute the between class scatter contribution of the third set
	//
	if (set3_d.size() > 0)
	{   
	    Matrix S = new Matrix();
	    Matrix M = new Matrix();
	    Matrix T = new Matrix();
	    
	    mean[0][0] = xmean3 - xout3;
	    mean[0][1] = ymean3 - yout3;
	    M.initMatrix(mean, 1, 2);
	    
	    transpose[0][0] = xmean3 - xout3;
	    transpose[1][0] = ymean3 - yout3;
	    T.initMatrix(transpose, 2, 1);
	    
	    T.multMatrix(M, S);
	    M3.addMatrix(S);
	}
	
	// compute the between class scatter contribution of the fourth set
	//
	if (set4_d.size() > 0)
	{	    
	    Matrix S = new Matrix();
	    Matrix M = new Matrix();
	    Matrix T = new Matrix();
	    
	    mean[0][0] = xmean4 - xout4;
	    mean[0][1] = ymean4 - yout4;
	    M.initMatrix(mean, 1, 2);
	    
	    transpose[0][0] = xmean4 - xout4;
	    transpose[1][0] = ymean4 - yout4;
	    T.initMatrix(transpose, 2, 1);
	    
	    T.multMatrix(M, S);
	    M4.addMatrix(S);
	}
    }
    

    /**
     * Counts the data points in each set in error and displays
     * them on the text message window
     */
    public void computeErrors()
    {
	// declare local variables
	//
	String text;
	double error;
	int samples = 0;
	int samples1 = 0;
	int samples2 = 0;
	int samples3 = 0;
	int samples4 = 0;
	
	int incorrect = 0;
	int incorrect1 = 0;
	int incorrect2 = 0;
	int incorrect3 = 0;
	int incorrect4 = 0;
	
	DisplayScale scale = output_panel_d.disp_area_d.getDisplayScale(); 

	// set scales
	//
	int outputWidth = output_panel_d.disp_area_d.getXPrecision();
	int outputHeight = output_panel_d.disp_area_d.getYPrecision();
	
	double incrementY = (scale.ymax - scale.ymin) / outputHeight;
	double incrementX = (scale.xmax - scale.xmin) / outputWidth;
	
	// compute the classification error for the first set
	//
	for (int i = 0; i < set1_d.size(); i++)
	{
	    
	    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++;
		}
	    }
	}
	
	if (set1_d.size() > 0)
	{
	    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);
	}
	
	// compute the classification error for the second set
	//
	for (int i = 0; i < set2_d.size(); i++)
	{
	    
	    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++;
		}
	    }
	}
	
	if (set2_d.size() > 0)
	{
	    
	    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);
	}
	
	// compute the classification error for the third set
	//
	for (int i = 0; i < set3_d.size(); i++)
	{  
	    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++;
		}
	    }
	}
	
	if (set3_d.size() > 0)
	{   
	    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);
	}
	
	// compute the classification error for the forth set
	//
	for (int i = 0; i < set4_d.size(); i++)
	{   
	    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++;
		}
	    }
	}
	
	if (set4_d.size() > 0)
	{   
	    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);
	}
	
	// compute the overall classification error
	//
	samples = samples1 + samples2 + samples3 + samples4;
	incorrect = incorrect1 + incorrect2 + incorrect3 + incorrect4;
	error = ((double)incorrect / (double)samples) * 100.0;
	
	text =
	    new String(
		       "       Overall results:\n"
		       + "          Total number of samples: "
		       + samples
		       + "\n"
		       + "          Misclassified samples: "
		       + incorrect
		       + "\n"
		       + "          Classification error: "
		       + MathUtil.setDecimal(error, 2)
		       + "%");
	
	pro_box_d.appendMessage(text);
    }

    /**
     * Display two matrices - covariance matrix and the transformation
     * matrix in the text message window
     */
    public void printMatrices()
    {
	double a11, a12, a21, a22;
	String text;

	if (set1_d.size() > 0)
	{
	    pro_box_d.appendMessage("      Set 1");
	    
	    a11 = MathUtil.setDecimal(CLDA1.Elem[0][0], 2);
	    a12 = MathUtil.setDecimal(CLDA1.Elem[0][1], 2);
	    a21 = MathUtil.setDecimal(CLDA1.Elem[1][0], 2);
	    a22 = MathUtil.setDecimal(CLDA1.Elem[1][1], 2);
	    
	    text = new String("      Covariance matrix:\n" +
			      "         " + a11 + "    " + a12 + "\n" +
			      "         " + a21 + "    " + a22);
	    
	    pro_box_d.appendMessage(text + "\n");
	    
	    a11 = MathUtil.setDecimal(LDA1.Elem[0][0], 2);
	    a12 = MathUtil.setDecimal(LDA1.Elem[0][1], 2);
	    a21 = MathUtil.setDecimal(LDA1.Elem[1][0], 2);
	    a22 = MathUtil.setDecimal(LDA1.Elem[1][1], 2);
	    
	    text = new String("      Transformation matrix:\n" +
			      "         " + a11 + "    " + a12 + "\n" +
			      "         " + a21 + "    " + a22);
	    
	    pro_box_d.appendMessage(text + "\n");
	}

	if (set2_d.size() > 0)
	{
	    pro_box_d.appendMessage("      Set 2");
	    
	    a11 = MathUtil.setDecimal(CLDA2.Elem[0][0], 2);
	    a12 = MathUtil.setDecimal(CLDA2.Elem[0][1], 2);
	    a21 = MathUtil.setDecimal(CLDA2.Elem[1][0], 2);
	    a22 = MathUtil.setDecimal(CLDA2.Elem[1][1], 2);
	    
	    text = new String("      Covariance matrix:\n" +
			      "         " + a11 + "    " + a12 + "\n" +
			      "         " + a21 + "    " + a22);
	    
	    pro_box_d.appendMessage(text + "\n");
	    
	    a11 = MathUtil.setDecimal(LDA2.Elem[0][0], 2);
	    a12 = MathUtil.setDecimal(LDA2.Elem[0][1], 2);
	    a21 = MathUtil.setDecimal(LDA2.Elem[1][0], 2);
	    a22 = MathUtil.setDecimal(LDA2.Elem[1][1], 2);
	    
	    text = new String("      Transformation matrix:\n" +
			      "         " + a11 + "    " + a12 + "\n" +
			      "         " + a21 + "    " + a22);
	    
	    pro_box_d.appendMessage(text + "\n");
	}

	if (set3_d.size() > 0)
	{
	    pro_box_d.appendMessage("      Set 3");
	    
	    a11 = MathUtil.setDecimal(CLDA3.Elem[0][0], 2);
	    a12 = MathUtil.setDecimal(CLDA3.Elem[0][1], 2);
	    a21 = MathUtil.setDecimal(CLDA3.Elem[1][0], 2);
	    a22 = MathUtil.setDecimal(CLDA3.Elem[1][1], 2);
	    
	    text = new String("      Covariance matrix:\n" +
			      "         " + a11 + "    " + a12 + "\n" +
			      "         " + a21 + "    " + a22);
	    
	    pro_box_d.appendMessage(text + "\n");
	    
	    a11 = MathUtil.setDecimal(LDA3.Elem[0][0], 2);
	    a12 = MathUtil.setDecimal(LDA3.Elem[0][1], 2);
	    a21 = MathUtil.setDecimal(LDA3.Elem[1][0], 2);
	    a22 = MathUtil.setDecimal(LDA3.Elem[1][1], 2);
	    
	    text = new String("      Transformation matrix:\n" +
			      "         " + a11 + "    " + a12 + "\n" +
			      "         " + a21 + "    " + a22);
	    
	    pro_box_d.appendMessage(text + "\n");
	}

	if (set4_d.size() > 0)
	{
	    
	    pro_box_d.appendMessage("      Set 4");
	    
	    a11 = MathUtil.setDecimal(CLDA4.Elem[0][0], 2);
	    a12 = MathUtil.setDecimal(CLDA4.Elem[0][1], 2);
	    a21 = MathUtil.setDecimal(CLDA4.Elem[1][0], 2);
	    a22 = MathUtil.setDecimal(CLDA4.Elem[1][1], 2);
	    
	    text = new String("      Covariance matrix:\n" +
			      "         " + a11 + "    " + a12 + "\n" +
			      "         " + a21 + "    " + a22);
	    
	    pro_box_d.appendMessage(text + "\n");
	    
	    a11 = MathUtil.setDecimal(LDA4.Elem[0][0], 2);
	    a12 = MathUtil.setDecimal(LDA4.Elem[0][1], 2);
	    a21 = MathUtil.setDecimal(LDA4.Elem[1][0], 2);
	    a22 = MathUtil.setDecimal(LDA4.Elem[1][1], 2);
	    
	    text = new String("      Transformation matrix:\n" +
			      "         " + a11 + "    " + a12 + "\n" +
			      "         " + a21 + "    " + a22);
	    
	    pro_box_d.appendMessage(text + "\n");
	}
    }
}

⌨️ 快捷键说明

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