OneRAttributeEval.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 5k
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.  *    OneRAttributeEval.java
  18.  *    Copyright (C) 1999 Mark Hall
  19.  *
  20.  */
  21. package  weka.attributeSelection;
  22. import  java.io.*;
  23. import  java.util.*;
  24. import  weka.core.*;
  25. import  weka.classifiers.*;
  26. import  weka.filters.*;
  27. /** 
  28.  * Class for Evaluating attributes individually by using the OneR
  29.  * classifier. <p>
  30.  *
  31.  * No options. <p>
  32.  *
  33.  * @author Mark Hall (mhall@cs.waikato.ac.nz)
  34.  * @version $Revision: 1.8 $
  35.  */
  36. public class OneRAttributeEval
  37.   extends AttributeEvaluator
  38. {
  39.   /** The training instances */
  40.   private Instances m_trainInstances;
  41.   /** The class index */
  42.   private int m_classIndex;
  43.   /** The number of attributes */
  44.   private int m_numAttribs;
  45.   /** The number of instances */
  46.   private int m_numInstances;
  47.   /**
  48.    * Returns a string describing this attribute evaluator
  49.    * @return a description of the evaluator suitable for
  50.    * displaying in the explorer/experimenter gui
  51.    */
  52.   public String globalInfo() {
  53.     return "OneRAttributeEval :nnEvaluates the worth of an attribute by "
  54.       +"using the OneR classifier.n";
  55.   }
  56.   /**
  57.    * Constructor
  58.    */
  59.   public OneRAttributeEval () {
  60.     resetOptions();
  61.   }
  62.   /**
  63.    * Initializes an information gain attribute evaluator.
  64.    * Discretizes all attributes that are numeric.
  65.    *
  66.    * @param data set of instances serving as training data 
  67.    * @exception Exception if the evaluator has not been 
  68.    * generated successfully
  69.    */
  70.   public void buildEvaluator (Instances data)
  71.     throws Exception
  72.   {
  73.     m_trainInstances = data;
  74.     if (m_trainInstances.checkForStringAttributes()) {
  75.       throw  new Exception("Can't handle string attributes!");
  76.     }
  77.     m_classIndex = m_trainInstances.classIndex();
  78.     m_numAttribs = m_trainInstances.numAttributes();
  79.     m_numInstances = m_trainInstances.numInstances();
  80.     if (m_trainInstances.attribute(m_classIndex).isNumeric()) {
  81.       throw  new Exception("Class must be nominal!");
  82.     }
  83.   }
  84.   /**
  85.    * rests to defaults.
  86.    */
  87.   protected void resetOptions () {
  88.     m_trainInstances = null;
  89.   }
  90.   /**
  91.    * evaluates an individual attribute by measuring the amount
  92.    * of information gained about the class given the attribute.
  93.    *
  94.    * @param attribute the index of the attribute to be evaluated
  95.    * @exception Exception if the attribute could not be evaluated
  96.    */
  97.   public double evaluateAttribute (int attribute)
  98.     throws Exception
  99.   {
  100.     int[] featArray = new int[2]; // feat + class
  101.     double errorRate;
  102.     Evaluation o_Evaluation;
  103.     AttributeFilter delTransform = new AttributeFilter();
  104.     delTransform.setInvertSelection(true);
  105.     // copy the instances
  106.     Instances trainCopy = new Instances(m_trainInstances);
  107.     featArray[0] = attribute;
  108.     featArray[1] = trainCopy.classIndex();
  109.     delTransform.setAttributeIndicesArray(featArray);
  110.     delTransform.setInputFormat(trainCopy);
  111.     trainCopy = Filter.useFilter(trainCopy, delTransform);
  112.     o_Evaluation = new Evaluation(trainCopy);
  113.     o_Evaluation.crossValidateModel("weka.classifiers.OneR", trainCopy, 10, null);
  114.     errorRate = o_Evaluation.errorRate();
  115.     return  (1 - errorRate)*100.0;
  116.   }
  117.   /**
  118.    * Return a description of the evaluator
  119.    * @return description as a string
  120.    */
  121.   public String toString () {
  122.     StringBuffer text = new StringBuffer();
  123.     if (m_trainInstances == null) {
  124.       text.append("tOneR feature evaluator has not been built yet");
  125.     }
  126.     else {
  127.       text.append("tOneR feature evaluator");
  128.     }
  129.     text.append("n");
  130.     return  text.toString();
  131.   }
  132.   // ============
  133.   // Test method.
  134.   // ============
  135.   /**
  136.    * Main method for testing this class.
  137.    *
  138.    * @param args the options
  139.    */
  140.   public static void main (String[] args) {
  141.     try {
  142.       System.out.println(AttributeSelection.
  143.  SelectAttributes(new OneRAttributeEval(), args));
  144.     }
  145.     catch (Exception e) {
  146.       e.printStackTrace();
  147.       System.out.println(e.getMessage());
  148.     }
  149.   }
  150. }