MOA 12.03
Real Time Analytics for Data Streams
classifiers/meta/MOA.java
Go to the documentation of this file.
00001 /*
00002  *    This program is free software; you can redistribute it and/or modify
00003  *    it under the terms of the GNU General Public License as published by
00004  *    the Free Software Foundation; either version 3 of the License, or
00005  *    (at your option) any later version.
00006  *
00007  *    This program is distributed in the hope that it will be useful,
00008  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *    GNU General Public License for more details.
00011  *
00012  *    You should have received a copy of the GNU General Public License
00013  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00014  *    
00015  */
00016 
00017 /*
00018  * MOA.java
00019  * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
00020  */
00021 
00022 package weka.classifiers.meta;
00023 
00024 import weka.classifiers.UpdateableClassifier;
00025 import weka.core.Capabilities;
00026 import weka.core.Instance;
00027 import weka.core.Instances;
00028 import weka.core.MOAUtils;
00029 import weka.core.Option;
00030 import weka.core.RevisionUtils;
00031 import weka.core.Utils;
00032 import weka.core.Capabilities.Capability;
00033 
00034 import java.util.Enumeration;
00035 import java.util.Vector;
00036 
00037 import moa.classifiers.Classifier;
00038 import moa.classifiers.trees.DecisionStump;
00039 import moa.options.ClassOption;
00040 
00065 public class MOA
00066   extends weka.classifiers.AbstractClassifier
00067   implements UpdateableClassifier {
00068 
00070         private static final long serialVersionUID = 2605797948130310166L;
00071 
00073         protected Classifier m_ActualClassifier = new DecisionStump();
00074 
00076         protected ClassOption m_Classifier = new ClassOption(
00077                         "classifier", 'B', "The MOA classifier to use from within WEKA.",
00078                         Classifier.class, m_ActualClassifier.getClass().getName().replace("moa.classifiers.", ""),
00079                         m_ActualClassifier.getClass().getName());
00080 
00087   public String globalInfo() {
00088     return
00089         "Wrapper for MOA classifiers.\n\n"
00090       + "Since MOA doesn't offer a mechanism to query a classifier for the "
00091       + "types of attributes and classes it can handle, the capabilities of "
00092       + "this wrapper are hard-coded: nominal and numeric attributes and "
00093       + "only nominal class attributes are allowed.";
00094   }
00095 
00101   public Enumeration listOptions() {
00102     Vector result = new Vector();
00103 
00104     result.addElement(new Option(
00105         "\tThe MOA classifier to use.\n"
00106         + "\t(default: " + MOAUtils.toCommandLine(new DecisionStump()) + ")",
00107         "B", 1, "-B <classname + options>"));
00108 
00109     Enumeration en = super.listOptions();
00110     while (en.hasMoreElements())
00111       result.addElement(en.nextElement());
00112 
00113     return result.elements();
00114   }
00115 
00135   public void setOptions(String[] options) throws Exception {
00136     String                              tmpStr;
00137     ClassOption                                 option;
00138 
00139     tmpStr = Utils.getOption('B', options);
00140     option = (ClassOption) m_Classifier.copy();
00141     if (tmpStr.length() == 0)
00142         option.setCurrentObject(new DecisionStump());
00143     else
00144         option.setCurrentObject(MOAUtils.fromCommandLine(m_Classifier, tmpStr));
00145     setClassifier(option);
00146 
00147     super.setOptions(options);
00148   }
00149 
00155   public String[] getOptions() {
00156     Vector<String>      result;
00157     String[]            options;
00158     int                 i;
00159 
00160     result = new Vector<String>();
00161 
00162     result.add("-B");
00163     result.add(MOAUtils.toCommandLine(m_ActualClassifier));
00164 
00165     options = super.getOptions();
00166     for (i = 0; i < options.length; i++)
00167       result.add(options[i]);
00168 
00169     return result.toArray(new String[result.size()]);
00170   }
00171 
00177   public void setClassifier(ClassOption value) {
00178         m_Classifier       = value;
00179         m_ActualClassifier = (Classifier) MOAUtils.fromOption(m_Classifier);
00180   }
00181 
00187   public ClassOption getClassifier() {
00188         return m_Classifier;
00189   }
00190 
00196   public String classifierTipText() {
00197         return "The MOA classifier to use.";
00198   }
00199 
00207   public Capabilities getCapabilities() {
00208     Capabilities result = new Capabilities(this);
00209 
00210     // attributes
00211     result.enable(Capability.NOMINAL_ATTRIBUTES);
00212     result.enable(Capability.NUMERIC_ATTRIBUTES);
00213     result.enable(Capability.MISSING_VALUES);
00214 
00215     // class
00216     result.enable(Capability.NOMINAL_CLASS);
00217     result.enable(Capability.MISSING_CLASS_VALUES);
00218 
00219     result.setMinimumNumberInstances(0);
00220 
00221     return result;
00222   }
00223 
00231   public void buildClassifier(Instances data) throws Exception {
00232         getCapabilities().testWithFail(data);
00233 
00234         data = new Instances(data);
00235         data.deleteWithMissingClass();
00236 
00237         m_ActualClassifier.resetLearning();
00238         for (int i = 0; i < data.numInstances(); i++)
00239                 updateClassifier(data.instance(i));
00240   }
00241 
00249   public void updateClassifier(Instance instance) throws Exception {
00250                 m_ActualClassifier.trainOnInstance(instance);
00251   }
00252 
00267   public double[] distributionForInstance(Instance instance) throws Exception {
00268         double[]        result;
00269 
00270         result = m_ActualClassifier.getVotesForInstance(instance);
00271         // ensure that the array has as many elements as there are
00272         // class values!
00273         if (result.length < instance.numClasses()) {
00274           double[] newResult = new double[instance.numClasses()];
00275           System.arraycopy(result, 0, newResult, 0, result.length);
00276           result = newResult;
00277         }
00278 
00279         try {
00280                 Utils.normalize(result);
00281         }
00282         catch (Exception e) {
00283                 result = new double[instance.numClasses()];
00284         }
00285 
00286         return result;
00287   }
00288 
00294   public String getRevision() {
00295     return RevisionUtils.extract("$Revision$");
00296   }
00297 
00303   public String toString() {
00304         StringBuilder           result;
00305 
00306         result = new StringBuilder();
00307         m_ActualClassifier.getDescription(result, 0);
00308 
00309         return result.toString();
00310   }
00311 
00317   public static void main(String [] args) {
00318     runClassifier(new MOA(), args);
00319   }
00320 }
 All Classes Namespaces Files Functions Variables Enumerations