NumericToBinaryFilter.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.  *    NumericToBinaryFilter.java
  18.  *    Copyright (C) 1999 Eibe Frank
  19.  *
  20.  */
  21. package weka.filters;
  22. import java.io.*;
  23. import java.util.*;
  24. import weka.core.*;
  25. /** 
  26.  * Converts all numeric attributes into binary attributes (apart from
  27.  * the class attribute): if the value of the numeric attribute is
  28.  * exactly zero, the value of the new attribute will be zero. If the
  29.  * value of the numeric attribute is missing, the value of the new
  30.  * attribute will be missing. Otherwise, the value of the new
  31.  * attribute will be one. The new attributes will nominal.<p>
  32.  *
  33.  * @author Eibe Frank (eibe@cs.waikato.ac.nz) 
  34.  * @version $Revision: 1.7 $ 
  35.  */
  36. public class NumericToBinaryFilter extends Filter {
  37.   /**
  38.    * Sets the format of the input instances.
  39.    *
  40.    * @param instanceInfo an Instances object containing the input 
  41.    * instance structure (any instances contained in the object are 
  42.    * ignored - only the structure is required).
  43.    * @return true if the outputFormat may be collected immediately
  44.    * @exception Exception if the input format can't be set 
  45.    * successfully
  46.    */
  47.   public boolean setInputFormat(Instances instanceInfo) throws Exception {
  48.     super.setInputFormat(instanceInfo);
  49.     setOutputFormat();
  50.     return true;
  51.   }
  52.   /**
  53.    * Input an instance for filtering.
  54.    *
  55.    * @param instance the input instance
  56.    * @return true if the filtered instance may now be
  57.    * collected with output().
  58.    * @exception IllegalStateException if no input format has been defined.
  59.    */
  60.   public boolean input(Instance instance) {
  61.     if (getInputFormat() == null) {
  62.       throw new IllegalStateException("No input instance format defined");
  63.     }
  64.     if (m_NewBatch) {
  65.       resetQueue();
  66.       m_NewBatch = false;
  67.     }
  68.     convertInstance(instance);
  69.     return true;
  70.   }
  71.   /** 
  72.    * Set the output format. 
  73.    */
  74.   private void setOutputFormat() {
  75.     FastVector newAtts;
  76.     int newClassIndex;
  77.     StringBuffer attributeName;
  78.     Instances outputFormat;
  79.     FastVector vals;
  80.     // Compute new attributes
  81.     newClassIndex = getInputFormat().classIndex();
  82.     newAtts = new FastVector();
  83.     for (int j = 0; j < getInputFormat().numAttributes(); j++) {
  84.       Attribute att = getInputFormat().attribute(j);
  85.       if ((j == newClassIndex) || (!att.isNumeric())) {
  86. newAtts.addElement(att.copy());
  87.       } else {
  88. attributeName = new StringBuffer(att.name() + "_binarized");
  89. vals = new FastVector(2);
  90. vals.addElement("0"); vals.addElement("1");
  91. newAtts.addElement(new Attribute(attributeName.toString(), vals));
  92.       }
  93.     }
  94.     outputFormat = new Instances(getInputFormat().relationName(), newAtts, 0);
  95.     outputFormat.setClassIndex(newClassIndex);
  96.     setOutputFormat(outputFormat);
  97.   }
  98.   /**
  99.    * Convert a single instance over. The converted instance is 
  100.    * added to the end of the output queue.
  101.    *
  102.    * @param instance the instance to convert
  103.    */
  104.   private void convertInstance(Instance instance) {
  105.   
  106.     Instance inst = null;
  107.     if (instance instanceof SparseInstance) {
  108.       double[] vals = new double[instance.numValues()];
  109.       int[] newIndices = new int[instance.numValues()];
  110.       for (int j = 0; j < instance.numValues(); j++) {
  111. Attribute att = getInputFormat().attribute(instance.index(j));
  112. if ((!att.isNumeric()) || (instance.index(j) == getInputFormat().classIndex())) {
  113.   vals[j] = instance.valueSparse(j);
  114. } else {
  115.   if (instance.isMissingSparse(j)) {
  116.     vals[j] = instance.valueSparse(j);
  117.   } else {
  118.     vals[j] = 1;
  119.   }
  120. newIndices[j] = instance.index(j);
  121.       }
  122.       inst = new SparseInstance(instance.weight(), vals, newIndices, 
  123.                                 outputFormatPeek().numAttributes());
  124.     } else {
  125.       double[] vals = new double[outputFormatPeek().numAttributes()];
  126.       for (int j = 0; j < getInputFormat().numAttributes(); j++) {
  127. Attribute att = getInputFormat().attribute(j);
  128. if ((!att.isNumeric()) || (j == getInputFormat().classIndex())) {
  129.   vals[j] = instance.value(j);
  130. } else {
  131.   if (instance.isMissing(j) || (instance.value(j) == 0)) {
  132.     vals[j] = instance.value(j);
  133.   } else {
  134.     vals[j] = 1;
  135.   }
  136.       }
  137.       inst = new Instance(instance.weight(), vals);
  138.     }
  139.     inst.setDataset(instance.dataset());
  140.     push(inst);
  141.   }
  142.   /**
  143.    * Main method for testing this class.
  144.    *
  145.    * @param argv should contain arguments to the filter: 
  146.    * use -h for help
  147.    */
  148.   public static void main(String [] argv) {
  149.     try {
  150.       if (Utils.getFlag('b', argv)) {
  151.   Filter.batchFilterFile(new NumericToBinaryFilter(), argv);
  152.       } else {
  153. Filter.filterFile(new NumericToBinaryFilter(), argv);
  154.       }
  155.     } catch (Exception ex) {
  156.       System.out.println(ex.getMessage());
  157.     }
  158.   }
  159. }