MOA 12.03
Real Time Analytics for Data Streams
OzaBag.java
Go to the documentation of this file.
00001 /*
00002  *    OzaBag.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.classifiers.meta;
00021 
00022 import moa.classifiers.AbstractClassifier;
00023 import moa.classifiers.Classifier;
00024 import weka.core.Instance;
00025 
00026 import moa.core.DoubleVector;
00027 import moa.core.Measurement;
00028 import moa.core.MiscUtils;
00029 import moa.options.ClassOption;
00030 import moa.options.IntOption;
00031 
00052 public class OzaBag extends AbstractClassifier {
00053 
00054     @Override
00055     public String getPurposeString() {
00056         return "Incremental on-line bagging of Oza and Russell.";
00057     }
00058         
00059     private static final long serialVersionUID = 1L;
00060 
00061     public ClassOption baseLearnerOption = new ClassOption("baseLearner", 'l',
00062             "Classifier to train.", Classifier.class, "trees.HoeffdingTree");
00063 
00064     public IntOption ensembleSizeOption = new IntOption("ensembleSize", 's',
00065             "The number of models in the bag.", 10, 1, Integer.MAX_VALUE);
00066 
00067     protected Classifier[] ensemble;
00068 
00069     @Override
00070     public void resetLearningImpl() {
00071         this.ensemble = new Classifier[this.ensembleSizeOption.getValue()];
00072         Classifier baseLearner = (Classifier) getPreparedClassOption(this.baseLearnerOption);
00073         baseLearner.resetLearning();
00074         for (int i = 0; i < this.ensemble.length; i++) {
00075             this.ensemble[i] = baseLearner.copy();
00076         }
00077     }
00078 
00079     @Override
00080     public void trainOnInstanceImpl(Instance inst) {
00081         for (int i = 0; i < this.ensemble.length; i++) {
00082             int k = MiscUtils.poisson(1.0, this.classifierRandom);
00083             if (k > 0) {
00084                 Instance weightedInst = (Instance) inst.copy();
00085                 weightedInst.setWeight(inst.weight() * k);
00086                 this.ensemble[i].trainOnInstance(weightedInst);
00087             }
00088         }
00089     }
00090 
00091     @Override
00092     public double[] getVotesForInstance(Instance inst) {
00093         DoubleVector combinedVote = new DoubleVector();
00094         for (int i = 0; i < this.ensemble.length; i++) {
00095             DoubleVector vote = new DoubleVector(this.ensemble[i].getVotesForInstance(inst));
00096             if (vote.sumOfValues() > 0.0) {
00097                 vote.normalize();
00098                 combinedVote.addValues(vote);
00099             }
00100         }
00101         return combinedVote.getArrayRef();
00102     }
00103 
00104     @Override
00105     public boolean isRandomizable() {
00106         return true;
00107     }
00108 
00109     @Override
00110     public void getModelDescription(StringBuilder out, int indent) {
00111         // TODO Auto-generated method stub
00112     }
00113 
00114     @Override
00115     protected Measurement[] getModelMeasurementsImpl() {
00116         return new Measurement[]{new Measurement("ensemble size",
00117                     this.ensemble != null ? this.ensemble.length : 0)};
00118     }
00119 
00120     @Override
00121     public Classifier[] getSubClassifiers() {
00122         return this.ensemble.clone();
00123     }
00124 }
 All Classes Namespaces Files Functions Variables Enumerations