MOA 12.03
Real Time Analytics for Data Streams
AddNoiseFilter.java
Go to the documentation of this file.
00001 /*
00002  *    AddNoiseFilter.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby ([email protected])
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.streams.filters;
00021 
00022 import java.util.Random;
00023 
00024 import moa.core.AutoExpandVector;
00025 import moa.core.DoubleVector;
00026 import moa.core.GaussianEstimator;
00027 import moa.core.InstancesHeader;
00028 import moa.options.FloatOption;
00029 import moa.options.IntOption;
00030 import weka.core.Instance;
00031 
00039 public class AddNoiseFilter extends AbstractStreamFilter {
00040 
00041     @Override
00042     public String getPurposeString() {
00043         return "Adds random noise to examples in a stream.";
00044     }
00045 
00046     private static final long serialVersionUID = 1L;
00047 
00048     public IntOption randomSeedOption = new IntOption("randomSeed", 'r',
00049             "Seed for random noise.", 1);
00050 
00051     public FloatOption attNoiseFractionOption = new FloatOption("attNoise",
00052             'a', "The fraction of attribute values to disturb.", 0.1, 0.0, 1.0);
00053 
00054     public FloatOption classNoiseFractionOption = new FloatOption("classNoise",
00055             'c', "The fraction of class labels to disturb.", 0.1, 0.0, 1.0);
00056 
00057     protected Random random;
00058 
00059     protected AutoExpandVector<Object> attValObservers;
00060 
00061     @Override
00062     protected void restartImpl() {
00063         this.random = new Random(this.randomSeedOption.getValue());
00064         this.attValObservers = new AutoExpandVector<Object>();
00065     }
00066 
00067     @Override
00068     public InstancesHeader getHeader() {
00069         return this.inputStream.getHeader();
00070     }
00071 
00072     @Override
00073     public Instance nextInstance() {
00074         Instance inst = (Instance) this.inputStream.nextInstance().copy();
00075         for (int i = 0; i < inst.numAttributes(); i++) {
00076             double noiseFrac = i == inst.classIndex() ? this.classNoiseFractionOption.getValue()
00077                     : this.attNoiseFractionOption.getValue();
00078             if (inst.attribute(i).isNominal()) {
00079                 DoubleVector obs = (DoubleVector) this.attValObservers.get(i);
00080                 if (obs == null) {
00081                     obs = new DoubleVector();
00082                     this.attValObservers.set(i, obs);
00083                 }
00084                 int originalVal = (int) inst.value(i);
00085                 if (!inst.isMissing(i)) {
00086                     obs.addToValue(originalVal, inst.weight());
00087                 }
00088                 if ((this.random.nextDouble() < noiseFrac)
00089                         && (obs.numNonZeroEntries() > 1)) {
00090                     do {
00091                         inst.setValue(i, this.random.nextInt(obs.numValues()));
00092                     } while (((int) inst.value(i) == originalVal)
00093                             || (obs.getValue((int) inst.value(i)) == 0.0));
00094                 }
00095             } else {
00096                 GaussianEstimator obs = (GaussianEstimator) this.attValObservers.get(i);
00097                 if (obs == null) {
00098                     obs = new GaussianEstimator();
00099                     this.attValObservers.set(i, obs);
00100                 }
00101                 obs.addObservation(inst.value(i), inst.weight());
00102                 inst.setValue(i, inst.value(i) + this.random.nextGaussian()
00103                         * obs.getStdDev() * noiseFrac);
00104             }
00105         }
00106         return inst;
00107     }
00108 
00109     @Override
00110     public void getDescription(StringBuilder sb, int indent) {
00111         // TODO Auto-generated method stub
00112     }
00113 }
 All Classes Namespaces Files Functions Variables Enumerations