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

📄 discretize.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. *//* *    Discretize.java *    Copyright (C) 1999 Eibe Frank,Len Trigg * */package weka.filters.supervised.attribute;import weka.core.Attribute;import weka.core.Capabilities;import weka.core.ContingencyTables;import weka.core.FastVector;import weka.core.Instance;import weka.core.Instances;import weka.core.Option;import weka.core.OptionHandler;import weka.core.Range;import weka.core.SparseInstance;import weka.core.SpecialFunctions;import weka.core.TechnicalInformation;import weka.core.TechnicalInformationHandler;import weka.core.Utils;import weka.core.WeightedInstancesHandler;import weka.core.Capabilities.Capability;import weka.core.TechnicalInformation.Field;import weka.core.TechnicalInformation.Type;import weka.filters.Filter;import weka.filters.SupervisedFilter;import java.util.Enumeration;import java.util.Vector;/**  <!-- globalinfo-start --> * An instance filter that discretizes a range of numeric attributes in the dataset into nominal attributes. Discretization is by Fayyad &amp; Irani's MDL method (the default).<br/> * <br/> * For more information, see:<br/> * <br/> * Usama M. Fayyad, Keki B. Irani: Multi-interval discretization of continuousvalued attributes for classification learning. In: Thirteenth International Joint Conference on Articial Intelligence, 1022-1027, 1993.<br/> * <br/> * Igor Kononenko: On Biases in Estimating Multi-Valued Attributes. In: 14th International Joint Conference on Articial Intelligence, 1034-1040, 1995. * <p/> <!-- globalinfo-end --> *  <!-- technical-bibtex-start --> * BibTeX: * <pre> * &#64;inproceedings{Fayyad1993, *    author = {Usama M. Fayyad and Keki B. Irani}, *    booktitle = {Thirteenth International Joint Conference on Articial Intelligence}, *    pages = {1022-1027}, *    publisher = {Morgan Kaufmann Publishers}, *    title = {Multi-interval discretization of continuousvalued attributes for classification learning}, *    volume = {2}, *    year = {1993} * } *  * &#64;inproceedings{Kononenko1995, *    author = {Igor Kononenko}, *    booktitle = {14th International Joint Conference on Articial Intelligence}, *    pages = {1034-1040}, *    title = {On Biases in Estimating Multi-Valued Attributes}, *    year = {1995}, *    PS = {http://ai.fri.uni-lj.si/papers/kononenko95-ijcai.ps.gz} * } * </pre> * <p/> <!-- technical-bibtex-end --> * <!-- options-start --> * Valid options are: <p/> *  * <pre> -R &lt;col1,col2-col4,...&gt; *  Specifies list of columns to Discretize. First and last are valid indexes. *  (default none)</pre> *  * <pre> -V *  Invert matching sense of column indexes.</pre> *  * <pre> -D *  Output binary attributes for discretized attributes.</pre> *  * <pre> -E *  Use better encoding of split point for MDL.</pre> *  * <pre> -K *  Use Kononenko's MDL criterion.</pre> *  <!-- options-end --> * * @author Len Trigg (trigg@cs.waikato.ac.nz) * @author Eibe Frank (eibe@cs.waikato.ac.nz) * @version $Revision: 1.6 $ */public class Discretize   extends Filter   implements SupervisedFilter, OptionHandler, WeightedInstancesHandler,   	     TechnicalInformationHandler {    /** for serialization */  static final long serialVersionUID = -3141006402280129097L;  /** Stores which columns to Discretize */  protected Range m_DiscretizeCols = new Range();  /** Store the current cutpoints */  protected double [][] m_CutPoints = null;  /** Output binary attributes for discretized attributes. */  protected boolean m_MakeBinary = false;  /** Use better encoding of split point for MDL. */  protected boolean m_UseBetterEncoding = false;  /** Use Kononenko's MDL criterion instead of Fayyad et al.'s */  protected boolean m_UseKononenko = false;  /** Constructor - initialises the filter */  public Discretize() {    setAttributeIndices("first-last");  }  /**   * Gets an enumeration describing the available options.   *   * @return an enumeration of all the available options.   */  public Enumeration listOptions() {    Vector newVector = new Vector(7);    newVector.addElement(new Option(              "\tSpecifies list of columns to Discretize. First"	      + " and last are valid indexes.\n"	      + "\t(default none)",              "R", 1, "-R <col1,col2-col4,...>"));    newVector.addElement(new Option(              "\tInvert matching sense of column indexes.",              "V", 0, "-V"));    newVector.addElement(new Option(              "\tOutput binary attributes for discretized attributes.",              "D", 0, "-D"));    newVector.addElement(new Option(              "\tUse better encoding of split point for MDL.",              "E", 0, "-E"));    newVector.addElement(new Option(              "\tUse Kononenko's MDL criterion.",              "K", 0, "-K"));    return newVector.elements();  }  /**   * Parses a given list of options. <p/>   *    <!-- options-start -->   * Valid options are: <p/>   *    * <pre> -R &lt;col1,col2-col4,...&gt;   *  Specifies list of columns to Discretize. First and last are valid indexes.   *  (default none)</pre>   *    * <pre> -V   *  Invert matching sense of column indexes.</pre>   *    * <pre> -D   *  Output binary attributes for discretized attributes.</pre>   *    * <pre> -E   *  Use better encoding of split point for MDL.</pre>   *    * <pre> -K   *  Use Kononenko's MDL criterion.</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 {    setMakeBinary(Utils.getFlag('D', options));    setUseBetterEncoding(Utils.getFlag('E', options));    setUseKononenko(Utils.getFlag('K', options));    setInvertSelection(Utils.getFlag('V', options));        String convertList = Utils.getOption('R', options);    if (convertList.length() != 0) {      setAttributeIndices(convertList);    } else {      setAttributeIndices("first-last");    }    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 [12];    int current = 0;    if (getMakeBinary()) {      options[current++] = "-D";    }    if (getUseBetterEncoding()) {      options[current++] = "-E";    }    if (getUseKononenko()) {      options[current++] = "-K";    }    if (getInvertSelection()) {      options[current++] = "-V";    }    if (!getAttributeIndices().equals("")) {      options[current++] = "-R"; options[current++] = getAttributeIndices();    }    while (current < options.length) {      options[current++] = "";    }    return options;  }  /**    * Returns the Capabilities of this filter.   *   * @return            the capabilities of this object   * @see               Capabilities   */  public Capabilities getCapabilities() {    Capabilities result = super.getCapabilities();    // attributes    result.enableAllAttributes();    result.enable(Capability.MISSING_VALUES);        // class    result.enable(Capability.NOMINAL_CLASS);        return result;  }  /**   * Sets the format of the input instances.   *   * @param instanceInfo an Instances object containing the input instance   * structure (any instances contained in the object are ignored - only the   * structure is required).   * @return true if the outputFormat may be collected immediately   * @throws Exception if the input format can't be set successfully   */  public boolean setInputFormat(Instances instanceInfo) throws Exception {    super.setInputFormat(instanceInfo);    m_DiscretizeCols.setUpper(instanceInfo.numAttributes() - 1);    m_CutPoints = null;        // If we implement loading cutfiles, then load     //them here and set the output format    return false;  }    /**   * Input an instance for filtering. Ordinarily the instance is processed   * and made available for output immediately. Some filters require all   * instances be read before producing output.   *   * @param instance the input instance   * @return true if the filtered instance may now be   * collected with output().   * @throws IllegalStateException if no input format has been defined.   */  public boolean input(Instance instance) {    if (getInputFormat() == null) {      throw new IllegalStateException("No input instance format defined");    }    if (m_NewBatch) {      resetQueue();      m_NewBatch = false;    }        if (m_CutPoints != null) {      convertInstance(instance);      return true;    }    bufferInput(instance);    return false;  }  /**   * Signifies that this batch of input to the filter is finished. If the    * filter requires all instances prior to filtering, output() may now    * be called to retrieve the filtered instances.   *   * @return true if there are instances pending output   * @throws IllegalStateException if no input structure has been defined   */  public boolean batchFinished() {    if (getInputFormat() == null) {      throw new IllegalStateException("No input instance format defined");    }    if (m_CutPoints == null) {      calculateCutPoints();      setOutputFormat();      // If we implement saving cutfiles, save the cuts here      // Convert pending input instances      for(int i = 0; i < getInputFormat().numInstances(); i++) {	convertInstance(getInputFormat().instance(i));      }    }     flushInput();    m_NewBatch = true;    return (numPendingOutput() != 0);  }  /**   * 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 discretizes a range of numeric"      + " attributes in the dataset into nominal attributes."      + " Discretization is by Fayyad & Irani's MDL method (the default).\n\n"      + "For more information, see:\n\n"      + getTechnicalInformation().toString();  }  /**   * Returns an instance of a TechnicalInformation object, containing    * detailed information about the technical background of this class,   * e.g., paper reference or book this class is based on.   *    * @return the technical information about this class   */  public TechnicalInformation getTechnicalInformation() {    TechnicalInformation 	result;    TechnicalInformation 	additional;        result = new TechnicalInformation(Type.INPROCEEDINGS);    result.setValue(Field.AUTHOR, "Usama M. Fayyad and Keki B. Irani");    result.setValue(Field.TITLE, "Multi-interval discretization of continuousvalued attributes for classification learning");    result.setValue(Field.BOOKTITLE, "Thirteenth International Joint Conference on Articial Intelligence");    result.setValue(Field.YEAR, "1993");    result.setValue(Field.VOLUME, "2");    result.setValue(Field.PAGES, "1022-1027");    result.setValue(Field.PUBLISHER, "Morgan Kaufmann Publishers");        additional = result.add(Type.INPROCEEDINGS);    additional.setValue(Field.AUTHOR, "Igor Kononenko");    additional.setValue(Field.TITLE, "On Biases in Estimating Multi-Valued Attributes");    additional.setValue(Field.BOOKTITLE, "14th International Joint Conference on Articial Intelligence");    additional.setValue(Field.YEAR, "1995");    additional.setValue(Field.PAGES, "1034-1040");    additional.setValue(Field.PS, "http://ai.fri.uni-lj.si/papers/kononenko95-ijcai.ps.gz");        return result;  }    /**   * Returns the tip text for this property   *   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String makeBinaryTipText() {    return "Make resulting attributes binary.";  }  /**   * Gets whether binary attributes should be made for discretized ones.   *   * @return true if attributes will be binarized   */  public boolean getMakeBinary() {    return m_MakeBinary;  }  /**    * Sets whether binary attributes should be made for discretized ones.   *   * @param makeBinary if binary attributes are to be made   */  public void setMakeBinary(boolean makeBinary) {    m_MakeBinary = makeBinary;  }  /**   * Returns the tip text for this property   *   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String useKononenkoTipText() {    return "Use Kononenko's MDL criterion. If set to false"      + " uses the Fayyad & Irani criterion.";  }    /**   * Gets whether Kononenko's MDL criterion is to be used.   *   * @return true if Kononenko's criterion will be used.   */  public boolean getUseKononenko() {    return m_UseKononenko;  }  /**    * Sets whether Kononenko's MDL criterion is to be used.   *   * @param useKon true if Kononenko's one is to be used   */  public void setUseKononenko(boolean useKon) {    m_UseKononenko = useKon;  }  /**   * Returns the tip text for this property   *   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String useBetterEncodingTipText() {    return "Uses a more efficient split point encoding.";  }  /**   * Gets whether better encoding is to be used for MDL.   *   * @return true if the better MDL encoding will be used   */  public boolean getUseBetterEncoding() {    return m_UseBetterEncoding;  }  /**    * Sets whether better encoding is to be used for MDL.   *   * @param useBetterEncoding true if better encoding to be used.   */  public void setUseBetterEncoding(boolean useBetterEncoding) {    m_UseBetterEncoding = useBetterEncoding;  }  /**   * Returns the tip text for this property   *   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String invertSelectionTipText() {    return "Set attribute selection mode. If false, only selected"      + " (numeric) attributes in the range will be discretized; if"      + " true, only non-selected attributes will be discretized.";  }  /**   * Gets whether the supplied columns are to be removed or kept

⌨️ 快捷键说明

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