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

📄 algorithmpca.java

📁 包含了模式识别中常用的一些分类器设计算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/** * @(#) AlgorithmPCA.java   v6.0 05/23/2005 * last edited Ryan Irwin  * document created  02/09/03 * * Defines the operation of the PCA Class-Independent Algorithm * */// import java packages//import java.awt.*;import java.util.*;/** * Operation of the PCA Class-Independent Algorithm */public class AlgorithmPCA extends Algorithm{        //-----------------------------------------------------------------    //    // instance data members    //    //-----------------------------------------------------------------        // vector of support region points    //    Matrix trans_matrix_d = new Matrix();    Matrix cov_matrix_d = new Matrix();    Vector<MyPoint> support_vectors_d = new Vector<MyPoint>();    Vector<MyPoint> decision_regions_d = new Vector<MyPoint>();    int output_canvas_d[][];        //-----------------------------------------------------------------    //    // public class methods    //    //------------------------------------------------------------------    /**     * Overrides the initialize() method in the base class.  Initializes     * member data and prepares for execution of first step.  This method     * "resets" the algorithm.     *     * @return true     */    public boolean initialize()    {	// Debug	//	// System.out.println(algo_id + ": initialize()");		trans_matrix_d = new Matrix();	cov_matrix_d = new Matrix();	support_vectors_d = new Vector<MyPoint>();	point_means_d = new Vector<MyPoint>();	decision_regions_d = new Vector<MyPoint>();	step_count = 3;	algo_id = "AlgorithmPCA";		// add the process description for the PCA algorithm	//	if (description_d.size() == 0)        {	    String str = new String("   0. Initialize the original data.");	    description_d.addElement(str);	    	    str = new String("   1. Displaying the original data.");	    description_d.addElement(str);	    	    str = new String("   2. Computing the support regions.");	    description_d.addElement(str);	    	    str = new String("   3. Computing the decision regions based on the class independent principal component analysis algorithm.");	    description_d.addElement(str);	}	    	// append message to process box	//	pro_box_d.appendMessage("Class Independent Principal" 				+ " Component Analysis:" + "\n");	// set the data points for this algorithm	//	//	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();	//	set1_d = data_points_d.dset1;	set2_d = data_points_d.dset2;	set3_d = data_points_d.dset3;	set4_d = data_points_d.dset4;		// set the step index	//	step_index_d = 0;		// append message to process box	//	pro_box_d.appendMessage((String)description_d.get(step_index_d));		// exit gracefully	//	return true;    }        /**     * Implementation of the run function from the Runnable interface.     * Determines what the current step is and calls the appropriate method.     */    public void run()    {	// Debug	//	// System.out.println(algo_id + ": run()");		if (step_index_d == 1)	{	    disableControl();	    step1();	    enableControl();	}	            else if (step_index_d == 2)	{	    disableControl();	    step2(); 	    enableControl();	}		else if (step_index_d == 3)        {	    disableControl();	    step3();	    pro_box_d.appendMessage("   Algorithm Complete");	    enableControl(); 	}		// exit gracefully	//	return;    }        /**     *     * step one of the algorithm. Scales the display to fit the plot.     *     * @return true     */    boolean step1()    {	// Debug	//	// System.out.println(algo_id + ": step1()");		pro_box_d.setProgressMin(0);	pro_box_d.setProgressMax(1);	pro_box_d.setProgressCurr(0);		scaleToFitData();		// Display original data	//	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);		// step 1 completed	//	pro_box_d.setProgressCurr(1);	output_panel_d.repaint();		// exit gracefully	//	return true;    }    /**     *     * step two of the algorithm. Finds the PCA for the given data     *     * @return true     */    boolean step2()    {	// Debug	//System.out.println(algo_id + ": step2()");			pro_box_d.setProgressMin(0);	pro_box_d.setProgressMax(20);	pro_box_d.setProgressCurr(0);		// append message to process box	//	transformPCA();	printMatrices();	computeMeans();		// display means	//	output_panel_d.addOutput(point_means_d, Classify.PTYPE_OUTPUT_LARGE, 				 Color.black);		// display support vectors	//	output_panel_d.addOutput(support_vectors_d, Classify.PTYPE_INPUT, 				 Color.cyan);		// display support vectors	//	pro_box_d.setProgressCurr(20);	output_panel_d.repaint();		// exit gracefully	//	return true;    }    /**     *     * step three of the algorithm. Computes the decision regions     *     * @return true     */    boolean step3()    {	// Debug	//	// System.out.println(algo_id + ": step3()");		pro_box_d.setProgressMin(0);	pro_box_d.setProgressMax(20);	pro_box_d.setProgressCurr(0);		// compute the decision regisions	//	computeDecisionRegions();	    	// compute errors	//	computeErrors();		// display support vectors	//	// display support vectors	//	output_panel_d.addOutput(decision_regions_d, Classify.PTYPE_INPUT, 				 new Color(255, 200, 0));		output_panel_d.repaint();		// exit gracefully	//	return true;    }        /**     *      * transforms a given set of points to a new space     * using the class independent principal component analysis algorithm     *     */    public void transformPCA()    {	// Debug	// 	// System.out.println(algo_id + ": transformPCA()");	    	// declare local variables	//	int size = 0;	int xsize = 0;	int ysize = 0;	    	// declare variables to compute the global mean	//	double xval = 0.0;	double yval = 0.0;	double xmean = 0.0;	double ymean = 0.0;	    	// declare the covariance object	//	Covariance cov = new Covariance();	    	// declare an eigen object	//	// Since Eigen is a class of static member functions 	// it is not correct to instantiate it - Phil T. 6-23-03 	//Eigen eigen = new Eigen();		// declare the covariance matrix	//	Matrix covariance = new Matrix();	covariance.row = covariance.col = 2;	covariance.Elem = new double[2][2];	    	// declare arrays for the eigenvalues	//	double eigVal[] = null;	    	// declare an array to store the eigen vectors	//	double eigVec[] = new double[2];	    	// declare matrix objects	//	Matrix T = new Matrix();	Matrix M = new Matrix();	Matrix W = new Matrix();	    	// allocate memory for the matrix elements	//	T.Elem = new double[2][2];	M.Elem = new double[2][2];	W.Elem = new double[2][2];	    	// declare arrays to store the samples	//	double x[] = null;	double y[] = null;	    	// declare the maximum size of all data sets together	//	int maxsize = set1_d.size() + set2_d.size() 	            + set3_d.size() + set4_d.size();	    	// initialize arrays to store the samples	//	x = new double[maxsize];	y = new double[maxsize];	    	// get the samples from the first data set	//	size = set1_d.size();	    	// set up the initial random vectors i.e., the vectors of	// X and Y coordinate points form the display	//	for (int i = 0; i < size; i++)        {	    MyPoint p = (MyPoint)set1_d.elementAt(i);	    xval += p.x;	    yval += p.y;	    x[xsize++] = p.x;	    y[ysize++] = p.y;	}		// get the samples from the second data set	//	size = set2_d.size();		// set up the initial random vectors i.e., the vectors of	// X and Y coordinate points form the display	//	for (int i = 0; i < size; i++)        {	    MyPoint p = (MyPoint)set2_d.elementAt(i);	    xval += p.x;	    yval += p.y;	    x[xsize++] = p.x;	    y[ysize++] = p.y;	}		// get the samples from the third data set	//	size = set3_d.size();		// set up the initial random vectors i.e., the vectors of	// X and Y coordinate points form the display	//	for (int i = 0; i < size; i++)        {	    MyPoint p = (MyPoint)set3_d.elementAt(i);	    xval += p.x;	    yval += p.y;	    x[xsize++] = p.x;	    y[ysize++] = p.y;	}		// get the samples from the first data set	//	size = set4_d.size();		// set up the initial random vectors i.e., the vectors of	// X and Y coordinate points form the display	//	for (int i = 0; i < size; i++)        {	    MyPoint p = (MyPoint)set4_d.elementAt(i);	    xval += p.x;	    yval += p.y;	    x[xsize++] = p.x;	    y[ysize++] = p.y;	}		if (maxsize > 0)	{	    	    // initialize the transformation matrix dimensions	    //	    W.row = 2;	    W.col = 2;	    	    // reset the matrices	    //	    W.resetMatrix();	    	    // compute the covariance matrix of the first data set	    //	    covariance.Elem = cov.computeCovariance(x, y);	    cov_matrix_d = covariance;	    	    // initialize the matrix needed to compute the eigenvalues	    //	    T.initMatrix(covariance.Elem, 2, 2);	    	    // make a copy of the original matrix	    //	    M.copyMatrix(T);	    	    // compute the eigen values	    //	    // Changed eigen to Eigen since member function is static -	    // Phil T. 6-23-03	    eigVal = Eigen.compEigenVal(T);	    	    // compute the eigen vectors	    //	    for (int i = 0; i < 2; i++)	    {		// Changed eigen to Eigen since member function is static - 		// Phil T. 6-23-03		Eigen.calcEigVec(M, eigVal[i], eigVec);		for (int j = 0; j < 2; j++)		{		    W.Elem[j][i] = eigVec[j] / Math.sqrt(eigVal[i]);		}	    }	    	    // save the transformation matrix 	    //	    trans_matrix_d = W;	}		// compute the global mean of the data sets	//	xmean = xval / xsize;	ymean = yval / ysize;		// determine points for the support regions	//	double val[][] = new double[2][1];		Matrix invT = new Matrix();	Matrix supp = new Matrix();	Matrix temp = new Matrix();		// set up the angle with which to rotate the axis	//	double theta = 0.0;	double alpha = cov_matrix_d.Elem[0][0] - cov_matrix_d.Elem[1][1];	double beta = -2 * cov_matrix_d.Elem[0][1];	    	if (eigVal[0] > eigVal[1])

⌨️ 快捷键说明

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