pca 特征抽取

来源:互联网 发布:小米6陶瓷版 知乎 编辑:程序博客网 时间:2024/06/05 08:50

原文地址:http://blog.csdn.net/aalbertini/article/details/6256656

主成分分析 ( Principal Component Analysis , PCA )是一种掌握事物主要矛盾的统计分析方法,它可以从多元事物中解析出主要影响因素,揭示事物的本质,简化复杂的问题。计算主成分的目的是将高维数据投影到较低维空间。给定 n 个变量的 m 个观察值,形成一个 n ′ m 的数据矩阵, n 通常比较大。对于一个由多个变量描述的复杂事物,人们难以认识,那么是否可以抓住事物主要方面进行重点分析呢?如果事物的主要方面刚好体现在几个主要变量上,我们只需要将这几个变量分离出来,进行详细分析。但是,在一般情况下,并不能直接找出这样的关键变量。这时我们可以用原有变量的线性组合来表示事物的主要方面, PCA 就是这样一种分析方法。

  PCA 主要 用于数据降维,对于一系列例子的特征组成的多维向量,多维向量里的某些元素本身没有区分性,比如某个元素在所有的例子中都为1,或者与1差距不大,那么这个元素本身就没有区分性,用它做特征来区分,贡献会非常小。所以我们的目的是找那些变化大的元素,即方差大的那些维,而去除掉那些变化不大的维,从而使特征留下的都是“精品”,而且计算量也变小了。 对于一个k维的特征来说,相当于它的每一维特征与其他维都是正交的(相当于在多维坐标系中,坐标轴都是垂直的),那么我们可以变化这些维的坐标系,从而使这个特征在某些维上方差大,而在某些维上方差很小。例如,一个45度倾斜的椭圆,在第一坐标系,如果按照x,y坐标来投影,这些点的x和y的属性很难用于区分他们,因为他们在x,y轴上坐标变化的方差都差不多,我们无法根据这个点的某个x属性来判断这个点是哪个,而如果将坐标轴旋转,以椭圆长轴为x轴,则椭圆在长轴上的分布比较长,方差大,而在短轴上的分布短,方差小,所以可以考虑只保留这些点的长轴属性,来区分椭圆上的点,这样,区分性比x,y轴的方法要好!

  所以我们的做法就是求得一个k维特征的投影矩阵,这个投影矩阵可以将特征从高维降到低维。投影矩阵也可以叫做变换矩阵。新的低维特征必须每个维都正交,特征向量都是正交的。通过求样本矩阵的协方差矩阵,然后求出协方差矩阵的特征向量,这些特征向量就可以构成这个投影矩阵了。特征向量的选择取决于协方差矩阵的特征值的大小。

  举一个例子:

  对于一个训练集,100个对象模板,特征是10维,那么它可以建立一个100*10的矩阵,作为样本。求这个样本的协方差矩阵,得到一个10*10的协方差矩阵,然后求出这个协方差矩阵的特征值和特征向量,应该有10个特征值和特征向量,我们根据特征值的大小,取前四个特征值所对应的特征向量,构成一个10*4的矩阵,这个矩阵就是我们要求的特征矩阵,100*10的样本矩阵乘以这个10*4的特征矩阵,就得到了一个100*4的新的降维之后的样本矩阵,每个特征的维数下降了。

  当给定一个测试的特征集之后,比如1*10维的特征,乘以上面得到的10*4的特征矩阵,便可以得到一个1*4的特征,用这个特征去分类。

  所以做PCA实际上是求得这个投影矩阵,用高维的特征乘以这个投影矩阵,便可以将高维特征的维数下降到指定的维数。

  PCA 的目标是寻找 r ( r<n )个新变量,使它们反映事物的主要特征,压缩原有数据矩阵的规模。每个新变量是原有变量的线性组合,体现原有变量的综合效果,具有一定的实际含义。这 r 个新变量称为“主成分”,它们可以在很大程度上反映原来 n 个变量的影响,并且这些新变量是互不相关的,也是正交的。通过主成分分析,压缩数据空间,将多元数据的特征在低维空间里直观地表示出来。例如,将多个时间点、多个实验条件下的基因表达谱数据( N 维)表示为 3 维空间中的一个点,即将数据的维数从 RN 降到 R3 。

  在进行基因表达数据分析时,一个重要问题是确定每个实验数据是否是独立的,如果每次实验数据之间不是独立的,则会影响基因表达数据分析结果的准确性。对于利用基因芯片所检测到的基因表达数据,如果用 PCA 方法进行分析,可以将各个基因作为变量,也可以将实验条件作为变量。当将基因作为变量时,通过分析确定一组“主要基因元素”,它们能够很好地说明基因的特征,解释实验现象;当将实验条件作为变量时,通过分析确定一组“主要实验因素”,它们能够很好地刻画实验条件的特征,解释基因的行为。下面着重考虑以实验条件作为变量的 PCA 分析方法。假设将数据的维数从 R N 降到 R 3 ,具体的 PCA 分析步骤如下:

  (1) 第一步计算矩阵 X 的样本的协方差矩阵 S :

  (2) 第二步计算协方差矩阵S的本征向量 e1,e2,…,eN的本征值 , i = 1,2,…,N 。本征值按大到小排序: ;

  (3)第三步投影数据到本征矢张成的空间之中,这些本征矢相应的本征值为 。现在数据可以在三维空间中展示为云状的点集。

  对于 PCA ,确定新变量的个数 r 是一个两难的问题。我们的目标是减小 r ,如果 r 小,则数据的维数低,便于分析,同时也降低了噪声,但可能丢失一些有用的信息。究竟如何确定 r 呢?这需要进一步分析每个主元素对信息的贡献。

  令 代表第 i 个特征值,定义第 i 个主元素的贡献率为:

  (8-45)

  前 r 个主成分的累计贡献率为:

  (8-46)

  贡献率表示所定义的主成分在整个数据分析中承担的主要意义占多大的比重,当取前 r 个主成分来代替原来全部变量时,累计贡献率的大小反应了这种取代的可靠性,累计贡献率越大,可靠性越大;反之,则可靠性越小。一般要求累计贡献率达到 70% 以上。

  经过 PCA 分析,一个多变量的复杂问题被简化为低维空间的简单问题。可以利用这种简化方法进行作图,形象地表示和分析复杂问题。在分析基因表达数据时,可以针对基因作图,也可以针对实验条件作图。前者称为 Q 分析,后者称为 R 分析。

 

 

 

 

 

 

1) 协方差矩阵

2) 特征值与特征向量

3) 贡献率

 

 

http://www.cad.zju.edu.cn/home/chenlu/pca.htm

[java] view plaincopy
  1. /* 
  2.  *    PrincipalComponents.java 
  3.  *    Copyright (C) 2000 University of Waikato, Hamilton, New Zealand 
  4.  * 
  5.  */  
  6.   
  7. package weka.attributeSelection;  
  8.   
  9.   
  10. /** 
  11.  <!-- globalinfo-start --> 
  12.  * Performs a principal components analysis and transformation of the data. Use in conjunction with a Ranker search. Dimensionality reduction is accomplished by choosing enough eigenvectors to account for some percentage of the variance in the original data---default 0.95 (95%). Attribute noise can be filtered by transforming to the PC space, eliminating some of the worst eigenvectors, and then transforming back to the original space. 
  13.  * <p/> 
  14.  <!-- globalinfo-end --> 
  15.  * 
  16.  <!-- options-start --> 
  17.  * Valid options are: <p/> 
  18.  *  
  19.  * <pre> -D 
  20.  *  Don't normalize input data.</pre> 
  21.  *  
  22.  * <pre> -R 
  23.  *  Retain enough PC attributes to account  
  24.  *  for this proportion of variance in the original data. 
  25.  *  (default = 0.95)</pre> 
  26.  *  
  27.  * <pre> -O 
  28.  *  Transform through the PC space and  
  29.  *  back to the original space.</pre> 
  30.  *  
  31.  * <pre> -A 
  32.  *  Maximum number of attributes to include in  
  33.  *  transformed attribute names. (-1 = include all)</pre> 
  34.  *  
  35.  <!-- options-end --> 
  36.  * 
  37.  * @author Mark Hall (mhall@cs.waikato.ac.nz) 
  38.  * @author Gabi Schmidberger (gabi@cs.waikato.ac.nz) 
  39.  * @version $Revision: 5987 $ 
  40.  */  
  41. public class PrincipalComponents   
  42.   extends UnsupervisedAttributeEvaluator   
  43.   implements AttributeTransformer, OptionHandler {  
  44.     
  45.   /** for serialization */  
  46.   static final long serialVersionUID = 3310137541055815078L;  
  47.     
  48.   /** The data to transform analyse/transform */  
  49.   private Instances m_trainInstances;  
  50.   
  51.   /** Keep a copy for the class attribute (if set) */  
  52.   private Instances m_trainHeader;  
  53.   
  54.   /** The header for the transformed data format */  
  55.   private Instances m_transformedFormat;  
  56.   
  57.   /** The header for data transformed back to the original space */  
  58.   private Instances m_originalSpaceFormat;  
  59.   
  60.   /** Data has a class set */  
  61.   private boolean m_hasClass;  
  62.   
  63.   /** Class index */  
  64.   private int m_classIndex;  
  65.   
  66.   /** Number of attributes */  
  67.   private int m_numAttribs;  
  68.   
  69.   /** Number of instances */  
  70.   private int m_numInstances;  
  71.   
  72.   /** Correlation matrix for the original data */  
  73.   private double [][] m_correlation;  
  74.   
  75.   /** Will hold the unordered linear transformations of the (normalized) 
  76.       original data */  
  77.   private double [][] m_eigenvectors;  
  78.     
  79.   /** Eigenvalues for the corresponding eigenvectors */  
  80.   private double [] m_eigenvalues = null;  
  81.   
  82.   /** Sorted eigenvalues */  
  83.   private int [] m_sortedEigens;  
  84.   
  85.   /** sum of the eigenvalues */  
  86.   private double m_sumOfEigenValues = 0.0;  
  87.     
  88.   /** Filters for original data */  
  89.   private ReplaceMissingValues m_replaceMissingFilter;  
  90.   private Normalize m_normalizeFilter;  
  91.   private NominalToBinary m_nominalToBinFilter;  
  92.   private Remove m_attributeFilter;  
  93.     
  94.   /** used to remove the class column if a class column is set */  
  95.   private Remove m_attribFilter;  
  96.   
  97.   /** The number of attributes in the pc transformed data */  
  98.   private int m_outputNumAtts = -1;  
  99.     
  100.   /** normalize the input data? */  
  101.   private boolean m_normalize = true;  
  102.   
  103.   /** the amount of varaince to cover in the original data when 
  104.       retaining the best n PC's */  
  105.   private double m_coverVariance = 0.95;  
  106.   
  107.   /** transform the data through the pc space and back to the original 
  108.       space ? */  
  109.   private boolean m_transBackToOriginal = false;  
  110.     
  111.   /** maximum number of attributes in the transformed attribute name */  
  112.   private int m_maxAttrsInName = 5;  
  113.   
  114.   /** holds the transposed eigenvectors for converting back to the 
  115.       original space */  
  116.   private double [][] m_eTranspose;  
  117.   
  118.   /** 
  119.    * Returns a string describing this attribute transformer 
  120.    * @return a description of the evaluator suitable for 
  121.    * displaying in the explorer/experimenter gui 
  122.    */  
  123.   public String globalInfo() {  
  124.     return "Performs a principal components analysis and transformation of "  
  125.       +"the data. Use in conjunction with a Ranker search. Dimensionality "  
  126.       +"reduction is accomplished by choosing enough eigenvectors to "  
  127.       +"account for some percentage of the variance in the original data---"  
  128.       +"default 0.95 (95%). Attribute noise can be filtered by transforming "  
  129.       +"to the PC space, eliminating some of the worst eigenvectors, and "  
  130.       +"then transforming back to the original space.";  
  131.   }  
  132.   
  133.   /** 
  134.    * Returns an enumeration describing the available options. <p> 
  135.    * 
  136.    * @return an enumeration of all the available options. 
  137.    **/  
  138.   public Enumeration listOptions () {  
  139.     Vector newVector = new Vector(3);  
  140.     newVector.addElement(new Option("/tDon't normalize input data."   
  141.                                     , "D"0"-D"));  
  142.   
  143.     newVector.addElement(new Option("/tRetain enough PC attributes to account "  
  144.                                     +"/n/tfor this proportion of variance in "  
  145.                                     +"the original data./n"  
  146.                                     + "/t(default = 0.95)",  
  147.                                     "R",1,"-R"));  
  148.       
  149.     newVector.addElement(new Option("/tTransform through the PC space and "  
  150.                                     +"/n/tback to the original space."  
  151.                                     , "O"0"-O"));  
  152.                                       
  153.     newVector.addElement(new Option("/tMaximum number of attributes to include in "  
  154.                                     + "/n/ttransformed attribute names. (-1 = include all)"  
  155.                                     , "A"1"-A"));  
  156.     return  newVector.elements();  
  157.   }  
  158.   
  159.   /** 
  160.    * Parses a given list of options. <p/> 
  161.    * 
  162.    <!-- options-start --> 
  163.    * Valid options are: <p/> 
  164.    *  
  165.    * <pre> -D 
  166.    *  Don't normalize input data.</pre> 
  167.    *  
  168.    * <pre> -R 
  169.    *  Retain enough PC attributes to account  
  170.    *  for this proportion of variance in the original data. 
  171.    *  (default = 0.95)</pre> 
  172.    *  
  173.    * <pre> -O 
  174.    *  Transform through the PC space and  
  175.    *  back to the original space.</pre> 
  176.    *  
  177.    * <pre> -A 
  178.    *  Maximum number of attributes to include in  
  179.    *  transformed attribute names. (-1 = include all)</pre> 
  180.    *  
  181.    <!-- options-end --> 
  182.    * 
  183.    * @param options the list of options as an array of strings 
  184.    * @throws Exception if an option is not supported 
  185.    */  
  186.   public void setOptions (String[] options)  
  187.     throws Exception {  
  188.     resetOptions();  
  189.     String optionString;  
  190.   
  191.     optionString = Utils.getOption('R', options);  
  192.     if (optionString.length() != 0) {  
  193.       Double temp;  
  194.       temp = Double.valueOf(optionString);  
  195.       setVarianceCovered(temp.doubleValue());  
  196.     }  
  197.     optionString = Utils.getOption('A', options);  
  198.     if (optionString.length() != 0) {  
  199.       setMaximumAttributeNames(Integer.parseInt(optionString));  
  200.     }  
  201.     setNormalize(!Utils.getFlag('D', options));  
  202.   
  203.     setTransformBackToOriginal(Utils.getFlag('O', options));  
  204.   }  
  205.   
  206.   /** 
  207.    * Reset to defaults 
  208.    */  
  209.   private void resetOptions() {  
  210.     m_coverVariance = 0.95;  
  211.     m_normalize = true;  
  212.     m_sumOfEigenValues = 0.0;  
  213.     m_transBackToOriginal = false;  
  214.   }  
  215.   
  216.   /** 
  217.    * Returns the tip text for this property 
  218.    * @return tip text for this property suitable for 
  219.    * displaying in the explorer/experimenter gui 
  220.    */  
  221.   public String normalizeTipText() {  
  222.     return "Normalize input data.";  
  223.   }  
  224.   
  225.   /** 
  226.    * Set whether input data will be normalized. 
  227.    * @param n true if input data is to be normalized 
  228.    */  
  229.   public void setNormalize(boolean n) {  
  230.     m_normalize = n;  
  231.   }  
  232.   
  233.   /** 
  234.    * Gets whether or not input data is to be normalized 
  235.    * @return true if input data is to be normalized 
  236.    */  
  237.   public boolean getNormalize() {  
  238.     return m_normalize;  
  239.   }  
  240.   
  241.   /** 
  242.    * Returns the tip text for this property 
  243.    * @return tip text for this property suitable for 
  244.    * displaying in the explorer/experimenter gui 
  245.    */  
  246.   public String varianceCoveredTipText() {  
  247.     return "Retain enough PC attributes to account for this proportion of "  
  248.       +"variance.";  
  249.   }  
  250.   
  251.   /** 
  252.    * Sets the amount of variance to account for when retaining 
  253.    * principal components 
  254.    * @param vc the proportion of total variance to account for 
  255.    */  
  256.   public void setVarianceCovered(double vc) {  
  257.     m_coverVariance = vc;  
  258.   }  
  259.   
  260.   /** 
  261.    * Gets the proportion of total variance to account for when 
  262.    * retaining principal components 
  263.    * @return the proportion of variance to account for 
  264.    */  
  265.   public double getVarianceCovered() {  
  266.     return m_coverVariance;  
  267.   }  
  268.   
  269.   /** 
  270.    * Returns the tip text for this property 
  271.    * @return tip text for this property suitable for 
  272.    * displaying in the explorer/experimenter gui 
  273.    */  
  274.   public String maximumAttributeNamesTipText() {  
  275.     return "The maximum number of attributes to include in transformed attribute names.";  
  276.   }  
  277.   
  278.   /** 
  279.    * Sets maximum number of attributes to include in 
  280.    * transformed attribute names. 
  281.    * @param m the maximum number of attributes 
  282.    */  
  283.   public void setMaximumAttributeNames(int m) {  
  284.     m_maxAttrsInName = m;  
  285.   }  
  286.   
  287.   /** 
  288.    * Gets maximum number of attributes to include in 
  289.    * transformed attribute names. 
  290.    * @return the maximum number of attributes 
  291.    */  
  292.   public int getMaximumAttributeNames() {  
  293.     return m_maxAttrsInName;  
  294.   }  
  295.   
  296.   /** 
  297.    * Returns the tip text for this property 
  298.    * @return tip text for this property suitable for 
  299.    * displaying in the explorer/experimenter gui 
  300.    */  
  301.   public String transformBackToOriginalTipText() {  
  302.     return "Transform through the PC space and back to the original space. "  
  303.       +"If only the best n PCs are retained (by setting varianceCovered < 1) "  
  304.       +"then this option will give a dataset in the original space but with "  
  305.       +"less attribute noise.";  
  306.   }  
  307.   
  308.   /** 
  309.    * Sets whether the data should be transformed back to the original 
  310.    * space 
  311.    * @param b true if the data should be transformed back to the 
  312.    * original space 
  313.    */  
  314.   public void setTransformBackToOriginal(boolean b) {  
  315.     m_transBackToOriginal = b;  
  316.   }  
  317.     
  318.   /** 
  319.    * Gets whether the data is to be transformed back to the original 
  320.    * space. 
  321.    * @return true if the data is to be transformed back to the original space 
  322.    */  
  323.   public boolean getTransformBackToOriginal() {  
  324.     return m_transBackToOriginal;  
  325.   }  
  326.   
  327.   /** 
  328.    * Gets the current settings of PrincipalComponents 
  329.    * 
  330.    * @return an array of strings suitable for passing to setOptions() 
  331.    */  
  332.   public String[] getOptions () {  
  333.   
  334.     String[] options = new String[6];  
  335.     int current = 0;  
  336.   
  337.     if (!getNormalize()) {  
  338.       options[current++] = "-D";  
  339.     }  
  340.   
  341.     options[current++] = "-R";  
  342.     options[current++] = ""+getVarianceCovered();  
  343.   
  344.     options[current++] = "-A";  
  345.     options[current++] = ""+getMaximumAttributeNames();  
  346.   
  347.     if (getTransformBackToOriginal()) {  
  348.       options[current++] = "-O";  
  349.     }  
  350.       
  351.     while (current < options.length) {  
  352.       options[current++] = "";  
  353.     }  
  354.       
  355.     return  options;  
  356.   }  
  357.   
  358.   /** 
  359.    * Returns the capabilities of this evaluator. 
  360.    * 
  361.    * @return            the capabilities of this evaluator 
  362.    * @see               Capabilities 
  363.    */  
  364.   public Capabilities getCapabilities() {  
  365.     Capabilities result = super.getCapabilities();  
  366.     result.disableAll();  
  367.       
  368.     // attributes  
  369.     result.enable(Capability.NOMINAL_ATTRIBUTES);  
  370.     result.enable(Capability.NUMERIC_ATTRIBUTES);  
  371.     result.enable(Capability.DATE_ATTRIBUTES);  
  372.     result.enable(Capability.MISSING_VALUES);  
  373.       
  374.     // class  
  375.     result.enable(Capability.NOMINAL_CLASS);  
  376.     result.enable(Capability.NUMERIC_CLASS);  
  377.     result.enable(Capability.DATE_CLASS);  
  378.     result.enable(Capability.MISSING_CLASS_VALUES);  
  379.     result.enable(Capability.NO_CLASS);  
  380.       
  381.     return result;  
  382.   }  
  383.   
  384.   /** 
  385.    * Initializes principal components and performs the analysis 
  386.    * @param data the instances to analyse/transform 
  387.    * @throws Exception if analysis fails 
  388.    */  
  389.   public void buildEvaluator(Instances data) throws Exception {  
  390.     // can evaluator handle data?  
  391.     getCapabilities().testWithFail(data);  
  392.   
  393.     buildAttributeConstructor(data);  
  394.   }  
  395.   
  396.   private void buildAttributeConstructor (Instances data) throws Exception {  
  397.     m_eigenvalues = null;  
  398.     m_outputNumAtts = -1;  
  399.     m_attributeFilter = null;  
  400.     m_nominalToBinFilter = null;  
  401.     m_sumOfEigenValues = 0.0;  
  402.     m_trainInstances = new Instances(data);  
  403.   
  404.     // make a copy of the training data so that we can get the class  
  405.     // column to append to the transformed data (if necessary)  
  406.     m_trainHeader = new Instances(m_trainInstances, 0);  
  407.       
  408.     //处理缺失值、归一化、类别二值化、删除单值属性或者都为缺失值的属性  
  409.      //mormalize nominaltobin  
  410.     //TODO.... deleted  
  411.   
  412.     m_numInstances = m_trainInstances.numInstances();  
  413.     m_numAttribs = m_trainInstances.numAttributes();  
  414.   
  415.     //计算相关矩阵,得到m_numAttribs * m_numAttribs的方阵  
  416.     fillCorrelation();  
  417.   
  418.     double [] d = new double[m_numAttribs];   
  419.     double [][] v = new double[m_numAttribs][m_numAttribs];  
  420.   
  421.     Matrix corr = new Matrix(m_correlation);  
  422.     //得到特征向量和特征值  
  423.     corr.eigenvalueDecomposition(v, d);  
  424.     m_eigenvectors = (double [][])v.clone();  
  425.     m_eigenvalues = (double [])d.clone();  
  426.   
  427.     // any eigenvalues less than 0 are not worth anything --- change to 0  
  428.     for (int i = 0; i < m_eigenvalues.length; i++) {  
  429.       if (m_eigenvalues[i] < 0) {  
  430.         m_eigenvalues[i] = 0.0;  
  431.       }  
  432.     }  
  433.     m_sortedEigens = Utils.sort(m_eigenvalues);  
  434.     m_sumOfEigenValues = Utils.sum(m_eigenvalues);  
  435.   
  436.   }  
  437.   
  438.   /** 
  439.    * Returns just the header for the transformed data (ie. an empty 
  440.    * set of instances. This is so that AttributeSelection can 
  441.    * determine the structure of the transformed data without actually 
  442.    * having to get all the transformed data through transformedData(). 
  443.    * @return the header of the transformed data. 
  444.    * @throws Exception if the header of the transformed data can't 
  445.    * be determined. 
  446.    */  
  447.   public Instances transformedHeader() throws Exception {  
  448.   
  449.   }  
  450.   
  451.   /** 
  452.    * Gets the transformed training data. 
  453.    * @return the transformed training data 
  454.    * @throws Exception if transformed data can't be returned 
  455.    */  
  456.   public Instances transformedData(Instances data) throws Exception {  
  457.   }  
  458.   
  459.   /** 
  460.    * Evaluates the merit of a transformed attribute. This is defined 
  461.    * to be 1 minus the cumulative variance explained. Merit can't 
  462.    * be meaningfully evaluated if the data is to be transformed back 
  463.    * to the original space. 
  464.    * @param att the attribute to be evaluated 
  465.    * @return the merit of a transformed attribute 
  466.    * @throws Exception if attribute can't be evaluated 
  467.    */  
  468.   public double evaluateAttribute(int att) throws Exception {  
  469.     if (m_eigenvalues == null) {  
  470.       throw new Exception("Principal components hasn't been built yet!");  
  471.     }  
  472.   
  473.     if (m_transBackToOriginal) {  
  474.       return 1.0// can't evaluate back in the original space!  
  475.     }  
  476.   
  477.     // return 1-cumulative variance explained for this transformed att  
  478.     double cumulative = 0.0;  
  479.     for (int i = m_numAttribs - 1; i >= m_numAttribs - att - 1; i--) {  
  480.       cumulative += m_eigenvalues[m_sortedEigens[i]];  
  481.     }  
  482.   
  483.     return 1.0 - cumulative / m_sumOfEigenValues;  
  484.   }  
  485.   
  486.   /** 
  487.    * Fill the correlation matrix 
  488.    */  
  489.   private void fillCorrelation() {  
  490.     m_correlation = new double[m_numAttribs][m_numAttribs];  
  491.     double [] att1 = new double [m_numInstances];  
  492.     double [] att2 = new double [m_numInstances];  
  493.     double corr;  
  494.   
  495.     for (int i = 0; i < m_numAttribs; i++) {  
  496.       for (int j = 0; j < m_numAttribs; j++) {  
  497.         if (i == j) {  
  498.           m_correlation[i][j] = 1.0;  
  499.         } else {  
  500.           for (int k = 0; k < m_numInstances; k++) {  
  501.             att1[k] = m_trainInstances.instance(k).value(i);  
  502.             att2[k] = m_trainInstances.instance(k).value(j);  
  503.           }  
  504.           corr = Utils.correlation(att1,att2,m_numInstances);  
  505.           m_correlation[i][j] = corr;  
  506.           m_correlation[j][i] = corr;  
  507.         }  
  508.       }  
  509.     }  
  510.   }  
  511.   
  512.   
  513.   /** 
  514.    * Main method for testing this class 
  515.    * @param argv should contain the command line arguments to the 
  516.    * evaluator/transformer (see AttributeSelection) 
  517.    */  
  518.   public static void main(String [] argv) {  
  519.     runEvaluator(new PrincipalComponents(), argv);  
  520.   }  
  521. }  


0 0
原创粉丝点击