graftsplit.java

来自「Weka」· Java 代码 · 共 541 行 · 第 1/2 页

JAVA
541
字号
   */  public int subsetOfInterest() {    if(m_testType == 2)       return 0;    if(m_testType == 3)       return 1;    return m_testType;  }  /**   * @return the number of positive cases in the subset of interest   */  public double positivesForSubsetOfInterest() {    return (m_distribution.perClassPerBag(subsetOfInterest(), m_maxClass));  }  /**   * @param subset the subset to get the positives for   * @return the number of positive cases in the specified subset   */  public double positives(int subset) {    return (m_distribution.perClassPerBag(subset,                                     m_distribution.maxClass(subset)));  }  /**   * @return the number of instances in the subset of interest   */  public double totalForSubsetOfInterest() {    return (m_distribution.perBag(subsetOfInterest()));  }    /**   * @param subset the index of the bag to get the total for   * @return the number of instances in the subset   */  public double totalForSubset(int subset) {    return (m_distribution.perBag(subset));  }  /**   * Prints left side of condition satisfied by instances.   *   * @param data the data.   */  public String leftSide(Instances data) {    return data.attribute(m_attIndex).name();  }  /**   * @return the index of the attribute to split on   */   public int attribute() {    return m_attIndex;  }  /**   * Prints condition satisfied by instances in subset index.   */  public final String rightSide(int index, Instances data) {    StringBuffer text;    text = new StringBuffer();    if(data.attribute(m_attIndex).isNominal())       if(index == 0)          text.append(" = "+                      data.attribute(m_attIndex).value((int)m_splitPoint));       else          text.append(" != "+                      data.attribute(m_attIndex).value((int)m_splitPoint));    else       if(index == 0)          text.append(" <= "+                      Utils.doubleToString(m_splitPoint,6));       else          text.append(" > "+                      Utils.doubleToString(m_splitPoint,6));    return text.toString();  }  /**   * Returns a string containing java source code equivalent to the test   * made at this node. The instance being tested is called "i".   *   * @param index index of the nominal value tested   * @param data the data containing instance structure info   * @return a value of type 'String'   */  public final String sourceExpression(int index, Instances data) {    StringBuffer expr = null;    if(index < 0) {       return "i[" + m_attIndex + "] == null";    }    if(data.attribute(m_attIndex).isNominal()) {       if(index == 0)          expr = new StringBuffer("i[");       else          expr = new StringBuffer("!i[");       expr.append(m_attIndex).append("]");       expr.append(".equals(\"").append(data.attribute(m_attIndex)                                      .value((int)m_splitPoint)).append("\")");    } else {       expr = new StringBuffer("((Double) i[");       expr.append(m_attIndex).append("])");       if(index == 0) {          expr.append(".doubleValue() <= ").append(m_splitPoint);       } else {          expr.append(".doubleValue() > ").append(m_splitPoint);       }    }    return expr.toString();  }  /**   * @param instance the instance to produce the weights for   * @return a double array of weights, null if only belongs to one subset   */  public double [] weights(Instance instance) {    double [] weights;    int i;    if(instance.isMissing(m_attIndex)) {       weights = new double [m_numSubsets];       for(i=0;i<m_numSubsets;i++) {          weights [i] = m_graftdistro.perBag(i)/m_graftdistro.total();       }       return weights;    } else {       return null;    }  }  /**   * @param instance the instance for which to determine the subset   * @return an int indicating the subset this instance belongs to   */  public int whichSubset(Instance instance) {    if(instance.isMissing(m_attIndex))       return -1;    if(instance.attribute(m_attIndex).isNominal()) {       // in the case of nominal, m_splitPoint is the = value, all else is !=       if(instance.value(m_attIndex) == m_splitPoint)          return 0;       else          return 1;    } else {       if(Utils.smOrEq(instance.value(m_attIndex), m_splitPoint))          return 0;       else          return 1;    }  }  /**   * @return the value of the split point   */  public double splitPoint() {    return m_splitPoint;  }  /**   * @return the dominate class for the subset of interest   */  public int maxClassForSubsetOfInterest() {    return m_maxClass;  }  /**   * @return the laplace value for maxClass of subset of interest   */  public double laplaceForSubsetOfInterest() {    return m_laplace;  }  /**   * returns the test type   * @return value of testtype   */  public int testType() {    return m_testType;  }  /**   * method needed for sorting a collection of GraftSplits by laplace value   * @param g the graft split to compare to this one   * @return -1, 0, or 1 if this GraftSplit laplace is <, = or > than that of g   */  public int compareTo(Object g) {    if(m_laplace > ((GraftSplit)g).laplaceForSubsetOfInterest())       return 1;    if(m_laplace < ((GraftSplit)g).laplaceForSubsetOfInterest())       return -1;    return 0;  }  /**   * returns the probability for instance for the specified class   * @param classIndex the index of the class   * @param instance the instance to get the probability for   * @param theSubset the subset   */  public final double classProb(int classIndex, Instance instance, 		            int theSubset) throws Exception {    if (theSubset <= -1) {       double [] weights = weights(instance);       if (weights == null) {          return m_distribution.prob(classIndex);       } else {          double prob = 0;          for (int i = 0; i < weights.length; i++) {             prob += weights[i] * m_distribution.prob(classIndex, i);          }          return prob;       }    } else {       if (Utils.gr(m_distribution.perBag(theSubset), 0)) {          return m_distribution.prob(classIndex, theSubset);       } else {          return m_distribution.prob(classIndex);       }    }  }  /**   * method for returning information about this GraftSplit   * @param data instances for determining names of attributes and values   * @return a string showing this GraftSplit's information   */  public String toString(Instances data) {    String theTest;    if(m_testType == 0)       theTest = " <= ";    else if(m_testType == 1)       theTest = " > ";    else if(m_testType == 2)       theTest = " = ";    else       theTest = " != ";    if(data.attribute(m_attIndex).isNominal())       theTest += data.attribute(m_attIndex).value((int)m_splitPoint);    else       theTest += Double.toString(m_splitPoint);    return data.attribute(m_attIndex).name() + theTest           + " (" + Double.toString(m_laplace) + ") --> "            + data.attribute(data.classIndex()).value(m_maxClass);  }}

⌨️ 快捷键说明

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