MOA 12.03
Real Time Analytics for Data Streams
MajorityClass.java
Go to the documentation of this file.
00001 /*
00002  *    MajorityClass.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.functions;
00021 
00022 import moa.classifiers.AbstractClassifier;
00023 import moa.core.DoubleVector;
00024 import moa.core.Measurement;
00025 import moa.core.StringUtils;
00026 import weka.core.Instance;
00027 
00034 public class MajorityClass extends AbstractClassifier {
00035 
00036     private static final long serialVersionUID = 1L;
00037 
00038     @Override
00039     public String getPurposeString() {
00040         return "Majority class classifier: always predicts the class that has been observed most frequently the in the training data.";
00041     }
00042 
00043     protected DoubleVector observedClassDistribution;
00044 
00045     @Override
00046     public void resetLearningImpl() {
00047         this.observedClassDistribution = new DoubleVector();
00048     }
00049 
00050     @Override
00051     public void trainOnInstanceImpl(Instance inst) {
00052         this.observedClassDistribution.addToValue((int) inst.classValue(), inst.weight());
00053     }
00054 
00055     public double[] getVotesForInstance(Instance i) {
00056         return this.observedClassDistribution.getArrayCopy();
00057     }
00058 
00059     @Override
00060     protected Measurement[] getModelMeasurementsImpl() {
00061         return null;
00062     }
00063 
00064     @Override
00065     public void getModelDescription(StringBuilder out, int indent) {
00066         StringUtils.appendIndented(out, indent, "Predicted majority ");
00067         out.append(getClassNameString());
00068         out.append(" = ");
00069         out.append(getClassLabelString(this.observedClassDistribution.maxIndex()));
00070         StringUtils.appendNewline(out);
00071         for (int i = 0; i < this.observedClassDistribution.numValues(); i++) {
00072             StringUtils.appendIndented(out, indent, "Observed weight of ");
00073             out.append(getClassLabelString(i));
00074             out.append(": ");
00075             out.append(this.observedClassDistribution.getValue(i));
00076             StringUtils.appendNewline(out);
00077         }
00078     }
00079 
00080     public boolean isRandomizable() {
00081         return false;
00082     }
00083 }
 All Classes Namespaces Files Functions Variables Enumerations