Classifier.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.  *    Classifier.java
  18.  *    Copyright (C) 1999 Eibe Frank, Len Trigg
  19.  *
  20.  */
  21. package weka.classifiers;
  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 classifier. All schemes for numeric or nominal prediction in
  29.  * Weka extend this class.
  30.  *
  31.  * @author Eibe Frank (eibe@cs.waikato.ac.nz)
  32.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  33.  * @version $Revision: 1.8 $
  34.  */
  35. public abstract class Classifier implements Cloneable, Serializable {
  36.  
  37.   /**
  38.    * Generates a classifier. Must initialize all fields of the classifier
  39.    * that are not being set via options (ie. multiple calls of buildClassifier
  40.    * must always lead to the same result). Must not change the dataset
  41.    * in any way.
  42.    *
  43.    * @param data set of instances serving as training data 
  44.    * @exception Exception if the classifier has not been 
  45.    * generated successfully
  46.    */
  47.   public abstract void buildClassifier(Instances data) throws Exception;
  48.   /**
  49.    * Classifies a given instance.
  50.    *
  51.    * @param instance the instance to be classified
  52.    * @return index of the predicted class as a double
  53.    * if the class is nominal, otherwise the predicted value
  54.    * @exception Exception if instance could not be classified
  55.    * successfully
  56.    */
  57.   public abstract double classifyInstance(Instance instance) throws Exception; 
  58.   
  59.   /**
  60.    * Creates a new instance of a classifier given it's class name and
  61.    * (optional) arguments to pass to it's setOptions method. If the
  62.    * classifier implements OptionHandler and the options parameter is
  63.    * non-null, the classifier will have it's options set.
  64.    *
  65.    * @param classifierName the fully qualified class name of the classifier
  66.    * @param options an array of options suitable for passing to setOptions. May
  67.    * be null.
  68.    * @return the newly created classifier, ready for use.
  69.    * @exception Exception if the classifier name is invalid, or the options
  70.    * supplied are not acceptable to the classifier
  71.    */
  72.   public static Classifier forName(String classifierName,
  73.    String [] options) throws Exception {
  74.     return (Classifier)Utils.forName(Classifier.class,
  75.      classifierName,
  76.      options);
  77.   }
  78.   /**
  79.    * Creates copies of the current classifier, which can then
  80.    * be used for boosting etc. Note that this method now uses
  81.    * Serialization to perform a deep copy, so the Classifier
  82.    * object must be fully Serializable. Any currently built model
  83.    * will now be copied as well.
  84.    *
  85.    * @param model an example classifier to copy
  86.    * @param num the number of classifiers copies to create.
  87.    * @return an array of classifiers.
  88.    * @exception Exception if an error occurs
  89.    */
  90.   public static Classifier [] makeCopies(Classifier model,
  91.  int num) throws Exception {
  92.     if (model == null) {
  93.       throw new Exception("No model classifier set");
  94.     }
  95.     Classifier [] classifiers = new Classifier [num];
  96.     SerializedObject so = new SerializedObject(model);
  97.     for(int i = 0; i < classifiers.length; i++) {
  98.       classifiers[i] = (Classifier) so.getObject();
  99.     }
  100.     return classifiers;
  101.   }
  102. }