📄 addnoise.java
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * AddNoise.java * Copyright (C) 2000 Gabi Schmidberger. */package weka.filters.unsupervised.attribute;import weka.filters.*;import weka.core.Instance;import weka.core.Instances;import weka.core.Attribute;import weka.core.OptionHandler;import weka.core.Option;import weka.core.Utils;import java.util.Random;import java.util.Enumeration;import java.util.Vector;/** * Introduces noise data a random subsample of the dataset * by changing a given attribute * (attribute must be nominal) * Valid options are:<p> * * -C col <br> * Index of the attribute to be changed. (default last)<p> * * -M <br> * flag: missing values are treated as an extra value <p> * * -P num <br> * Percentage of noise to be introduced to the data (default 10).<p> * * -S seed <br> * Random number seed for choosing the data to be changed <p> * and for choosing the value it is changed to (default 1). <p> * * @author Gabi Schmidberger (gabi@cs.waikato.ac.nz) * @version $Revision: 1.1.1.1 $ **/public class AddNoise extends Filter implements UnsupervisedFilter, OptionHandler { /** The index of the attribute to be changed. */ private int m_AttIndexSet = -1; /** Flag if missing values are taken as value. */ private boolean m_UseMissing = false; /** The subsample size, percent of original set, default 10% */ private int m_Percent = 10; /** The random number generator seed */ private int m_RandomSeed = 1; /** The attribute's index */ private int m_AttIndex; /** True if the first batch has been done */ private boolean m_FirstBatchDone = false; /** * Returns a string describing this filter * * @return a description of the filter suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "An instance filter that changes a percentage of a given" + " attributes values. The attribute must be nominal." + " Missing value can be treated as value itself."; } /** * Returns an enumeration describing the available options * * @return an enumeration of all the available options */ public Enumeration listOptions() { Vector newVector = new Vector(1); newVector.addElement(new Option( "\tIndex of the attribute to be changed \n" +"\t(default last attribute)", "C", 1, "-C <col>")); newVector.addElement(new Option( "\tTreat missing values as an extra value \n", "M", 1, "-M")); newVector.addElement(new Option( "\tSpecify the percentage of noise introduced \n" +"\tto the data (default 10)", "P", 1, "-P <num>")); newVector.addElement(new Option( "\tSpecify the random number seed (default 1)", "S", 1, "-S <num>")); return newVector.elements(); } /** * Parses a list of options for this object. Valid options are:<p> * * -C col <br> * Index of the attribute to be changed. (default last)<p> * * -M <br> * missing values are treated as an extra value <p> * * -P num <br> * Specify the percentage of noise introduced to the data (default 10).<p> * * -S num <br> * Specify the random number seed (default 1).<p> * * @param options the list of options as an array of strings * @exception Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { String indexString = Utils.getOption('C', options); if (indexString.length() != 0) { setAttIndexSet((int)Double.valueOf(indexString).doubleValue()); } else { setAttIndexSet(-1); } if (Utils.getFlag('M', options)) { setUseMissing(true); } String percentString = Utils.getOption('P', options); if (percentString.length() != 0) { setPercent((int) Double.valueOf(percentString).doubleValue()); } else { setPercent(10); } String seedString = Utils.getOption('S', options); if (seedString.length() != 0) { setRandomSeed(Integer.parseInt(seedString)); } else { setRandomSeed(1); } } /** * Gets the current settings of the filter. * * @return an array of strings suitable for passing to setOptions */ public String [] getOptions() { String [] options = new String [8]; int current = 0; options[current++] = "-C"; options[current++] = "" + getAttIndexSet(); if (getUseMissing()) { options[current++] = "-M"; } options[current++] = "-P"; options[current++] = "" + getPercent(); options[current++] = "-S"; options[current++] = "" + getRandomSeed(); while (current < options.length) { options[current++] = ""; } return options; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String useMissingTipText() { return "Flag to set if missing values are used."; } /** * Gets the flag if missing values are treated as extra values. * * @return the flag missing values. */ public boolean getUseMissing() { return m_UseMissing; } /** * Sets the flag if missing values are treated as extra values. * * @param newUseMissing the new flag value. */ public void setUseMissing(boolean newUseMissing) { m_UseMissing = newUseMissing; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String randomSeedTipText() { return "Random number seed."; } /** * Gets the random number seed. * * @return the random number seed. */ public int getRandomSeed() { return m_RandomSeed; } /** * Sets the random number seed. * * @param newSeed the new random number seed. */ public void setRandomSeed(int newSeed) { m_RandomSeed = newSeed; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String percentTipText() { return "Percentage of introduced noise to data."; } /** * Gets the size of noise data as a percentage of the original set. * * @return the noise data size */ public int getPercent() { return m_Percent; } /** * Sets the size of noise data, as a percentage of the original set. * * @param newPercent the subsample set size, between 0 and 100. */ public void setPercent(int newPercent) { m_Percent = newPercent; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String attIndexSetTipText() { return "Index of the attribute that is to changed."; } /** * Gets the Index of the Attribute that is to be changed. * * @return the attribute index that is to be changed */ public int getAttIndexSet() { return m_AttIndexSet; } /** * Sets the Index of the Attribute to be changed.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -