ZeroR.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.  *    ZeroR.java
  18.  *    Copyright (C) 1999 Eibe Frank
  19.  *
  20.  */
  21. package weka.classifiers;
  22. import java.io.*;
  23. import java.util.*;
  24. import weka.core.*;
  25. /**
  26.  * Class for building and using a 0-R classifier. Predicts the mean
  27.  * (for a numeric class) or the mode (for a nominal class).
  28.  *
  29.  * @author Eibe Frank (eibe@cs.waikato.ac.nz)
  30.  * @version $Revision: 1.6 $
  31.  */
  32. public class ZeroR extends DistributionClassifier 
  33.   implements WeightedInstancesHandler {
  34.   /** The class value 0R predicts. */
  35.   private double m_ClassValue;
  36.   /** The number of instances in each class (null if class numeric). */
  37.   private double [] m_Counts;
  38.   
  39.   /** The class attribute. */
  40.   private Attribute m_Class;
  41.   /**
  42.    * Generates the classifier.
  43.    *
  44.    * @param instances set of instances serving as training data 
  45.    * @exception Exception if the classifier has not been generated successfully
  46.    */
  47.   public void buildClassifier(Instances instances) throws Exception {
  48.     m_Class = instances.classAttribute();
  49.     m_ClassValue = 0;
  50.     switch (instances.classAttribute().type()) {
  51.     case Attribute.NUMERIC:
  52.       m_Counts = null;
  53.       break;
  54.     case Attribute.NOMINAL:
  55.       m_Counts = new double [instances.numClasses()];
  56.       for (int i = 0; i < m_Counts.length; i++) {
  57. m_Counts[i] = 1;
  58.       }
  59.       break;
  60.     default:
  61.       throw new Exception("ZeroR can only handle nominal and numeric class"
  62.   + " attributes.");
  63.     }
  64.     Enumeration enum = instances.enumerateInstances();
  65.     while (enum.hasMoreElements()) {
  66.       Instance instance = (Instance) enum.nextElement();
  67.       if (!instance.classIsMissing()) {
  68. if (instances.classAttribute().isNominal()) {
  69.   m_Counts[(int)instance.classValue()] += instance.weight();
  70. } else {
  71.   m_ClassValue += instance.weight() * instance.classValue();
  72. }
  73.       }
  74.     }
  75.     if (instances.classAttribute().isNumeric()) {
  76.       if (Utils.gr(instances.sumOfWeights(), 0)) {
  77. m_ClassValue /= instances.sumOfWeights();
  78.       }
  79.     } else {
  80.       m_ClassValue = Utils.maxIndex(m_Counts);
  81.       Utils.normalize(m_Counts);
  82.     }
  83.   }
  84.   /**
  85.    * Classifies a given instance.
  86.    *
  87.    * @param instance the instance to be classified
  88.    * @return index of the predicted class
  89.    */
  90.   public double classifyInstance(Instance instance) {
  91.     return m_ClassValue;
  92.   }
  93.   /**
  94.    * Calculates the class membership probabilities for the given test instance.
  95.    *
  96.    * @param instance the instance to be classified
  97.    * @return predicted class probability distribution
  98.    * @exception Exception if class is numeric
  99.    */
  100.   public double [] distributionForInstance(Instance instance) 
  101.        throws Exception {
  102.  
  103.     if (m_Counts == null) {
  104.       double[] result = new double[1];
  105.       result[0] = m_ClassValue;
  106.       return result;
  107.     } else {
  108.       return (double []) m_Counts.clone();
  109.     }
  110.   }
  111.   
  112.   /**
  113.    * Returns a description of the classifier.
  114.    *
  115.    * @return a description of the classifier as a string.
  116.    */
  117.   public String toString() {
  118.     if (m_Class ==  null) {
  119.       return "ZeroR: No model built yet.";
  120.     }
  121.     if (m_Counts == null) {
  122.       return "ZeroR predicts class value: " + m_ClassValue;
  123.     } else {
  124.       return "ZeroR predicts class value: " + m_Class.value((int) m_ClassValue);
  125.     }
  126.   }
  127.   /**
  128.    * Main method for testing this class.
  129.    *
  130.    * @param argv the options
  131.    */
  132.   public static void main(String [] argv) {
  133.     try {
  134.       System.out.println(Evaluation.evaluateModel(new ZeroR(), argv));
  135.     } catch (Exception e) {
  136.       System.err.println(e.getMessage());
  137.     }
  138.   }
  139. }