ibk.java

来自「Weka」· Java 代码 · 共 1,028 行 · 第 1/3 页

JAVA
1,028
字号
/* *    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. *//* *    IBk.java *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand * */package weka.classifiers.lazy;import weka.classifiers.Classifier;import weka.classifiers.UpdateableClassifier;import weka.core.Attribute;import weka.core.Capabilities;import weka.core.Instance;import weka.core.Instances;import weka.core.neighboursearch.LinearNNSearch;import weka.core.neighboursearch.NearestNeighbourSearch;import weka.core.Option;import weka.core.OptionHandler;import weka.core.SelectedTag;import weka.core.Tag;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.core.AdditionalMeasureProducer;import java.util.Enumeration;import java.util.Vector;/** <!-- globalinfo-start --> * K-nearest neighbours classifier. Can select appropriate value of K based on cross-validation. Can also do distance weighting.<br/> * <br/> * For more information, see<br/> * <br/> * D. Aha, D. Kibler (1991). Instance-based learning algorithms. Machine Learning. 6:37-66. * <p/> <!-- globalinfo-end --> *  <!-- technical-bibtex-start --> * BibTeX: * <pre> * &#64;article{Aha1991, *    author = {D. Aha and D. Kibler}, *    journal = {Machine Learning}, *    pages = {37-66}, *    title = {Instance-based learning algorithms}, *    volume = {6}, *    year = {1991} * } * </pre> * <p/> <!-- technical-bibtex-end --> * <!-- options-start --> * Valid options are: <p/> *  * <pre> -I *  Weight neighbours by the inverse of their distance *  (use when k &gt; 1)</pre> *  * <pre> -F *  Weight neighbours by 1 - their distance *  (use when k &gt; 1)</pre> *  * <pre> -K &lt;number of neighbors&gt; *  Number of nearest neighbours (k) used in classification. *  (Default = 1)</pre> *  * <pre> -E *  Minimise mean squared error rather than mean absolute *  error when using -X option with numeric prediction.</pre> *  * <pre> -W &lt;window size&gt; *  Maximum number of training instances maintained. *  Training instances are dropped FIFO. (Default = no window)</pre> *  * <pre> -X *  Select the number of nearest neighbours between 1 *  and the k value specified using hold-one-out evaluation *  on the training data (use when k &gt; 1)</pre> *  * <pre> -A *  The nearest neighbour search algorithm to use (default: weka.core.neighboursearch.LinearNNSearch). * </pre> *  <!-- options-end --> * * @author Stuart Inglis (singlis@cs.waikato.ac.nz) * @author Len Trigg (trigg@cs.waikato.ac.nz) * @author Eibe Frank (eibe@cs.waikato.ac.nz) * @version $Revision: 1.40 $ */public class IBk   extends Classifier   implements OptionHandler, UpdateableClassifier, WeightedInstancesHandler,             TechnicalInformationHandler, AdditionalMeasureProducer {  /** for serialization. */  static final long serialVersionUID = -3080186098777067172L;  /** The training instances used for classification. */  protected Instances m_Train;  /** The number of class values (or 1 if predicting numeric). */  protected int m_NumClasses;  /** The class attribute type. */  protected int m_ClassType;  /** The number of neighbours to use for classification (currently). */  protected int m_kNN;  /**   * The value of kNN provided by the user. This may differ from   * m_kNN if cross-validation is being used.   */  protected int m_kNNUpper;  /**   * Whether the value of k selected by cross validation has   * been invalidated by a change in the training instances.   */  protected boolean m_kNNValid;  /**   * The maximum number of training instances allowed. When   * this limit is reached, old training instances are removed,   * so the training data is "windowed". Set to 0 for unlimited   * numbers of instances.   */  protected int m_WindowSize;  /** Whether the neighbours should be distance-weighted. */  protected int m_DistanceWeighting;  /** Whether to select k by cross validation. */  protected boolean m_CrossValidate;  /**   * Whether to minimise mean squared error rather than mean absolute   * error when cross-validating on numeric prediction tasks.   */  protected boolean m_MeanSquared;  /** no weighting. */  public static final int WEIGHT_NONE = 1;  /** weight by 1/distance. */  public static final int WEIGHT_INVERSE = 2;  /** weight by 1-distance. */  public static final int WEIGHT_SIMILARITY = 4;  /** possible instance weighting methods. */  public static final Tag [] TAGS_WEIGHTING = {    new Tag(WEIGHT_NONE, "No distance weighting"),    new Tag(WEIGHT_INVERSE, "Weight by 1/distance"),    new Tag(WEIGHT_SIMILARITY, "Weight by 1-distance")  };    /** for nearest-neighbor search. */  protected NearestNeighbourSearch m_NNSearch = new LinearNNSearch();  /** The number of attributes the contribute to a prediction. */  protected double m_NumAttributesUsed;    /**   * IBk classifier. Simple instance-based learner that uses the class   * of the nearest k training instances for the class of the test   * instances.   *   * @param k the number of nearest neighbors to use for prediction   */  public IBk(int k) {    init();    setKNN(k);  }    /**   * IB1 classifer. Instance-based learner. Predicts the class of the   * single nearest training instance for each test instance.   */  public IBk() {    init();  }    /**   * Returns a string describing classifier.   * @return a description suitable for   * displaying in the explorer/experimenter gui   */  public String globalInfo() {    return  "K-nearest neighbours classifier. Can "      + "select appropriate value of K based on cross-validation. Can also do "      + "distance weighting.\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;        result = new TechnicalInformation(Type.ARTICLE);    result.setValue(Field.AUTHOR, "D. Aha and D. Kibler");    result.setValue(Field.YEAR, "1991");    result.setValue(Field.TITLE, "Instance-based learning algorithms");    result.setValue(Field.JOURNAL, "Machine Learning");    result.setValue(Field.VOLUME, "6");    result.setValue(Field.PAGES, "37-66");        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 KNNTipText() {    return "The number of neighbours to use.";  }    /**   * Set the number of neighbours the learner is to use.   *   * @param k the number of neighbours.   */  public void setKNN(int k) {    m_kNN = k;    m_kNNUpper = k;    m_kNNValid = false;  }  /**   * Gets the number of neighbours the learner will use.   *   * @return the number of neighbours.   */  public int getKNN() {    return m_kNN;  }  /**   * Returns the tip text for this property.   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String windowSizeTipText() {    return "Gets the maximum number of instances allowed in the training " +      "pool. The addition of new instances above this value will result " +      "in old instances being removed. A value of 0 signifies no limit " +      "to the number of training instances.";  }    /**   * Gets the maximum number of instances allowed in the training   * pool. The addition of new instances above this value will result   * in old instances being removed. A value of 0 signifies no limit   * to the number of training instances.   *   * @return Value of WindowSize.   */  public int getWindowSize() {        return m_WindowSize;  }    /**   * Sets the maximum number of instances allowed in the training   * pool. The addition of new instances above this value will result   * in old instances being removed. A value of 0 signifies no limit   * to the number of training instances.   *   * @param newWindowSize Value to assign to WindowSize.   */  public void setWindowSize(int newWindowSize) {        m_WindowSize = newWindowSize;  }    /**   * Returns the tip text for this property.   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String distanceWeightingTipText() {    return "Gets the distance weighting method used.";  }    /**   * Gets the distance weighting method used. Will be one of   * WEIGHT_NONE, WEIGHT_INVERSE, or WEIGHT_SIMILARITY   *   * @return the distance weighting method used.   */  public SelectedTag getDistanceWeighting() {    return new SelectedTag(m_DistanceWeighting, TAGS_WEIGHTING);  }    /**   * Sets the distance weighting method used. Values other than   * WEIGHT_NONE, WEIGHT_INVERSE, or WEIGHT_SIMILARITY will be ignored.   *   * @param newMethod the distance weighting method to use   */  public void setDistanceWeighting(SelectedTag newMethod) {        if (newMethod.getTags() == TAGS_WEIGHTING) {      m_DistanceWeighting = newMethod.getSelectedTag().getID();    }  }    /**

⌨️ 快捷键说明

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