Measures.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 4k
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.  *    Measures.java
  18.  *    Copyright (C) 1999 Yong Wang
  19.  *
  20.  */
  21. package weka.classifiers.m5;
  22. import java.io.*;
  23. import java.util.*;
  24. import weka.core.*;
  25. /**
  26.  * Class for performance measures
  27.  * @author Yong Wang (yongwang@cs.waikato.ac.nz)
  28.  * @version $Revision: 1.4 $
  29.  */
  30. public final class Measures {
  31.   int type;                  /* type for printing */
  32.   double correlation;        /* correlation coefficient */
  33.   double meanAbsErr;         /* mean absolute error */
  34.   double meanSqrErr;         /* mean sqaured error */
  35.   
  36.   /**
  37.    * Constructs a Measures object which could containing the performance measures
  38.    */
  39.   public  Measures(){
  40.     
  41.     type = 0;
  42.     correlation =0.0;
  43.     meanAbsErr =0.0;
  44.     meanSqrErr =0.0;
  45.   }
  46.   /**
  47.    * Converts the performance measures to a string
  48.    * @param absDev the absolute deviation of the class attribute
  49.    * @param sd the standard deviation of the class attribute
  50.    * @set the type of the performance measures, is one of "t","T","f","F","x", for training, test, training of one fold in cross-validation, test of one test in cross-validation, cross-validation final resuls respectively
  51.    * @param smooth either "u" or "s" for unsmoothed or smoothed
  52.    * @return the converted string
  53.    */
  54.   public final String  toString(double absDev,double sd,String set,String smooth){
  55.     
  56.     StringBuffer text = new StringBuffer();
  57.     switch(type){
  58.     case 0:
  59.       text.append("    Correlation coefficient:tt" + M5Utils.doubleToStringF(correlation,9,3) + "ttt      " + set + smooth);
  60.       text.append("    Mean absolute error:tt" + M5Utils.doubleToStringG(meanAbsErr,9,4) + "ttt      " + set + smooth);
  61.       text.append("    Root mean squared error:tt" + M5Utils.doubleToStringG(Math.sqrt(Math.abs(meanSqrErr)),9,4) + "ttt      " + set + smooth);
  62.       text.append("    Relative absolute error:tt" + M5Utils.doubleToStringF(meanAbsErr/absDev*100.0,9,2) + " %ttt      " + set + smooth);
  63.       text.append("    Root relative squared error:t" + M5Utils.doubleToStringF(Math.sqrt(Math.abs(meanSqrErr))/sd*100.0,9,2) + " %ttt      " + set + smooth);
  64.       break;
  65.     case 1:
  66.       text.append("    Correlation coefficient:tt" + M5Utils.doubleToStringF(correlation,9,3) + "ttt      " + set + smooth);
  67.       text.append("    Mean absolute error:tt" + M5Utils.doubleToStringG(meanAbsErr,9,4) + "ttt      " + set + smooth);
  68.       text.append("    Root mean squared error:tt" + M5Utils.doubleToStringG(Math.sqrt(Math.abs(meanSqrErr)),9,4) + "ttt      " + set + smooth);
  69.       text.append("    Relative absolute error:ttundefinedttt      " + set + smooth);
  70.       text.append("    Root relative squared error:tundefinedttt      " + set + smooth);
  71.       break;
  72.     case 2:
  73.       text.append("    Correlation coefficient:tt" + M5Utils.doubleToStringF(correlation,9,3) + "ttt      " + set + smooth);
  74.       text.append("    Mean absolute error:ttundefinedttt      " + set + smooth);
  75.       text.append("    Root mean squared error:ttundefinedttt      " + set + smooth);
  76.       text.append("    Relative absolute error:ttundefinedttt      " + set + smooth);
  77.       text.append("    Root relative squared error:tundefinedttt      " + set + smooth);
  78.       break;
  79.     default:
  80.       M5Utils.errorMsg("wrong type in Measures.print().");
  81.     }
  82.     return text.toString();
  83.   }
  84.   /**
  85.    * Adds up performance measures for cross-validation
  86.    * @param m performance measures of a fold
  87.    */
  88.   public final void  incremental(Measures m){
  89.     correlation += m.correlation;
  90.     meanAbsErr  += m.meanAbsErr;
  91.     meanSqrErr  += m.meanSqrErr;
  92.   }
  93. }