cobweb.java

来自「Java 编写的多种数据挖掘算法 包括聚类、分类、预处理等」· Java 代码 · 共 1,153 行 · 第 1/3 页

JAVA
1,153
字号
  protected CNode m_cobwebTree = null;  /**   * Number of clusters (nodes in the tree).   */  protected int m_numberOfClusters = -1;    protected int m_numberSplits;  protected int m_numberMerges;  /**   * Output instances in graph representation of Cobweb tree (Allows   * instances at nodes in the tree to be visualized in the Explorer).   */  protected boolean m_saveInstances = false;  /**   * Returns a string describing this clusterer   * @return a description of the evaluator suitable for   * displaying in the explorer/experimenter gui   */  public String globalInfo() {    return         "Class implementing the Cobweb and Classit clustering algorithms.\n\n"      + "Note: the application of node operators (merging, splitting etc.) in "      + "terms of ordering and priority differs (and is somewhat ambiguous) "      + "between the original Cobweb and Classit papers. This algorithm always "      + "compares the best host, adding a new leaf, merging the two best hosts, "      + "and splitting the best host when considering where to place a new "      + "instance.\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.ARTICLE);    result.setValue(Field.AUTHOR, "D. Fisher");    result.setValue(Field.YEAR, "1987");    result.setValue(Field.TITLE, "Knowledge acquisition via incremental conceptual clustering");    result.setValue(Field.JOURNAL, "Machine Learning");    result.setValue(Field.VOLUME, "2");    result.setValue(Field.NUMBER, "2");    result.setValue(Field.PAGES, "139-172");        additional = result.add(Type.ARTICLE);    additional.setValue(Field.AUTHOR, "J. H. Gennari and P. Langley and D. Fisher");    additional.setValue(Field.YEAR, "1990");    additional.setValue(Field.TITLE, "Models of incremental concept formation");    additional.setValue(Field.JOURNAL, "Artificial Intelligence");    additional.setValue(Field.VOLUME, "40");    additional.setValue(Field.PAGES, "11-61");        return result;  }  /**   * Returns default capabilities of the clusterer.   *   * @return      the capabilities of this clusterer   */  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);    return result;  }  /**   * Builds the clusterer.   *   * @param data the training instances.   * @throws Exception if something goes wrong.   */  public void buildClusterer(Instances data) throws Exception {    m_numberOfClusters = -1;    m_cobwebTree = null;    m_numberSplits = 0;    m_numberMerges = 0;    // can clusterer handle the data?    getCapabilities().testWithFail(data);    // randomize the instances    data = new Instances(data);    data.randomize(new Random(42));    for (int i = 0; i < data.numInstances(); i++) {      addInstance(data.instance(i));    }        int [] numClusts = new int [1];    numClusts[0] = 0;    m_cobwebTree.assignClusterNums(numClusts);    m_numberOfClusters = numClusts[0];  }  /**   * Classifies a given instance.   *   * @param instance the instance to be assigned to a cluster   * @return the number of the assigned cluster as an interger   * if the class is enumerated, otherwise the predicted value   * @throws Exception if instance could not be classified   * successfully   */  public int clusterInstance(Instance instance) throws Exception {    CNode host = m_cobwebTree;    CNode temp = null;        do {      if (host.m_children == null) {	temp = null;	break;      }      host.updateStats(instance, false);      temp = host.findHost(instance, true);      host.updateStats(instance, true);            if (temp != null) {	host = temp;      }    } while (temp != null);        return host.m_clusterNum;  }  /**   * Returns the number of clusters.   *   * @return the number of clusters   */  public int numberOfClusters() {    return m_numberOfClusters;  }  /**   * Adds an instance to the Cobweb tree.   *   * @param newInstance the instance to be added   * @throws Exception if something goes wrong   */  public void addInstance(Instance newInstance) throws Exception {    if (m_cobwebTree == null) {      m_cobwebTree = new CNode(newInstance.numAttributes(), newInstance);    } else {      m_cobwebTree.addInstance(newInstance);    }  }  /**   * Returns an enumeration describing the available options.   *   * @return an enumeration of all the available options.   **/  public Enumeration listOptions() {        Vector newVector = new Vector(2);        newVector.addElement(new Option("\tAcuity.\n"				    +"\t(default=1.0)", "A", 1,"-A <acuity>"));    newVector.addElement(new Option("\tCutoff.\n"				    +"\t(default=0.002)", "C", 1,"-C <cutoff>"));        return newVector.elements();  }  /**   * Parses a given list of options. <p/>   *   <!-- options-start -->   * Valid options are: <p/>   *    * <pre> -A &lt;acuity&gt;   *  Acuity.   *  (default=1.0)</pre>   *    * <pre> -C &lt;cutoff&gt;   *  Cutoff.   *  (default=0.002)</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 optionString;    optionString = Utils.getOption('A', options);     if (optionString.length() != 0) {      Double temp = new Double(optionString);      setAcuity(temp.doubleValue());    }    else {      m_acuity = 1.0;    }    optionString = Utils.getOption('C', options);     if (optionString.length() != 0) {      Double temp = new Double(optionString);      setCutoff(temp.doubleValue());    }    else {      m_cutoff = 0.01 * Cobweb.m_normal;    }  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String acuityTipText() {    return "set the minimum standard deviation for numeric attributes";  }  /**   * set the acuity.   * @param a the acuity value   */  public void setAcuity(double a) {    m_acuity = a;  }  /**   * get the acuity value   * @return the acuity   */  public double getAcuity() {    return m_acuity;  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String cutoffTipText() {    return "set the category utility threshold by which to prune nodes";  }  /**   * set the cutoff   * @param c the cutof   */  public void setCutoff(double c) {    m_cutoff = c;  }  /**   * get the cutoff   * @return the cutoff   */  public double getCutoff() {    return m_cutoff;  }    /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String saveInstanceDataTipText() {    return "save instance information for visualization purposes";  }  /**   * Get the value of saveInstances.   *   * @return Value of saveInstances.   */  public boolean getSaveInstanceData() {        return m_saveInstances;  }    /**   * Set the value of saveInstances.   *   * @param newsaveInstances Value to assign to saveInstances.   */  public void setSaveInstanceData(boolean newsaveInstances) {        m_saveInstances = newsaveInstances;  }    /**   * Gets the current settings of Cobweb.   *   * @return an array of strings suitable for passing to setOptions()   */  public String [] getOptions() {        String [] options = new String [4];    int current = 0;    options[current++] = "-A";     options[current++] = "" + m_acuity;    options[current++] = "-C";     options[current++] = "" + m_cutoff;    while (current < options.length) {      options[current++] = "";    }    return options;  }  /**   * Returns a description of the clusterer as a string.   *   * @return a string describing the clusterer.   */  public String toString() {     StringBuffer text = new StringBuffer();    if (m_cobwebTree == null) {      return "Cobweb hasn't been built yet!";    }    else {      m_cobwebTree.dumpTree(0, text);       return "Number of merges: "	+ m_numberMerges+"\nNumber of splits: "	+ m_numberSplits+"\nNumber of clusters: "	+ m_numberOfClusters+"\n"+text.toString()+"\n\n";         }  }      /**   *  Returns the type of graphs this class   *  represents   *  @return Drawable.TREE   */     public int graphType() {      return Drawable.TREE;  }  /**   * Generates the graph string of the Cobweb tree   *   * @return a <code>String</code> value   * @throws Exception if an error occurs   */  public String graph() throws Exception {    StringBuffer text = new StringBuffer();        text.append("digraph CobwebTree {\n");    m_cobwebTree.graphTree(text);    text.append("}\n");    return text.toString();  }  /**    * Main method   *    * @param argv the commandline options   */  public static void main(String [] argv) {    try {      System.out.println(ClusterEvaluation.evaluateClusterer(new Cobweb(), 							     argv));    }    catch (Exception e) {      System.out.println(e.getMessage());      e.printStackTrace();    }  }}

⌨️ 快捷键说明

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