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

📄 oner.java

📁 Weka
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    OneRRule r;    // ... create array to hold the missing value counts    int[] missingValueCounts =      new int [data.classAttribute().numValues()];        if (attr.isNominal()) {      r = newNominalRule(attr, data, missingValueCounts);    } else {      r = newNumericRule(attr, data, missingValueCounts);    }    r.m_missingValueClass = Utils.maxIndex(missingValueCounts);    if (missingValueCounts[r.m_missingValueClass] == 0) {      r.m_missingValueClass = -1; // signal for no missing value class    } else {      r.m_correct += missingValueCounts[r.m_missingValueClass];    }    return r;  }  /**   * Create a rule branching on this nominal attribute.   *   * @param attr the attribute to branch on   * @param data the data to be used for creating the rule   * @param missingValueCounts to be filled in   * @return the generated rule   * @throws Exception if the rule can't be built successfully   */  public OneRRule newNominalRule(Attribute attr, Instances data,                                 int[] missingValueCounts) throws Exception {    // ... create arrays to hold the counts    int[][] counts = new int [attr.numValues()]                             [data.classAttribute().numValues()];          // ... calculate the counts    Enumeration enu = data.enumerateInstances();    while (enu.hasMoreElements()) {      Instance i = (Instance) enu.nextElement();      if (i.isMissing(attr)) {	missingValueCounts[(int) i.classValue()]++;       } else {	counts[(int) i.value(attr)][(int) i.classValue()]++;      }    }    OneRRule r = new OneRRule(data, attr); // create a new rule    for (int value = 0; value < attr.numValues(); value++) {      int best = Utils.maxIndex(counts[value]);      r.m_classifications[value] = best;      r.m_correct += counts[value][best];    }    return r;  }  /**   * Create a rule branching on this numeric attribute   *   * @param attr the attribute to branch on   * @param data the data to be used for creating the rule   * @param missingValueCounts to be filled in   * @return the generated rule   * @throws Exception if the rule can't be built successfully   */  public OneRRule newNumericRule(Attribute attr, Instances data,                             int[] missingValueCounts) throws Exception {    // ... can't be more than numInstances buckets    int [] classifications = new int[data.numInstances()];    double [] breakpoints = new double[data.numInstances()];    // create array to hold the counts    int [] counts = new int[data.classAttribute().numValues()];    int correct = 0;    int lastInstance = data.numInstances();    // missing values get sorted to the end of the instances    data.sort(attr);    while (lastInstance > 0 &&            data.instance(lastInstance-1).isMissing(attr)) {      lastInstance--;      missingValueCounts[(int) data.instance(lastInstance).                         classValue()]++;     }    int i = 0;     int cl = 0; // index of next bucket to create    int it;    while (i < lastInstance) { // start a new bucket      for (int j = 0; j < counts.length; j++) counts[j] = 0;      do { // fill it until it has enough of the majority class        it = (int) data.instance(i++).classValue();        counts[it]++;      } while (counts[it] < m_minBucketSize && i < lastInstance);      // while class remains the same, keep on filling      while (i < lastInstance &&              (int) data.instance(i).classValue() == it) {         counts[it]++;         i++;      }      while (i < lastInstance && // keep on while attr value is the same             (data.instance(i - 1).value(attr) 	      == data.instance(i).value(attr))) {        counts[(int) data.instance(i++).classValue()]++;      }      for (int j = 0; j < counts.length; j++) {        if (counts[j] > counts[it]) { 	  it = j;	}      }      if (cl > 0) { // can we coalesce with previous class?        if (counts[classifications[cl - 1]] == counts[it]) {          it = classifications[cl - 1];	}        if (it == classifications[cl - 1]) {	  cl--; // yes!	}      }      correct += counts[it];      classifications[cl] = it;      if (i < lastInstance) {        breakpoints[cl] = (data.instance(i - 1).value(attr)			   + data.instance(i).value(attr)) / 2;      }      cl++;    }    if (cl == 0) {      throw new Exception("Only missing values in the training data!");    }    OneRRule r = new OneRRule(data, attr, cl); // new rule with cl branches    r.m_correct = correct;    for (int v = 0; v < cl; v++) {      r.m_classifications[v] = classifications[v];      if (v < cl-1) {	r.m_breakpoints[v] = breakpoints[v];      }    }    return r;  }  /**   * Returns an enumeration describing the available options..   *   * @return an enumeration of all the available options.   */  public Enumeration listOptions() {    String string = "\tThe minimum number of objects in a bucket (default: 6).";    Vector newVector = new Vector(1);    newVector.addElement(new Option(string, "B", 1, 				    "-B <minimum bucket size>"));    return newVector.elements();  }  /**   * Parses a given list of options. <p/>   *   <!-- options-start -->   * Valid options are: <p/>   *    * <pre> -B &lt;minimum bucket size&gt;   *  The minimum number of objects in a bucket (default: 6).</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 bucketSizeString = Utils.getOption('B', options);    if (bucketSizeString.length() != 0) {      m_minBucketSize = Integer.parseInt(bucketSizeString);    } else {      m_minBucketSize = 6;    }  }  /**   * Gets the current settings of the OneR classifier.   *   * @return an array of strings suitable for passing to setOptions   */  public String [] getOptions() {    String [] options = new String [2];    int current = 0;    options[current++] = "-B"; options[current++] = "" + m_minBucketSize;    while (current < options.length) {      options[current++] = "";    }    return options;  }  /**   * Returns a string that describes the classifier as source. The   * classifier will be contained in a class with the given name (there may   * be auxiliary classes),   * and will contain a method with the signature:   * <pre><code>   * public static double classify(Object[] i);   * </code></pre>   * where the array <code>i</code> contains elements that are either   * Double, String, with missing values represented as null. The generated   * code is public domain and comes with no warranty.   *   * @param className the name that should be given to the source class.   * @return the object source described by a string   * @throws Exception if the souce can't be computed   */  public String toSource(String className) throws Exception {    StringBuffer        result;    int                 i;        result = new StringBuffer();        if (m_ZeroR != null) {      result.append(((ZeroR) m_ZeroR).toSource(className));    }    else {      result.append("class " + className + " {\n");      result.append("  public static double classify(Object[] i) {\n");      result.append("    // chosen attribute: " + m_rule.m_attr.name() + " (" + m_rule.m_attr.index() + ")\n");      result.append("\n");      // missing values      result.append("    // missing value?\n");      result.append("    if (i[" + m_rule.m_attr.index() + "] == null)\n");      if (m_rule.m_missingValueClass != -1)        result.append("      return Double.NaN;\n");      else        result.append("      return 0;\n");      result.append("\n");            // actual prediction      result.append("    // prediction\n");      result.append("    double v = 0;\n");      result.append("    double[] classifications = new double[]{" + Utils.arrayToString(m_rule.m_classifications) + "};");      result.append(" // ");      for (i = 0; i < m_rule.m_classifications.length; i++) {        if (i > 0)          result.append(", ");        result.append(m_rule.m_class.value(m_rule.m_classifications[i]));      }      result.append("\n");      if (m_rule.m_attr.isNominal()) {        for (i = 0; i < m_rule.m_attr.numValues(); i++) {          result.append("    ");          if (i > 0)            result.append("else ");          result.append("if (((String) i[" + m_rule.m_attr.index() + "]).equals(\"" + m_rule.m_attr.value(i) + "\"))\n");          result.append("      v = " + i + "; // " + m_rule.m_class.value(m_rule.m_classifications[i]) + "\n");        }      }      else {        result.append("    double[] breakpoints = new double[]{" + Utils.arrayToString(m_rule.m_breakpoints) + "};\n");        result.append("    while (v < breakpoints.length && \n");        result.append("           ((Double) i[" + m_rule.m_attr.index() + "]) >= breakpoints[(int) v]) {\n");        result.append("      v++;\n");        result.append("    }\n");      }      result.append("    return classifications[(int) v];\n");            result.append("  }\n");      result.append("}\n");    }        return result.toString();  }  /**   * Returns a description of the classifier   *    * @return a string representation of the classifier   */  public String toString() {    // only ZeroR model?    if (m_ZeroR != null) {      StringBuffer buf = new StringBuffer();      buf.append(this.getClass().getName().replaceAll(".*\\.", "") + "\n");      buf.append(this.getClass().getName().replaceAll(".*\\.", "").replaceAll(".", "=") + "\n\n");      buf.append("Warning: No model could be built, hence ZeroR model is used:\n\n");      buf.append(m_ZeroR.toString());      return buf.toString();    }        if (m_rule == null) {      return "OneR: No model built yet.";    }    return m_rule.toString();  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String minBucketSizeTipText() {    return "The minimum bucket size used for discretizing numeric "      + "attributes.";  }    /**   * Get the value of minBucketSize.   * @return Value of minBucketSize.   */  public int getMinBucketSize() {        return m_minBucketSize;  }    /**   * Set the value of minBucketSize.   * @param v  Value to assign to minBucketSize.   */  public void setMinBucketSize(int v) {        m_minBucketSize = v;  }    /**   * Main method for testing this class   *    * @param argv the commandline options   */  public static void main(String [] argv) {    runClassifier(new OneR(), argv);  }}

⌨️ 快捷键说明

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