Values.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 3k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    Values.java
  18.  *    Copyright (C) 1999 
  19.  *
  20.  */
  21. package weka.classifiers.trees.m5;
  22. import java.io.*;
  23. import java.util.*;
  24. import weka.core.*;
  25. /**
  26.  * Stores some statistics.
  27.  * @author Yong Wang (yongwang@cs.waikato.ac.nz)
  28.  * @version $Revision: 1.5 $
  29.  */
  30. public final class Values {
  31.   int  numInstances;        // number of the instances
  32.   int  missingInstances;    // number of the instances with missing values 
  33.   int  first;               // index of the first instance
  34.   int  last;                // index of the last instance
  35.   int  attr;                // attribute
  36.   double  sum;              // sum of the instances for attribute
  37.   double  sqrSum;           // squared sum of the instances for attribute
  38.   double  va;               // variance
  39.   double  sd;               // standard deviation
  40.   
  41.   /**
  42.    * Constructs an object which stores some statistics of the instances such 
  43.    *      as sum, squared sum, variance, standard deviation
  44.    * @param low the index of the first instance
  45.    * @param high the index of the last instance
  46.    * @param attribute the attribute
  47.    * @param inst the instances
  48.    */
  49.   public Values(int low,int high,int attribute,Instances inst){
  50.     int i,count=0;
  51.     double value;
  52.     numInstances = high-low+1;
  53.     missingInstances = 0;
  54.     first = low;
  55.     last = high;
  56.     attr = attribute;
  57.     sum=0.0;
  58.     sqrSum=0.0;
  59.     for(i=first;i<=last;i++){
  60.       if(inst.instance(i).isMissing(attr)==false){
  61. count++;
  62. value = inst.instance(i).value(attr);
  63. sum += value;
  64. sqrSum += value * value;
  65.       }
  66.       
  67.       if(count >1){
  68. va = (sqrSum - sum * sum/count)/count;
  69. va = Math.abs(va);
  70. sd = Math.sqrt(va);
  71.       }
  72.       else {va = 0.0;  sd = 0.0;}      
  73.     }
  74.   }
  75.   /**
  76.    * Converts the stats to a string
  77.    * @return the converted string
  78.    */
  79.   public final String  toString(){
  80.     
  81.     StringBuffer text = new StringBuffer();
  82.     text.append("Print statistic values of instances (" + first + "-" + last + 
  83. "n");
  84.     text.append("    Number of instances:t" + numInstances + "n");
  85.     text.append("    NUmber of instances with unknowns:t" + missingInstances +
  86. "n");
  87.     text.append("    Attribute:ttt:" + attr + "n");
  88.     text.append("    Sum:ttt" + sum + "n");
  89.     text.append("    Squared sum:tt" + sqrSum + "n");
  90.     text.append("    Stanard Deviation:tt" + sd + "n");
  91.     return text.toString();
  92.   }
  93.   
  94.   
  95. }