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

📄 kstar.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   * @param first the test instance.   * @param second the train instance.   * @param col the index of the attribute in the instance.   * @return the value of the transformation probability.   */  private double attrTransProb(Instance first, Instance second, int col) {    String debug = "(KStar.attrTransProb)";    double transProb = 0.0;    KStarNominalAttribute ksNominalAttr;    KStarNumericAttribute ksNumericAttr;    switch ( m_Train.attribute(col).type() )      {      case Attribute.NOMINAL:	ksNominalAttr = new KStarNominalAttribute(first, second, col, m_Train, 						  m_RandClassCols, 						  m_Cache[col]);	ksNominalAttr.setOptions(m_MissingMode, m_BlendMethod, m_GlobalBlend);	transProb = ksNominalAttr.transProb();	ksNominalAttr = null;	break;      case Attribute.NUMERIC:	ksNumericAttr = new KStarNumericAttribute(first, second, col, 						  m_Train, m_RandClassCols, 						  m_Cache[col]);	ksNumericAttr.setOptions(m_MissingMode, m_BlendMethod, m_GlobalBlend);	transProb = ksNumericAttr.transProb();	ksNumericAttr = null;	break;      }    return transProb;  }     /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String missingModeTipText() {    return "Determines how missing attribute values are treated.";  }  /**   * Gets the method to use for handling missing values. Will be one of   * M_NORMAL, M_AVERAGE, M_MAXDIFF or M_DELETE.   *   * @return the method used for handling missing values.   */  public SelectedTag getMissingMode() {    return new SelectedTag(m_MissingMode, TAGS_MISSING);  }    /**   * Sets the method to use for handling missing values. Values other than   * M_NORMAL, M_AVERAGE, M_MAXDIFF and M_DELETE will be ignored.   *   * @param newMode the method to use for handling missing values.   */  public void setMissingMode(SelectedTag newMode) {        if (newMode.getTags() == TAGS_MISSING) {      m_MissingMode = newMode.getSelectedTag().getID();    }  }  /**   * Returns an enumeration describing the available options.   *   * @return an enumeration of all the available options.   */  public Enumeration listOptions() {    Vector optVector = new Vector( 3 );    optVector.addElement(new Option(	      "\tManual blend setting (default 20%)\n",	      "B", 1, "-B <num>"));    optVector.addElement(new Option(	      "\tEnable entropic auto-blend setting (symbolic class only)\n",	      "E", 0, "-E"));    optVector.addElement(new Option(	      "\tSpecify the missing value treatment mode (default a)\n"	      +"\tValid options are: a(verage), d(elete), m(axdiff), n(ormal)\n",	      "M", 1,"-M <char>"));    return optVector.elements();  }     /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String globalBlendTipText() {    return "The parameter for global blending. Values are restricted to [0,100].";  }  /**   * Set the global blend parameter   * @param b the value for global blending   */  public void setGlobalBlend(int b) {     m_GlobalBlend = b;      if ( m_GlobalBlend > 100 ) {	m_GlobalBlend = 100;      }      if ( m_GlobalBlend < 0 ) {	m_GlobalBlend = 0;      }  }  /**   * Get the value of the global blend parameter   * @return the value of the global blend parameter   */  public int getGlobalBlend() {    return m_GlobalBlend;  }     /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String entropicAutoBlendTipText() {    return "Whether entropy-based blending is to be used.";  }  /**   * Set whether entropic blending is to be used.   * @param e true if entropic blending is to be used   */  public void setEntropicAutoBlend(boolean e) {    if (e) {      m_BlendMethod = B_ENTROPY;    } else {      m_BlendMethod = B_SPHERE;    }  }  /**   * Get whether entropic blending being used   * @return true if entropic blending is used   */  public boolean getEntropicAutoBlend() {    if (m_BlendMethod == B_ENTROPY) {      return true;    }    return false;  }  /**   * Parses a given list of options. <p/>   *   <!-- options-start -->   * Valid options are: <p/>   *    * <pre> -B &lt;num&gt;   *  Manual blend setting (default 20%)   * </pre>   *    * <pre> -E   *  Enable entropic auto-blend setting (symbolic class only)   * </pre>   *    * <pre> -M &lt;char&gt;   *  Specify the missing value treatment mode (default a)   *  Valid options are: a(verage), d(elete), m(axdiff), n(ormal)   * </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 debug = "(KStar.setOptions)";    String blendStr = Utils.getOption('B', options);    if (blendStr.length() != 0) {      setGlobalBlend(Integer.parseInt(blendStr));    }    setEntropicAutoBlend(Utils.getFlag('E', options));        String missingModeStr = Utils.getOption('M', options);    if (missingModeStr.length() != 0) {      switch ( missingModeStr.charAt(0) ) {      case 'a':	setMissingMode(new SelectedTag(M_AVERAGE, TAGS_MISSING));	break;      case 'd':	setMissingMode(new SelectedTag(M_DELETE, TAGS_MISSING));	break;      case 'm':	setMissingMode(new SelectedTag(M_MAXDIFF, TAGS_MISSING));	break;      case 'n':	setMissingMode(new SelectedTag(M_NORMAL, TAGS_MISSING));	break;      default:	setMissingMode(new SelectedTag(M_AVERAGE, TAGS_MISSING));      }    }    Utils.checkForRemainingOptions(options);  }  /**   * Gets the current settings of K*.   *   * @return an array of strings suitable for passing to setOptions()   */  public String [] getOptions() {    // -B <num> -E -M <char>    String [] options = new String [ 5 ];    int itr = 0;    options[itr++] = "-B";    options[itr++] = "" + m_GlobalBlend;    if (getEntropicAutoBlend()) {      options[itr++] = "-E";    }    options[itr++] = "-M";    if (m_MissingMode == M_AVERAGE) {      options[itr++] = "" + "a";    }    else if (m_MissingMode == M_DELETE) {      options[itr++] = "" + "d";    }    else if (m_MissingMode == M_MAXDIFF) {      options[itr++] = "" + "m";    }    else if (m_MissingMode == M_NORMAL) {      options[itr++] = "" + "n";    }    while (itr < options.length) {      options[itr++] = "";    }    return options;  }  /**   * Returns a description of this classifier.   *   * @return a description of this classifier as a string.   */  public String toString() {    StringBuffer st = new StringBuffer();    st.append("KStar Beta Verion (0.1b).\n"	      +"Copyright (c) 1995-97 by Len Trigg (trigg@cs.waikato.ac.nz).\n"	      +"Java port to Weka by Abdelaziz Mahoui "	      +"(am14@cs.waikato.ac.nz).\n\nKStar options : ");    String [] ops = getOptions();    for (int i=0;i<ops.length;i++) {      st.append(ops[i]+' ');    }    return st.toString();  }    /**   * Main method for testing this class.   *   * @param argv should contain command line options (see setOptions)   */  public static void main(String [] argv) {    runClassifier(new KStar(), argv);  }  /**   * Initializes the m_Attributes of the class.   */  private void init_m_Attributes() {    try {      m_NumInstances = m_Train.numInstances();      m_NumClasses = m_Train.numClasses();      m_NumAttributes = m_Train.numAttributes();      m_ClassType = m_Train.classAttribute().type();      m_InitFlag = ON;    } catch(Exception e) {      e.printStackTrace();    }  }  /**   * Updates the m_attributes of the class.   */  private void update_m_Attributes() {    m_NumInstances = m_Train.numInstances();    m_InitFlag = ON;  }  /**   * Note: for Nominal Class Only!   * Generates a set of random versions of the class colomn.   */  private void generateRandomClassColomns() {    String debug = "(KStar.generateRandomClassColomns)";    Random generator = new Random(42);    //    Random generator = new Random();    m_RandClassCols = new int [NUM_RAND_COLS+1][];    int [] classvals = classValues();    for (int i=0; i < NUM_RAND_COLS; i++) {      // generate a randomized version of the class colomn      m_RandClassCols[i] = randomize(classvals, generator);    }    // original colomn is preserved in colomn NUM_RAND_COLS    m_RandClassCols[NUM_RAND_COLS] = classvals;  }  /**   * Note: for Nominal Class Only!   * Returns an array of the class values   *   * @return an array of class values   */  private int [] classValues() {    String debug = "(KStar.classValues)";    int [] classval = new int[m_NumInstances];    for (int i=0; i < m_NumInstances; i++) {      try {	classval[i] = (int)m_Train.instance(i).classValue();      } catch (Exception ex) {	ex.printStackTrace();      }    }    return classval;  }  /**   * Returns a copy of the array with its elements randomly redistributed.   *   * @param array the array to randomize.   * @param generator the random number generator to use   * @return a copy of the array with its elements randomly redistributed.   */  private int [] randomize(int [] array, Random generator) {    String debug = "(KStar.randomize)";    int index;    int temp;    int [] newArray = new int[array.length];    System.arraycopy(array, 0, newArray, 0, array.length);    for (int j = newArray.length - 1; j > 0; j--) {      index = (int) ( generator.nextDouble() * (double)j );      temp = newArray[j];      newArray[j] = newArray[index];      newArray[index] = temp;    }    return newArray;  }} // class end

⌨️ 快捷键说明

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