Clusterer.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 4k
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.  *    Clusterer.java
  18.  *    Copyright (C) 1999 Mark Hall
  19.  *
  20.  */
  21. package weka.clusterers;
  22. import java.io.Serializable;
  23. import weka.core.Instance;
  24. import weka.core.Instances;
  25. import weka.core.SerializedObject;
  26. import weka.core.Utils;
  27. /** 
  28.  * Abstract clusterer.
  29.  *
  30.  * @author Mark Hall (mhall@cs.waikato.ac.nz)
  31.  * @version $Revision: 1.8 $
  32.  */
  33. public abstract class Clusterer implements Cloneable, Serializable {
  34.   // ===============
  35.   // Public methods.
  36.   // ===============
  37.  
  38.   /**
  39.    * Generates a clusterer. Has to initialize all fields of the clusterer
  40.    * that are not being set via options.
  41.    *
  42.    * @param data set of instances serving as training data 
  43.    * @exception Exception if the clusterer has not been 
  44.    * generated successfully
  45.    */
  46.   public abstract void buildClusterer(Instances data) throws Exception;
  47.   /**
  48.    * Classifies a given instance.
  49.    *
  50.    * @param instance the instance to be assigned to a cluster
  51.    * @return the number of the assigned cluster as an interger
  52.    * if the class is enumerated, otherwise the predicted value
  53.    * @exception Exception if instance could not be classified
  54.    * successfully
  55.    */
  56.   public abstract int clusterInstance(Instance instance) throws Exception; 
  57.   /**
  58.    * Returns the number of clusters.
  59.    *
  60.    * @return the number of clusters generated for a training dataset.
  61.    * @exception Exception if number of clusters could not be returned
  62.    * successfully
  63.    */
  64.   public abstract int numberOfClusters() throws Exception;
  65.   /**
  66.    * Creates a new instance of a clusterer given it's class name and
  67.    * (optional) arguments to pass to it's setOptions method. If the
  68.    * clusterer implements OptionHandler and the options parameter is
  69.    * non-null, the clusterer will have it's options set.
  70.    *
  71.    * @param searchName the fully qualified class name of the clusterer
  72.    * @param options an array of options suitable for passing to setOptions. May
  73.    * be null.
  74.    * @return the newly created search object, ready for use.
  75.    * @exception Exception if the clusterer class name is invalid, or the 
  76.    * options supplied are not acceptable to the clusterer.
  77.    */
  78.   public static Clusterer forName(String clustererName,
  79.   String [] options) throws Exception
  80.   {
  81.     return (Clusterer)Utils.forName(Clusterer.class,
  82.     clustererName,
  83.     options);
  84.   }
  85.   /**
  86.    * Creates copies of the current clusterer. Note that this method
  87.    * now uses Serialization to perform a deep copy, so the Clusterer
  88.    * object must be fully Serializable. Any currently built model will
  89.    * now be copied as well.
  90.    *
  91.    * @param model an example clusterer to copy
  92.    * @param num the number of clusterer copies to create.
  93.    * @return an array of clusterers.
  94.    * @exception Exception if an error occurs 
  95.    */
  96.   public static Clusterer [] makeCopies(Clusterer model,
  97. int num) throws Exception {
  98.      if (model == null) {
  99.       throw new Exception("No model clusterer set");
  100.     }
  101.     Clusterer [] clusterers = new Clusterer [num];
  102.     SerializedObject so = new SerializedObject(model);
  103.     for(int i = 0; i < clusterers.length; i++) {
  104.       clusterers[i] = (Clusterer) so.getObject();
  105.     }
  106.     return clusterers;
  107.   }
  108. }