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

📄 algorithmpca.java,v

📁 包含了模式识别中常用的一些分类器设计算法
💻 JAVA,V
📖 第 1 页 / 共 4 页
字号:
head	1.7;access;symbols;locks; strict;comment	@# @;1.7date	2005.06.10.18.23.46;	author rirwin;	state Exp;branches;next	1.6;1.6date	2005.05.24.21.36.18;	author rirwin;	state Exp;branches;next	1.5;1.5date	2005.05.23.19.26.50;	author rirwin;	state Exp;branches;next	1.4;1.4date	2005.03.17.17.34.37;	author patil;	state Exp;branches;next	1.3;1.3date	2005.03.08.03.31.10;	author patil;	state Exp;branches;next	1.2;1.2date	2005.01.19.21.31.55;	author patil;	state Exp;branches;next	1.1;1.1date	2004.12.28.00.04.32;	author patil;	state Exp;branches;next	;desc@No changes made.@1.7log@Esablishing RCS version.@text@/** * @@(#) 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])	{	    theta = Math.atan2((alpha - Math.sqrt((alpha * alpha) 						  + (beta * beta))), beta);	}	else	{	    theta = Math.atan2((alpha + Math.sqrt((alpha * alpha) 						  + (beta * beta))), beta);	}		// compute the inverse of the transformation matrix	//	trans_matrix_d.invertMatrix(invT);	    	// loop through all points on the circumference of the 	// gaussian sphere in the transformed space	//	for (int i = 0; i < 360; i++)        {			    // get the x and y co-ordinates	    //	    val[0][0] = 1.5 * Math.cos(i);	    val[1][0] = 1.5 * Math.sin(i);	    	    // set up the points as a matrix in order for multiplication	    //	    temp.initMatrix(val, 2, 1);	    	    // transform the points from the feature space back to the	    // original space to create the support region for the data set	    //	    invT.multMatrix(temp, supp);	    	    // rotate the points after transforming them to the new space	    //	    xval = (supp.Elem[0][0] * Math.cos(theta)) 		 - (supp.Elem[1][0] * Math.sin(theta));	    yval = (supp.Elem[0][0] * Math.sin(theta)) 		 + (supp.Elem[1][0] * Math.cos(theta));	    	    // time shift the co-ordinates to the global mean	    //	    xval = xval + xmean;	    yval = yval + ymean;	    	    // add the point to the support region vector	    //	    MyPoint pt = new MyPoint(xval, yval);	    support_vectors_d.addElement(pt);	}    }    /**     * Computes the line of discrimination for the classification      * algorithms when the corresponding flags have been initialized     *     */    public void computeDecisionRegions()    {	// Debug	//	// System.out.println(algo_id + ": computeDecisionRegions()");		DisplayScale scale = output_panel_d.disp_area_d.getDisplayScale();		double currentX = scale.xmin;	double currentY = scale.ymin;		// set precision	//	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;		// declare a 2D array to store the class associations	//	output_canvas_d = new int[outputWidth][outputHeight];		// loop through each and every point on the pixmap and	// determine which class each pixel is associated with	//	MyPoint point;	double dist = 0.0;	int associated = 0;	double smallestSoFar = Double.MAX_VALUE;		int target = 0;	boolean set1flag = true;	boolean set2flag = true;	boolean set3flag = true;		pro_box_d.setProgressMin(0);	pro_box_d.setProgressMax(outputWidth);	pro_box_d.setProgressCurr(0);		for (int i = 0; i < outputWidth; i++)	{

⌨️ 快捷键说明

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