NominalPrediction.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 6k
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.  *    NominalPrediction.java
  18.  *    Copyright (C) 2000 Intelligenesis Corp.
  19.  *
  20.  */
  21. package weka.classifiers.evaluation;
  22. import java.io.Serializable;
  23. /**
  24.  * Encapsulates an evaluatable nominal prediction: the predicted probability
  25.  * distribution plus the actual class value.
  26.  *
  27.  * @author Len Trigg (len@intelligenesis.net)
  28.  * @version $Revision: 1.8 $
  29.  */
  30. public class NominalPrediction implements Prediction, Serializable {
  31.   /**
  32.    * Remove this if you change this class so that serialization would be
  33.    * affected.
  34.    */
  35.   static final long serialVersionUID = -8871333992740492788L;
  36.   /** The predicted probabilities */
  37.   private double [] m_Distribution;
  38.   /** The actual class value */
  39.   private double m_Actual = MISSING_VALUE;
  40.   /** The predicted class value */
  41.   private double m_Predicted = MISSING_VALUE;
  42.   /** The weight assigned to this prediction */
  43.   private double m_Weight = 1;
  44.   /**
  45.    * Creates the NominalPrediction object with a default weight of 1.0.
  46.    *
  47.    * @param actual the actual value, or MISSING_VALUE.
  48.    * @param distribution the predicted probability distribution. Use 
  49.    * NominalPrediction.makeDistribution() if you only know the predicted value.
  50.    */
  51.   public NominalPrediction(double actual, double [] distribution) {
  52.     this(actual, distribution, 1);
  53.   }
  54.   /**
  55.    * Creates the NominalPrediction object.
  56.    *
  57.    * @param actual the actual value, or MISSING_VALUE.
  58.    * @param distribution the predicted probability distribution. Use 
  59.    * NominalPrediction.makeDistribution() if you only know the predicted value.
  60.    * @param weight the weight assigned to the prediction.
  61.    */
  62.   public NominalPrediction(double actual, double [] distribution, 
  63.                            double weight) {
  64.     if (distribution == null) {
  65.       throw new NullPointerException("Null distribution in NominalPrediction.");
  66.     }
  67.     m_Actual = actual;
  68.     m_Distribution = distribution;
  69.     m_Weight = weight;
  70.     updatePredicted();
  71.   }
  72.   /** Gets the predicted probabilities */
  73.   public double [] distribution() { 
  74.     return m_Distribution; 
  75.   }
  76.   /** 
  77.    * Gets the actual class value.
  78.    *
  79.    * @return the actual class value, or MISSING_VALUE if no
  80.    * prediction was made.  
  81.    */
  82.   public double actual() { 
  83.     return m_Actual; 
  84.   }
  85.   /**
  86.    * Gets the predicted class value.
  87.    *
  88.    * @return the predicted class value, or MISSING_VALUE if no
  89.    * prediction was made.  
  90.    */
  91.   public double predicted() { 
  92.     return m_Predicted; 
  93.   }
  94.   /** 
  95.    * Gets the weight assigned to this prediction. This is typically the weight
  96.    * of the test instance the prediction was made for.
  97.    *
  98.    * @return the weight assigned to this prediction.
  99.    */
  100.   public double weight() { 
  101.     return m_Weight; 
  102.   }
  103.   /**
  104.    * Calculates the prediction margin. This is defined as the difference
  105.    * between the probability predicted for the actual class and the highest
  106.    * predicted probability of the other classes.
  107.    *
  108.    * @return the margin for this prediction, or
  109.    * MISSING_VALUE if either the actual or predicted value
  110.    * is missing.  
  111.    */
  112.   public double margin() {
  113.     if ((m_Actual == MISSING_VALUE) ||
  114.         (m_Predicted == MISSING_VALUE)) {
  115.       return MISSING_VALUE;
  116.     }
  117.     double probActual = m_Distribution[(int)m_Actual];
  118.     double probNext = 0;
  119.     for(int i = 0; i < m_Distribution.length; i++)
  120.       if ((i != m_Actual) &&
  121.   (m_Distribution[i] > probNext))
  122. probNext = m_Distribution[i];
  123.     return probActual - probNext;
  124.   }
  125.   /**
  126.    * Convert a single prediction into a probability distribution
  127.    * with all zero probabilities except the predicted value which
  128.    * has probability 1.0. If no prediction was made, all probabilities
  129.    * are zero.
  130.    *
  131.    * @param predictedClass the index of the predicted class, or 
  132.    * MISSING_VALUE if no prediction was made.
  133.    * @param numClasses the number of possible classes for this nominal
  134.    * prediction.
  135.    * @return the probability distribution.  
  136.    */
  137.   public static double [] makeDistribution(double predictedClass, 
  138.                                            int numClasses) {
  139.     double [] dist = new double [numClasses];
  140.     if (predictedClass == MISSING_VALUE) {
  141.       return dist;
  142.     }
  143.     dist[(int)predictedClass] = 1.0;
  144.     return dist;
  145.   }
  146.   
  147.   /**
  148.    * Creates a uniform probability distribution -- where each of the
  149.    * possible classes is assigned equal probability.
  150.    *
  151.    * @param numClasses the number of possible classes for this nominal
  152.    * prediction.
  153.    * @return the probability distribution.  
  154.    */
  155.   public static double [] makeUniformDistribution(int numClasses) {
  156.     
  157.     double [] dist = new double [numClasses];
  158.     for (int i = 0; i < numClasses; i++) {
  159.       dist[i] = 1.0 / numClasses;
  160.     }
  161.     return dist;
  162.   }
  163.  
  164.   /**
  165.    * Determines the predicted class (doesn't detect multiple 
  166.    * classifications). If no prediction was made (i.e. all zero
  167.    * probababilities in the distribution), m_Prediction is set to
  168.    * MISSING_VALUE.
  169.    */
  170.   private void updatePredicted() {
  171.     int predictedClass = -1;
  172.     double bestProb = 0.0;
  173.     for(int i = 0; i < m_Distribution.length; i++) {
  174.       if (m_Distribution[i] > bestProb) {
  175.         predictedClass = i;
  176.         bestProb = m_Distribution[i];
  177.       }
  178.     }
  179.     if (predictedClass != -1) {
  180.       m_Predicted = predictedClass;
  181.     } else {
  182.       m_Predicted = MISSING_VALUE;
  183.     }
  184.   }
  185.   /**
  186.    * Gets a human readable representation of this prediction.
  187.    *
  188.    * @return a human readable representation of this prediction.
  189.    */
  190.   public String toString() {
  191.     StringBuffer sb = new StringBuffer();
  192.     sb.append("NOM: ").append(actual()).append(" ").append(predicted());
  193.     sb.append(' ').append(weight());
  194.     double [] dist = distribution();
  195.     for (int i = 0; i < dist.length; i++) {
  196.       sb.append(' ').append(dist[i]);
  197.     }
  198.     return sb.toString();
  199.   }
  200. }