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