TwoClassStats.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.  *    TwoClassStats.java
  18.  *    Copyright (C) 2000 Intelligenesis Corp.
  19.  *
  20.  */
  21. package weka.classifiers.evaluation;
  22. /**
  23.  * Encapsulates performance functions for two-class problems.
  24.  *
  25.  * @author Len Trigg (len@intelligenesis.net)
  26.  * @version $Revision: 1.5 $
  27.  */
  28. public class TwoClassStats {
  29.   /** The names used when converting this object to a confusion matrix */
  30.   private final static String [] CATEGORY_NAMES = {"negative", "positive"};
  31.   /** Pos predicted as pos */
  32.   private double m_TruePos;
  33.   /** Neg predicted as pos */
  34.   private double m_FalsePos;
  35.   /** Neg predicted as neg */
  36.   private double m_TrueNeg;
  37.   /** Pos predicted as neg */
  38.   private double m_FalseNeg;
  39.   /**
  40.    * Creates the TwoClassStats with the given initial performance values.
  41.    *
  42.    * @param tp the number of correctly classified positives
  43.    * @param fp the number of incorrectly classified negatives
  44.    * @param tn the number of correctly classified negatives
  45.    * @param fn the number of incorrectly classified positives
  46.    */
  47.   public TwoClassStats(double tp, double fp, double tn, double fn) {
  48.       
  49.     setTruePositive(tp); 
  50.     setFalsePositive(fp);
  51.     setTrueNegative(tn); 
  52.     setFalseNegative(fn);
  53.   }
  54.   /** Sets the number of positive instances predicted as positive */
  55.   public void setTruePositive(double tp) { m_TruePos = tp; }
  56.   /** Sets the number of negative instances predicted as positive */
  57.   public void setFalsePositive(double fp) { m_FalsePos = fp; }
  58.   /** Sets the number of negative instances predicted as negative */
  59.   public void setTrueNegative(double tn) { m_TrueNeg = tn; }
  60.   /** Sets the number of positive instances predicted as negative */
  61.   public void setFalseNegative(double fn) { m_FalseNeg = fn; }
  62.   /** Gets the number of positive instances predicted as positive */
  63.   public double getTruePositive() { return m_TruePos; }
  64.   /** Gets the number of negative instances predicted as positive */
  65.   public double getFalsePositive() { return m_FalsePos; }
  66.   /** Gets the number of negative instances predicted as negative */
  67.   public double getTrueNegative() { return m_TrueNeg; }
  68.   /** Gets the number of positive instances predicted as negative */
  69.   public double getFalseNegative() { return m_FalseNeg; }
  70.   /**
  71.    * Calculate the true positive rate. 
  72.    * This is defined as<p>
  73.    * <pre>
  74.    * correctly classified positives
  75.    * ------------------------------
  76.    *       total positives
  77.    * </pre>
  78.    *
  79.    * @return the true positive rate
  80.    */
  81.   public double getTruePositiveRate() { 
  82.     if (0 == (m_TruePos + m_FalseNeg)) {
  83.       return 0;
  84.     } else {
  85.       return m_TruePos / (m_TruePos + m_FalseNeg); 
  86.     }
  87.   }
  88.   /**
  89.    * Calculate the false positive rate. 
  90.    * This is defined as<p>
  91.    * <pre>
  92.    * incorrectly classified negatives
  93.    * --------------------------------
  94.    *        total negatives
  95.    * </pre>
  96.    *
  97.    * @return the false positive rate
  98.    */
  99.   public double getFalsePositiveRate() { 
  100.     if (0 == (m_FalsePos + m_TrueNeg)) {
  101.       return 0;
  102.     } else {
  103.       return m_FalsePos / (m_FalsePos + m_TrueNeg); 
  104.     }
  105.   }
  106.   /**
  107.    * Calculate the precision. 
  108.    * This is defined as<p>
  109.    * <pre>
  110.    * correctly classified positives
  111.    * ------------------------------
  112.    *  total predicted as positive
  113.    * </pre>
  114.    *
  115.    * @return the precision
  116.    */
  117.   public double getPrecision() { 
  118.     if (0 == (m_TruePos + m_FalsePos)) {
  119.       return 0;
  120.     } else {
  121.       return m_TruePos / (m_TruePos + m_FalsePos); 
  122.     }
  123.   }
  124.   /**
  125.    * Calculate the recall. 
  126.    * This is defined as<p>
  127.    * <pre>
  128.    * correctly classified positives
  129.    * ------------------------------
  130.    *       total positives
  131.    * </pre><p>
  132.    * (Which is also the same as the truePositiveRate.)
  133.    *
  134.    * @return the recall
  135.    */
  136.   public double getRecall() { return getTruePositiveRate(); }
  137.   /**
  138.    * Calculate the F-Measure. 
  139.    * This is defined as<p>
  140.    * <pre>
  141.    * 2 * recall * precision
  142.    * ----------------------
  143.    *   recall + precision
  144.    * </pre>
  145.    *
  146.    * @return the F-Measure
  147.    */
  148.   public double getFMeasure() {
  149.     double precision = getPrecision();
  150.     double recall = getRecall();
  151.     if ((precision + recall) == 0) {
  152.       return 0;
  153.     }
  154.     return 2 * precision * recall / (precision + recall);
  155.   }
  156.   /**
  157.    * Calculate the fallout. 
  158.    * This is defined as<p>
  159.    * <pre>
  160.    * incorrectly classified negatives
  161.    * --------------------------------
  162.    *   total predicted as positive
  163.    * </pre>
  164.    *
  165.    * @return the fallout
  166.    */
  167.   public double getFallout() { 
  168.     if (0 == (m_TruePos + m_FalsePos)) {
  169.       return 0;
  170.     } else {
  171.       return m_FalsePos / (m_TruePos + m_FalsePos); 
  172.     }
  173.   }
  174.   /**
  175.    * Generates a <code>ConfusionMatrix</code> representing the current
  176.    * two-class statistics, using class names "negative" and "positive".
  177.    *
  178.    * @return a <code>ConfusionMatrix</code>.
  179.    */
  180.   public ConfusionMatrix getConfusionMatrix() {
  181.     ConfusionMatrix cm = new ConfusionMatrix(CATEGORY_NAMES);
  182.     cm.setElement(0, 0, m_TrueNeg);
  183.     cm.setElement(0, 1, m_FalsePos);
  184.     cm.setElement(1, 0, m_FalseNeg);
  185.     cm.setElement(1, 1, m_TruePos);
  186.     return cm;
  187.   }
  188.   /**
  189.    * Returns a string containing the various performance measures
  190.    * for the current object 
  191.    */
  192.   public String toString() {
  193.     StringBuffer res = new StringBuffer();
  194.     res.append(getTruePositive()).append(' ');
  195.     res.append(getFalseNegative()).append(' ');
  196.     res.append(getTrueNegative()).append(' ');
  197.     res.append(getFalsePositive()).append(' ');
  198.     res.append(getFalsePositiveRate()).append(' ');
  199.     res.append(getTruePositiveRate()).append(' ');
  200.     res.append(getPrecision()).append(' ');
  201.     res.append(getRecall()).append(' ');
  202.     res.append(getFMeasure()).append(' ');
  203.     res.append(getFallout()).append(' ');
  204.     return res.toString();
  205.   }
  206. }