⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 removefrequentvalues.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *    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. *//* *    RemoveFrequentValues.java *    Copyright (C) 2004 FracPete * */package weka.filters.unsupervised.instance;import weka.core.Attribute;import weka.core.AttributeStats;import weka.core.Capabilities;import weka.core.FastVector;import weka.core.Instance;import weka.core.Instances;import weka.core.Option;import weka.core.OptionHandler;import weka.core.SingleIndex;import weka.core.UnsupportedAttributeTypeException;import weka.core.Utils;import weka.core.Capabilities.Capability;import weka.filters.Filter;import weka.filters.UnsupervisedFilter;import java.util.Arrays;import java.util.Enumeration;import java.util.HashSet;import java.util.Iterator;import java.util.Vector;/**  <!-- globalinfo-start --> * Determines which values (frequent or infrequent ones) of an (nominal) attribute are retained and filters the instances accordingly. In case of values with the same frequency, they are kept in the way they appear in the original instances object. E.g. if you have the values "1,2,3,4" with the frequencies "10,5,5,3" and you chose to keep the 2 most common values, the values "1,2" would be returned, since the value "2" comes before "3", even though they have the same frequency. * <p/> <!-- globalinfo-end --> *  <!-- options-start --> * Valid options are: <p/> *  * <pre> -C &lt;num&gt; *  Choose attribute to be used for selection.</pre> *  * <pre> -N &lt;num&gt; *  Number of values to retain for the sepcified attribute,  *  i.e. the ones with the most instances (default 2).</pre> *  * <pre> -L *  Instead of values with the most instances the ones with the  *  least are retained. * </pre> *  * <pre> -H *  When selecting on nominal attributes, removes header *  references to excluded values.</pre> *  * <pre> -V *  Invert matching sense.</pre> *  <!-- options-end --> * * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision: 1.4 $ */public class RemoveFrequentValues    extends Filter    implements OptionHandler, UnsupervisedFilter {     /** for serialization */   static final long serialVersionUID = -2447432930070059511L;   /** The attribute's index setting. */   private SingleIndex m_AttIndex = new SingleIndex("last");    /** the number of values to retain. */   protected int m_NumValues = 2;       /** whether to retain values with least instances instead of most. */   protected boolean m_LeastValues = false;      /** whether to invert the matching sense. */   protected boolean m_Invert = false;      /** Modify header for nominal attributes? */   protected boolean m_ModifyHeader = false;      /** If m_ModifyHeader, stores a mapping from old to new indexes */   protected int [] m_NominalMapping;      /** contains the values to retain */   protected HashSet m_Values = null;      /**    * Returns a string describing this filter    * @return a description of the classifier suitable for    * displaying in the explorer/experimenter gui    */   public String globalInfo() {     return          "Determines which values (frequent or infrequent ones) of an "       + "(nominal) attribute are retained and filters the instances "       + "accordingly. In case of values with the same frequency, they are "       + "kept in the way they appear in the original instances object. E.g. "       + "if you have the values \"1,2,3,4\" with the frequencies \"10,5,5,3\" "       + "and you chose to keep the 2 most common values, the values \"1,2\" "       + "would be returned, since the value \"2\" comes before \"3\", even "       + "though they have the same frequency.";   }   /**    * Returns an enumeration describing the available options.    *    * @return an enumeration of all the available options.    */   public Enumeration listOptions() {      Vector newVector = new Vector(5);      newVector.addElement(new Option(                "\tChoose attribute to be used for selection.",                "C", 1, "-C <num>"));      newVector.addElement(new Option(                  "\tNumber of values to retain for the sepcified attribute, \n"                + "\ti.e. the ones with the most instances (default 2).",                "N", 1, "-N <num>"));      newVector.addElement(new Option(                  "\tInstead of values with the most instances the ones with the \n"                + "\tleast are retained.\n",                "L", 0, "-L"));      newVector.addElement(new Option(                  "\tWhen selecting on nominal attributes, removes header\n"            	 + "\treferences to excluded values.",            	 "H", 0, "-H"));      newVector.addElement(new Option(            	 "\tInvert matching sense.",                "V", 0, "-V"));      return newVector.elements();   }   /**    * Parses a given list of options. <p/>    *     <!-- options-start -->    * Valid options are: <p/>    *     * <pre> -C &lt;num&gt;    *  Choose attribute to be used for selection.</pre>    *     * <pre> -N &lt;num&gt;    *  Number of values to retain for the sepcified attribute,     *  i.e. the ones with the most instances (default 2).</pre>    *     * <pre> -L    *  Instead of values with the most instances the ones with the     *  least are retained.    * </pre>    *     * <pre> -H    *  When selecting on nominal attributes, removes header    *  references to excluded values.</pre>    *     * <pre> -V    *  Invert matching sense.</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 attIndex = Utils.getOption('C', options);      if (attIndex.length() != 0) {         setAttributeIndex(attIndex);      } else {         setAttributeIndex("last");      }            String numValues = Utils.getOption('N', options);      if (numValues.length() != 0) {         setNumValues(Integer.parseInt(numValues));      } else {         setNumValues(2);      }            setUseLeastValues(Utils.getFlag('L', options));      setModifyHeader(Utils.getFlag('H', options));      setInvertSelection(Utils.getFlag('V', options));            if (getInputFormat() != null) {         setInputFormat(getInputFormat());      }   }   /**    * 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());      options[current++] = "-N";      options[current++] = "" + (getNumValues());      if (getUseLeastValues()) {        options[current++] = "-H";      }      if (getModifyHeader()) {         options[current++] = "-H";      }      if (getInvertSelection()) {         options[current++] = "-V";      }      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 attributeIndexTipText() {     return "Choose attribute to be used for selection (default last).";   }   /**    * Get the index of the attribute used.    *    * @return the index of the attribute    */   public String getAttributeIndex() {     return m_AttIndex.getSingleIndex();   }   /**    * Sets index of the attribute used.    *    * @param attIndex the index of the attribute    */   public void setAttributeIndex(String attIndex) {     m_AttIndex.setSingleIndex(attIndex);   }   /**    * Returns the tip text for this property    *     * @return tip text for this property suitable for    * displaying in the explorer/experimenter gui    */   public String numValuesTipText() {      return "The number of values to retain.";   }   /**    * Gets how many values are retained    *    * @return how many values are retained    */   public int getNumValues() {      return m_NumValues;   }   /**    * Sets how many values are retained      *    * @param numValues the number of values to retain    */   public void setNumValues(int numValues) {      m_NumValues = numValues;   }   /**    * Returns the tip text for this property    *     * @return tip text for this property suitable for    * displaying in the explorer/experimenter gui    */   public String useLeastValuesTipText() {      return "Retains values with least instance instead of most.";   }   /**    * Gets whether to use values with least or most instances    *    * @return true if values with least instances are retained    */   public boolean getUseLeastValues() {      return m_LeastValues;   }   /**    * Sets whether to use values with least or most instances      *    * @param leastValues whether values with least or most instances are retained    */   public void setUseLeastValues(boolean leastValues) {      m_LeastValues = leastValues;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -