DistributionMetaClusterer.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 6k
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.  *    DistributionMetaClusterer.java
  18.  *    Copyright (C) 2000 Intelligenesis Corp.
  19.  *
  20.  */
  21. package weka.clusterers;
  22. import java.util.Enumeration;
  23. import java.util.Random;
  24. import java.util.Vector;
  25. import weka.core.Attribute;
  26. import weka.core.Instance;
  27. import weka.core.Instances;
  28. import weka.core.Option;
  29. import weka.core.OptionHandler;
  30. import weka.core.Utils;
  31. /**
  32.  * Class that wraps up a Clusterer and presents it as a DistributionClusterer
  33.  * for ease of programmatically handling Clusterers in general -- only the
  34.  * one predict method (distributionForInstance) need be worried about. The
  35.  * distributions produced by this clusterer place a probability of 1 on the
  36.  * class value predicted by the sub-clusterer.<p>
  37.  *
  38.  * Valid options are:<p>
  39.  *
  40.  * -W classname <br>
  41.  * Specify the full class name of a sub-clusterer (required).<p>
  42.  *
  43.  * @author Richard Littin (richard@intelligenesis.net)
  44.  * @version $Revision: 1.4 $
  45.  */
  46. public class DistributionMetaClusterer extends DistributionClusterer
  47.   implements OptionHandler {
  48.   /** The clusterer. */
  49.   private Clusterer m_Clusterer = new weka.clusterers.EM();
  50.   /**
  51.    * Builds the clusterer.
  52.    *
  53.    * @param insts the training data.
  54.    * @exception Exception if a clusterer can't be built
  55.    */
  56.   public void buildClusterer(Instances insts) throws Exception {
  57.     if (m_Clusterer == null) {
  58.       throw new Exception("No base clusterer has been set!");
  59.     }
  60.     m_Clusterer.buildClusterer(insts);
  61.   }
  62.   /**
  63.    * Returns the distribution for an instance.
  64.    *
  65.    * @exception Exception if the distribution can't be computed successfully
  66.    */
  67.   public double[] distributionForInstance(Instance inst) throws Exception {
  68.     
  69.     double[] result = new double[m_Clusterer.numberOfClusters()];
  70.     int predictedCluster = m_Clusterer.clusterInstance(inst);
  71.     result[predictedCluster] = 1.0;
  72.     return result;
  73.   }
  74.   /**
  75.    * Returns the density for an instance.
  76.    *
  77.    * @exception Exception if the distribution can't be computed successfully
  78.    */
  79.   public double densityForInstance(Instance inst) throws Exception {
  80.     return Utils.sum(distributionForInstance(inst));
  81.   }
  82.   /**
  83.    * Returns the number of clusters.
  84.    *
  85.    * @return the number of clusters generated for a training dataset.
  86.    * @exception Exception if number of clusters could not be returned
  87.    * successfully
  88.    */
  89.   public int numberOfClusters() throws Exception {
  90.     return  m_Clusterer.numberOfClusters();
  91.   }
  92.   /**
  93.    * Prints the clusterers.
  94.    */
  95.   public String toString() {
  96.     return "DistributionMetaClusterer: " + m_Clusterer.toString() + "n";
  97.   }
  98.   /**
  99.    * Returns an enumeration describing the available options
  100.    *
  101.    * @return an enumeration of all the available options
  102.    */
  103.   public Enumeration listOptions() {
  104.     Vector vec = new Vector(1);
  105.     vec.addElement(new Option("tSets the base clusterer.",
  106.       "W", 1, "-W <base clusterer>"));
  107.     
  108.     if (m_Clusterer != null) {
  109.       try {
  110. vec.addElement(new Option("",
  111.   "", 0, "nOptions specific to clusterer "
  112.   + m_Clusterer.getClass().getName() + ":"));
  113. Enumeration enum = ((OptionHandler)m_Clusterer).listOptions();
  114. while (enum.hasMoreElements()) {
  115.   vec.addElement(enum.nextElement());
  116. }
  117.       } catch (Exception e) {
  118.       }
  119.     }
  120.     return vec.elements();
  121.   }
  122.   /**
  123.    * Parses a given list of options. Valid options are:<p>
  124.    *
  125.    * -W classname <br>
  126.    * Specify the full class name of a learner as the basis for 
  127.    * the multiclassclusterer (required).<p>
  128.    *
  129.    * @param options the list of options as an array of strings
  130.    * @exception Exception if an option is not supported
  131.    */
  132.   public void setOptions(String[] options) throws Exception {
  133.   
  134.     String clustererName = Utils.getOption('W', options);
  135.     if (clustererName.length() == 0) {
  136.       throw new Exception("A clusterer must be specified with"
  137.   + " the -W option.");
  138.     }
  139.     setClusterer(Clusterer.forName(clustererName,
  140.      Utils.partitionOptions(options)));
  141.   }
  142.   /**
  143.    * Gets the current settings of the Clusterer.
  144.    *
  145.    * @return an array of strings suitable for passing to setOptions
  146.    */
  147.   public String [] getOptions() {
  148.     
  149.     String [] clustererOptions = new String [0];
  150.     if ((m_Clusterer != null) &&
  151. (m_Clusterer instanceof OptionHandler)) {
  152.       clustererOptions = ((OptionHandler)m_Clusterer).getOptions();
  153.     }
  154.     String [] options = new String [clustererOptions.length + 3];
  155.     int current = 0;
  156.     if (getClusterer() != null) {
  157.       options[current++] = "-W";
  158.       options[current++] = getClusterer().getClass().getName();
  159.     }
  160.     options[current++] = "--";
  161.     System.arraycopy(clustererOptions, 0, options, current, 
  162.      clustererOptions.length);
  163.     current += clustererOptions.length;
  164.     while (current < options.length) {
  165.       options[current++] = "";
  166.     }
  167.     return options;
  168.   }
  169.   /**
  170.    * Set the base clusterer. 
  171.    *
  172.    * @param newClusterer the Clusterer to use.
  173.    */
  174.   public void setClusterer(Clusterer newClusterer) {
  175.     m_Clusterer = newClusterer;
  176.   }
  177.   /**
  178.    * Get the clusterer used as the clusterer
  179.    *
  180.    * @return the clusterer used as the clusterer
  181.    */
  182.   public Clusterer getClusterer() {
  183.     return m_Clusterer;
  184.   }
  185.   /**
  186.    * Main method for testing this class.
  187.    *
  188.    * @param argv the options
  189.    */
  190.   public static void main(String [] argv) {
  191.     try {
  192.       DistributionClusterer scheme = new DistributionMetaClusterer();
  193.       System.out.println(ClusterEvaluation.evaluateClusterer(scheme, argv));
  194.     } catch (Exception e) {
  195.       System.err.println(e.getMessage());
  196.     }
  197.   }
  198. }