NumericPrediction.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.  *    NumericPrediction.java
  18.  *    Copyright (C) 2000 Intelligenesis Corp.
  19.  *
  20.  */
  21. package weka.classifiers.evaluation;
  22. import java.io.Serializable;
  23. /**
  24.  * Encapsulates an evaluatable numeric prediction: the predicted class value
  25.  * plus the actual class value.
  26.  *
  27.  * @author Len Trigg (len@intelligenesis.net)
  28.  * @version $Revision: 1.5 $
  29.  */
  30. public class NumericPrediction implements Prediction, Serializable {
  31.   /** The actual class value */
  32.   private double m_Actual = MISSING_VALUE;
  33.   /** The predicted class value */
  34.   private double m_Predicted = MISSING_VALUE;
  35.   /** The weight assigned to this prediction */
  36.   private double m_Weight = 1;
  37.   /**
  38.    * Creates the NumericPrediction object with a default weight of 1.0.
  39.    *
  40.    * @param actual the actual value, or MISSING_VALUE.
  41.    * @param predicted the predicted value, or MISSING_VALUE.
  42.    */
  43.   public NumericPrediction(double actual, double predicted) {
  44.     this(actual, predicted, 1);
  45.   }
  46.   /**
  47.    * Creates the NumericPrediction object.
  48.    *
  49.    * @param actual the actual value, or MISSING_VALUE.
  50.    * @param predicted the predicted value, or MISSING_VALUE.
  51.    * @param weight the weight assigned to the prediction.
  52.    */
  53.   public NumericPrediction(double actual, double predicted, double weight) {
  54.     m_Actual = actual;
  55.     m_Predicted = predicted;
  56.     m_Weight = weight;
  57.   }
  58.   /** 
  59.    * Gets the actual class value.
  60.    *
  61.    * @return the actual class value, or MISSING_VALUE if no
  62.    * prediction was made.  
  63.    */
  64.   public double actual() { 
  65.     return m_Actual; 
  66.   }
  67.   /**
  68.    * Gets the predicted class value.
  69.    *
  70.    * @return the predicted class value, or MISSING_VALUE if no
  71.    * prediction was made.  
  72.    */
  73.   public double predicted() { 
  74.     return m_Predicted; 
  75.   }
  76.   /** 
  77.    * Gets the weight assigned to this prediction. This is typically the weight
  78.    * of the test instance the prediction was made for.
  79.    *
  80.    * @return the weight assigned to this prediction.
  81.    */
  82.   public double weight() { 
  83.     return m_Weight; 
  84.   }
  85.   /**
  86.    * Calculates the prediction error. This is defined as the predicted
  87.    * value minus the actual value.
  88.    *
  89.    * @return the error for this prediction, or
  90.    * MISSING_VALUE if either the actual or predicted value
  91.    * is missing.  
  92.    */
  93.   public double error() {
  94.     if ((m_Actual == MISSING_VALUE) ||
  95.         (m_Predicted == MISSING_VALUE)) {
  96.       return MISSING_VALUE;
  97.     }
  98.     return m_Predicted - m_Actual;
  99.   }
  100.   /**
  101.    * Gets a human readable representation of this prediction.
  102.    *
  103.    * @return a human readable representation of this prediction.
  104.    */
  105.   public String toString() {
  106.     StringBuffer sb = new StringBuffer();
  107.     sb.append("NUM: ").append(actual()).append(' ').append(predicted());
  108.     sb.append(' ').append(weight());
  109.     return sb.toString();
  110.   }
  111. }