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