ibk.java

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

JAVA
1,028
字号
   * Returns the tip text for this property.   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String meanSquaredTipText() {    return "Whether the mean squared error is used rather than mean "      + "absolute error when doing cross-validation for regression problems.";  }  /**   * Gets whether the mean squared error is used rather than mean   * absolute error when doing cross-validation.   *   * @return true if so.   */  public boolean getMeanSquared() {        return m_MeanSquared;  }    /**   * Sets whether the mean squared error is used rather than mean   * absolute error when doing cross-validation.   *   * @param newMeanSquared true if so.   */  public void setMeanSquared(boolean newMeanSquared) {        m_MeanSquared = newMeanSquared;  }    /**   * Returns the tip text for this property.   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String crossValidateTipText() {    return "Whether hold-one-out cross-validation will be used " +      "to select the best k value.";  }    /**   * Gets whether hold-one-out cross-validation will be used   * to select the best k value.   *   * @return true if cross-validation will be used.   */  public boolean getCrossValidate() {        return m_CrossValidate;  }    /**   * Sets whether hold-one-out cross-validation will be used   * to select the best k value.   *   * @param newCrossValidate true if cross-validation should be used.   */  public void setCrossValidate(boolean newCrossValidate) {        m_CrossValidate = newCrossValidate;  }  /**   * Returns the tip text for this property.   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String nearestNeighbourSearchAlgorithmTipText() {    return "The nearest neighbour search algorithm to use " +    	   "(Default: weka.core.neighboursearch.LinearNNSearch).";  }    /**   * Returns the current nearestNeighbourSearch algorithm in use.   * @return the NearestNeighbourSearch algorithm currently in use.   */  public NearestNeighbourSearch getNearestNeighbourSearchAlgorithm() {    return m_NNSearch;  }    /**   * Sets the nearestNeighbourSearch algorithm to be used for finding nearest   * neighbour(s).   * @param nearestNeighbourSearchAlgorithm - The NearestNeighbourSearch class.   */  public void setNearestNeighbourSearchAlgorithm(NearestNeighbourSearch nearestNeighbourSearchAlgorithm) {    m_NNSearch = nearestNeighbourSearchAlgorithm;  }     /**   * Get the number of training instances the classifier is currently using.   *    * @return the number of training instances the classifier is currently using   */  public int getNumTraining() {    return m_Train.numInstances();  }  /**   * Returns default capabilities of the classifier.   *   * @return      the capabilities of this classifier   */  public Capabilities getCapabilities() {    Capabilities result = super.getCapabilities();    // attributes    result.enable(Capability.NOMINAL_ATTRIBUTES);    result.enable(Capability.NUMERIC_ATTRIBUTES);    result.enable(Capability.DATE_ATTRIBUTES);    result.enable(Capability.MISSING_VALUES);    // class    result.enable(Capability.NOMINAL_CLASS);    result.enable(Capability.NUMERIC_CLASS);    result.enable(Capability.DATE_CLASS);    result.enable(Capability.MISSING_CLASS_VALUES);    // instances    result.setMinimumNumberInstances(0);        return result;  }    /**   * Generates the classifier.   *   * @param instances set of instances serving as training data    * @throws Exception if the classifier has not been generated successfully   */  public void buildClassifier(Instances instances) throws Exception {        // can classifier handle the data?    getCapabilities().testWithFail(instances);    // remove instances with missing class    instances = new Instances(instances);    instances.deleteWithMissingClass();        m_NumClasses = instances.numClasses();    m_ClassType = instances.classAttribute().type();    m_Train = new Instances(instances, 0, instances.numInstances());    // Throw away initial instances until within the specified window size    if ((m_WindowSize > 0) && (instances.numInstances() > m_WindowSize)) {      m_Train = new Instances(m_Train, 			      m_Train.numInstances()-m_WindowSize, 			      m_WindowSize);    }    m_NumAttributesUsed = 0.0;    for (int i = 0; i < m_Train.numAttributes(); i++) {      if ((i != m_Train.classIndex()) && 	  (m_Train.attribute(i).isNominal() ||	   m_Train.attribute(i).isNumeric())) {	m_NumAttributesUsed += 1.0;      }    }        m_NNSearch.setInstances(m_Train);    // Invalidate any currently cross-validation selected k    m_kNNValid = false;  }  /**   * Adds the supplied instance to the training set.   *   * @param instance the instance to add   * @throws Exception if instance could not be incorporated   * successfully   */  public void updateClassifier(Instance instance) throws Exception {    if (m_Train.equalHeaders(instance.dataset()) == false) {      throw new Exception("Incompatible instance types");    }    if (instance.classIsMissing()) {      return;    }    m_Train.add(instance);    m_NNSearch.update(instance);    m_kNNValid = false;    if ((m_WindowSize > 0) && (m_Train.numInstances() > m_WindowSize)) {      boolean deletedInstance=false;      while (m_Train.numInstances() > m_WindowSize) {	m_Train.delete(0);        deletedInstance=true;      }      //rebuild datastructure KDTree currently can't delete      if(deletedInstance==true)        m_NNSearch.setInstances(m_Train);    }  }  /**   * Calculates the class membership probabilities for the given test instance.   *   * @param instance the instance to be classified   * @return predicted class probability distribution   * @throws Exception if an error occurred during the prediction   */  public double [] distributionForInstance(Instance instance) throws Exception {    if (m_Train.numInstances() == 0) {      throw new Exception("No training instances!");    }    if ((m_WindowSize > 0) && (m_Train.numInstances() > m_WindowSize)) {      m_kNNValid = false;      boolean deletedInstance=false;      while (m_Train.numInstances() > m_WindowSize) {	m_Train.delete(0);      }      //rebuild datastructure KDTree currently can't delete      if(deletedInstance==true)        m_NNSearch.setInstances(m_Train);    }    // Select k by cross validation    if (!m_kNNValid && (m_CrossValidate) && (m_kNNUpper >= 1)) {      crossValidate();    }    m_NNSearch.addInstanceInfo(instance);    Instances neighbours = m_NNSearch.kNearestNeighbours(instance, m_kNN);    double [] distances = m_NNSearch.getDistances();    double [] distribution = makeDistribution( neighbours, distances );    return distribution;  }  /**   * Returns an enumeration describing the available options.   *   * @return an enumeration of all the available options.   */  public Enumeration listOptions() {    Vector newVector = new Vector(8);    newVector.addElement(new Option(	      "\tWeight neighbours by the inverse of their distance\n"+	      "\t(use when k > 1)",	      "I", 0, "-I"));    newVector.addElement(new Option(	      "\tWeight neighbours by 1 - their distance\n"+	      "\t(use when k > 1)",	      "F", 0, "-F"));    newVector.addElement(new Option(	      "\tNumber of nearest neighbours (k) used in classification.\n"+	      "\t(Default = 1)",	      "K", 1,"-K <number of neighbors>"));    newVector.addElement(new Option(          "\tMinimise mean squared error rather than mean absolute\n"+	      "\terror when using -X option with numeric prediction.",	      "E", 0,"-E"));    newVector.addElement(new Option(          "\tMaximum number of training instances maintained.\n"+	      "\tTraining instances are dropped FIFO. (Default = no window)",	      "W", 1,"-W <window size>"));    newVector.addElement(new Option(	      "\tSelect the number of nearest neighbours between 1\n"+	      "\tand the k value specified using hold-one-out evaluation\n"+	      "\ton the training data (use when k > 1)",	      "X", 0,"-X"));    newVector.addElement(new Option(	      "\tThe nearest neighbour search algorithm to use "+          "(default: weka.core.neighboursearch.LinearNNSearch).\n",	      "A", 0, "-A"));    return newVector.elements();  }  /**   * Parses a given list of options. <p/>   *   <!-- 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 -->   *   * @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 knnString = Utils.getOption('K', options);    if (knnString.length() != 0) {      setKNN(Integer.parseInt(knnString));    } else {      setKNN(1);    }    String windowString = Utils.getOption('W', options);    if (windowString.length() != 0) {      setWindowSize(Integer.parseInt(windowString));    } else {      setWindowSize(0);    }    if (Utils.getFlag('I', options)) {      setDistanceWeighting(new SelectedTag(WEIGHT_INVERSE, TAGS_WEIGHTING));    } else if (Utils.getFlag('F', options)) {      setDistanceWeighting(new SelectedTag(WEIGHT_SIMILARITY, TAGS_WEIGHTING));    } else {      setDistanceWeighting(new SelectedTag(WEIGHT_NONE, TAGS_WEIGHTING));    }    setCrossValidate(Utils.getFlag('X', options));    setMeanSquared(Utils.getFlag('E', options));

⌨️ 快捷键说明

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