Normalize.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 7k
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.  *    Normalize.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.  * Normalizes all numeric values in the given dataset. The resulting
  28.  * values are in [0,1] for the data used to compute the normalization
  29.  * intervals.
  30.  *
  31.  * @author Eibe Frank (eibe@cs.waikato.ac.nz) 
  32.  * @version $Revision: 1.1 $
  33.  */
  34. public class Normalize extends Filter implements UnsupervisedFilter {
  35.   /** The minimum values for numeric attributes. */
  36.   private double [] m_MinArray;
  37.   
  38.   /** The maximum values for numeric attributes. */
  39.   private double [] m_MaxArray;
  40.   /**
  41.    * Sets the format of the input instances.
  42.    *
  43.    * @param instanceInfo an Instances object containing the input 
  44.    * instance structure (any instances contained in the object are 
  45.    * ignored - only the structure is required).
  46.    * @return true if the outputFormat may be collected immediately
  47.    * @exception Exception if the input format can't be set 
  48.    * successfully
  49.    */
  50.   public boolean setInputFormat(Instances instanceInfo) 
  51.        throws Exception {
  52.     super.setInputFormat(instanceInfo);
  53.     setOutputFormat(instanceInfo);
  54.     m_MinArray = m_MaxArray = null;
  55.     return true;
  56.   }
  57.   /**
  58.    * Input an instance for filtering. Filter requires all
  59.    * training instances be read before producing output.
  60.    *
  61.    * @param instance the input instance
  62.    * @return true if the filtered instance may now be
  63.    * collected with output().
  64.    * @exception IllegalStateException if no input format has been set.
  65.    */
  66.   public boolean input(Instance instance) {
  67.     if (getInputFormat() == null) {
  68.       throw new IllegalStateException("No input instance format defined");
  69.     }
  70.     if (m_NewBatch) {
  71.       resetQueue();
  72.       m_NewBatch = false;
  73.     }
  74.     if (m_MinArray == null) {
  75.       bufferInput(instance);
  76.       return false;
  77.     } else {
  78.       convertInstance(instance);
  79.       return true;
  80.     }
  81.   }
  82.   /**
  83.    * Signify that this batch of input to the filter is finished. 
  84.    * If the filter requires all instances prior to filtering,
  85.    * output() may now be called to retrieve the filtered instances.
  86.    *
  87.    * @return true if there are instances pending output
  88.    * @exception IllegalStateException if no input structure has been defined
  89.    */
  90.   public boolean batchFinished() {
  91.     if (getInputFormat() == null) {
  92.       throw new IllegalStateException("No input instance format defined");
  93.     }
  94.     if (m_MinArray == null) {
  95.       Instances input = getInputFormat();
  96.       // Compute minimums and maximums
  97.       m_MinArray = new double[input.numAttributes()];
  98.       m_MaxArray = new double[input.numAttributes()];
  99.       for (int i = 0; i < input.numAttributes(); i++) {
  100. m_MinArray[i] = Double.NaN;
  101.       }
  102.       for (int j = 0; j < input.numInstances(); j++) {
  103. double[] value = input.instance(j).toDoubleArray();
  104. for (int i = 0; i < input.numAttributes(); i++) {
  105.   if (input.attribute(i).isNumeric()) {
  106.     if (!Instance.isMissingValue(value[i])) {
  107.       if (Double.isNaN(m_MinArray[i])) {
  108. m_MinArray[i] = m_MaxArray[i] = value[i];
  109.       } else {
  110. if (value[i] < m_MinArray[i]) {
  111.   m_MinArray[i] = value[i];
  112. }
  113. if (value[i] > m_MaxArray[i]) {
  114.   m_MaxArray[i] = value[i];
  115. }
  116.       }
  117.     }
  118.   }
  119.       }
  120.       // Convert pending input instances
  121.       for(int i = 0; i < input.numInstances(); i++) {
  122. convertInstance(input.instance(i));
  123.       }
  124.     } 
  125.     // Free memory
  126.     flushInput();
  127.     m_NewBatch = true;
  128.     return (numPendingOutput() != 0);
  129.   }
  130.   /**
  131.    * Convert a single instance over. The converted instance is 
  132.    * added to the end of the output queue.
  133.    *
  134.    * @param instance the instance to convert
  135.    */
  136.   private void convertInstance(Instance instance) {
  137.   
  138.     Instance inst = null;
  139.     if (instance instanceof SparseInstance) {
  140.       double[] newVals = new double[instance.numAttributes()];
  141.       int[] newIndices = new int[instance.numAttributes()];
  142.       double[] vals = instance.toDoubleArray();
  143.       int ind = 0;
  144.       for (int j = 0; j < instance.numAttributes(); j++) {
  145. double value;
  146. if (instance.attribute(j).isNumeric() &&
  147.     (!Instance.isMissingValue(vals[j]))) {
  148.   if (Double.isNaN(m_MinArray[j]) ||
  149.       (m_MaxArray[j] == m_MinArray[j])) {
  150.     value = 0;
  151.   } else {
  152.     value = (vals[j] - m_MinArray[j]) / 
  153.       (m_MaxArray[j] - m_MinArray[j]);
  154.   }
  155.   if (value != 0.0) {
  156.     newVals[ind] = value;
  157.     newIndices[ind] = j;
  158.     ind++;
  159.   }
  160. } else {
  161.   value = vals[j];
  162.   if (value != 0.0) {
  163.     newVals[ind] = value;
  164.     newIndices[ind] = j;
  165.     ind++;
  166.   }
  167. }
  168.       }
  169.       double[] tempVals = new double[ind];
  170.       int[] tempInd = new int[ind];
  171.       System.arraycopy(newVals, 0, tempVals, 0, ind);
  172.       System.arraycopy(newIndices, 0, tempInd, 0, ind);
  173.       inst = new SparseInstance(instance.weight(), tempVals, tempInd,
  174.                                 instance.numAttributes());
  175.     } else {
  176.       double[] vals = instance.toDoubleArray();
  177.       for (int j = 0; j < getInputFormat().numAttributes(); j++) {
  178. if (instance.attribute(j).isNumeric() &&
  179.     (!Instance.isMissingValue(vals[j]))) {
  180.   if (Double.isNaN(m_MinArray[j]) ||
  181.       (m_MaxArray[j] == m_MinArray[j])) {
  182.     vals[j] = 0;
  183.   } else {
  184.     vals[j] = (vals[j] - m_MinArray[j]) / 
  185.       (m_MaxArray[j] - m_MinArray[j]);
  186.   }
  187. }
  188.       }
  189.       inst = new Instance(instance.weight(), vals);
  190.     }
  191.     inst.setDataset(instance.dataset());
  192.     push(inst);
  193.   }
  194.   /**
  195.    * Main method for testing this class.
  196.    *
  197.    * @param argv should contain arguments to the filter: 
  198.    * use -h for help
  199.    */
  200.   public static void main(String [] argv) {
  201.     try {
  202.       if (Utils.getFlag('b', argv)) {
  203.   Filter.batchFilterFile(new Normalize(), argv);
  204.       } else {
  205. Filter.filterFile(new Normalize(), argv);
  206.       }
  207.     } catch (Exception ex) {
  208.       System.out.println(ex.getMessage());
  209.     }
  210.   }
  211. }