📄 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.core.Capabilities;import weka.core.Instance;import weka.core.Instances;import weka.core.Option;import weka.core.OptionHandler;import weka.core.SingleIndex;import weka.core.Utils;import weka.core.Capabilities.Capability;import weka.filters.Filter;import weka.filters.UnsupervisedFilter;import java.util.Enumeration;import java.util.Random;import java.util.Vector;/** <!-- globalinfo-start --> * 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. * <p/> <!-- globalinfo-end --> * <!-- options-start --> * Valid options are: <p/> * * <pre> -C <col> * Index of the attribute to be changed * (default last attribute)</pre> * * <pre> -M * Treat missing values as an extra value * </pre> * * <pre> -P <num> * Specify the percentage of noise introduced * to the data (default 10)</pre> * * <pre> -S <num> * Specify the random number seed (default 1)</pre> * <!-- options-end --> * * @author Gabi Schmidberger (gabi@cs.waikato.ac.nz) * @version $Revision: 1.6 $ */public class AddNoise extends Filter implements UnsupervisedFilter, OptionHandler { /** for serialization */ static final long serialVersionUID = -8499673222857299082L; /** The attribute's index setting. */ private SingleIndex m_AttIndex = new SingleIndex("last"); /** 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; /** * 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(4); 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 given list of options. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -C <col> * Index of the attribute to be changed * (default last attribute)</pre> * * <pre> -M * Treat missing values as an extra value * </pre> * * <pre> -P <num> * Specify the percentage of noise introduced * to the data (default 10)</pre> * * <pre> -S <num> * Specify the random number seed (default 1)</pre> * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { String indexString = Utils.getOption('C', options); if (indexString.length() != 0) { setAttributeIndex(indexString); } else { setAttributeIndex("last"); } 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 [7]; int current = 0; options[current++] = "-C"; options[current++] = "" + getAttributeIndex(); 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."; } /** * Get the index of the attribute used. * * @return the index of the attribute */ public String getAttributeIndex() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -