Java Code Examples for weka.core.Utils#sm()

The following examples show how to use weka.core.Utils#sm() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassifierDecList.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
  * Choose last index (ie. choose rule).
  */
 public final int chooseLastIndex() {
   
   int minIndex = 0;
   double estimated, min = Double.MAX_VALUE;
   
   if (!m_isLeaf) 
     for (int i = 0; i < m_sons.length; i++)
if (son(i) != null) {
  if (Utils.grOrEq(localModel().distribution().perBag(i),
		   (double)m_minNumObj)) {
    estimated = son(i).getSizeOfBranch();
    if (Utils.sm(estimated,min)) {
      min = estimated;
      minIndex = i;
    }
  }
}

   return minIndex;
 }
 
Example 2
Source File: MISMO.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Quick and dirty check whether the quadratic programming problem is solved.
 * 
 * @throws Exception if something goes wrong
 */
protected void checkClassifier() throws Exception {

  double sum = 0;
  for (int i = 0; i < m_alpha.length; i++) {
    if (m_alpha[i] > 0) {
      sum += m_class[i] * m_alpha[i];
    }
  }
  System.err.println("Sum of y(i) * alpha(i): " + sum);

  for (int i = 0; i < m_alpha.length; i++) {
    double output = SVMOutput(i, m_data.instance(i));
    if (Utils.eq(m_alpha[i], 0)) {
      if (Utils.sm(m_class[i] * output, 1)) {
        System.err.println("KKT condition 1 violated: " + m_class[i] * output);
      }
    } 
    if (Utils.gr(m_alpha[i], 0) && 
        Utils.sm(m_alpha[i], m_C * m_data.instance(i).weight())) {
      if (!Utils.eq(m_class[i] * output, 1)) {
        System.err.println("KKT condition 2 violated: " + m_class[i] * output);
      }
        } 
    if (Utils.eq(m_alpha[i], m_C * m_data.instance(i).weight())) {
      if (Utils.gr(m_class[i] * output, 1)) {
        System.err.println("KKT condition 3 violated: " + m_class[i] * output);
      }
    } 
  }
}
 
Example 3
Source File: ClassifierDecList.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Method for choosing a subset to expand.
  */
 public final int chooseIndex() {
   
   int minIndex = -1;
   double estimated, min = Double.MAX_VALUE;
   int i, j;

   for (i = 0; i < m_sons.length; i++)
     if (son(i) == null) {
if (Utils.sm(localModel().distribution().perBag(i),
	     (double)m_minNumObj))
  estimated = Double.MAX_VALUE;
else{
  estimated = 0;
  for (j = 0; j < localModel().distribution().numClasses(); j++) 
    estimated -= m_splitCrit.logFunc(localModel().distribution().
			     perClassPerBag(i,j));
  estimated += m_splitCrit.logFunc(localModel().distribution().
			   perBag(i));
  estimated /= localModel().distribution().perBag(i);
}
if (Utils.smOrEq(estimated,0))
  return i;
if (Utils.sm(estimated,min)) {
  min = estimated;
  minIndex = i;
}
     }

   return minIndex;
 }
 
Example 4
Source File: SMO.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
    * Quick and dirty check whether the quadratic programming problem is solved.
    * 
    * @throws Exception if checking fails
    */
   protected void checkClassifier() throws Exception {

     double sum = 0;
     for (int i = 0; i < m_alpha.length; i++) {
if (m_alpha[i] > 0) {
  sum += m_class[i] * m_alpha[i];
}
     }
     System.err.println("Sum of y(i) * alpha(i): " + sum);

     for (int i = 0; i < m_alpha.length; i++) {
double output = SVMOutput(i, m_data.instance(i));
if (Utils.eq(m_alpha[i], 0)) {
  if (Utils.sm(m_class[i] * output, 1)) {
    System.err.println("KKT condition 1 violated: " + m_class[i] * output);
  }
} 
if (Utils.gr(m_alpha[i], 0) && 
    Utils.sm(m_alpha[i], m_C * m_data.instance(i).weight())) {
  if (!Utils.eq(m_class[i] * output, 1)) {
    System.err.println("KKT condition 2 violated: " + m_class[i] * output);
  }
} 
if (Utils.eq(m_alpha[i], m_C * m_data.instance(i).weight())) {
  if (Utils.gr(m_class[i] * output, 1)) {
    System.err.println("KKT condition 3 violated: " + m_class[i] * output);
  }
} 
     }
   }
 
Example 5
Source File: CollectiveTree.java    From collective-classification-weka-package with GNU General Public License v3.0 4 votes vote down vote up
/**
 * determines the distribution of the instances with a non-missing value
 * at the given attribute position.
 * @param data        the instances to work on
 * @param indices     the sorted indices
 * @param att         the attribute to determine the distribution for
 * @return            the distribution
 */
protected double[] determineAttributeDistribution( Instances data,
                                                   int[] indices,
                                                   int att) {
  double[]      result;
  int           i;
  Instance      inst;
  int           count;
  double[]      values;
  double        median;

  // nominal attribute
  if (data.attribute(att).isNominal()) {
    result = new double[data.attribute(att).numValues()];

    // determine attribute distribution (necessary to distribute instances
    // with no class and missing attribute)
    for (i = 0; i < indices.length; i++) {
      inst = data.instance(indices[i]);
      if (inst.isMissing(att))
        break;
      result[(int) inst.value(att)] += inst.weight();
    }
  }
  // numeric attribute
  else {
    result = new double[2];   // less or greater/equal than median

    // determine number of instances w/o missing attribute
    count = 0;
    for (i = 0; i < indices.length; i++) {
      inst = data.instance(indices[i]);
      if (inst.isMissing(att))
        break;
      count++;
    }

    // determine median
    values = new double[count];
    for (i = 0; i < count; i++) {
      inst      = data.instance(indices[i]);
      values[i] = inst.value(att);
    }
    if (values.length == 0)
      median = 0;
    else if (values.length == 1)
      median = values[0];
    else
      median = Utils.kthSmallestValue(values, values.length / 2);

    // disitribute
    for (i = 0; i < count; i++) {
      inst = data.instance(indices[i]);
      if (Utils.sm(inst.value(att), median))
        result[0] += inst.weight();
      else
        result[1] += inst.weight();
    }
  }

  if (Utils.gr(Utils.sum(result), 0))
    Utils.normalize(result);

  return result;
}
 
Example 6
Source File: DecisionTreeNode.java    From collective-classification-weka-package with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Computes class distribution of an instance using the decision tree.
  * 
  * @param instance	the instance to compute the distribution for
  * @return		the class distribution
  * @throws Exception	if something goes wrong
  */
 public double[] distributionForInstance(Instance instance) throws Exception {
   
   double[] returnedDist = null;
   
   if (getAttribute() > -1) {
     // Node is not a leaf
     if (instance.isMissing(getAttribute())) {
       if (getDebugLevel() > 0)
         System.out.println(toStringNode());

// Value is missing
returnedDist = new double[getInformation().numClasses()];
       
// Split instance up
for (int i = 0; i < getChildCount(); i++) {
  double[] help = getNodeAt(i).distributionForInstance(instance);
         if (getDebugLevel() > 1)
           System.out.println("help: " + Utils.arrayToString(help));
  if (help != null) {
    for (int j = 0; j < help.length; j++) {
      returnedDist[j] += m_Prop[i] * help[j];
    }
  }
}
       if (getDebugLevel() > 1)
         System.out.println(   "--> returnedDist: " 
                             + Utils.arrayToString(returnedDist));
     } 
     else if (getInformation().attribute(getAttribute()).isNominal()) {
// For nominal attributes
       int branch = 0;

       // branch for each nominal value?
       if (getNominalSplit() == null) {
         branch = (int) instance.value(getAttribute());
       }
       else {
         // determine the branch we have to go down
         for (int i = 0; i < getNominalSplit().length; i++) {
           for (int n = 0; n < getNominalSplit()[i].length; n++) {
             if (Utils.eq(instance.value(getAttribute()), 
                          getNominalSplit()[i][n])) {
               branch = i;
               break;
             }
           }
         }
       }

       returnedDist = getNodeAt(branch).distributionForInstance(instance);
     } 
     else {
// For numeric attributes
if (Utils.sm(instance.value(getAttribute()), getSplitPoint())) {
  returnedDist = getNodeAt(0).distributionForInstance(instance);
} 
       else {
  returnedDist = getNodeAt(1).distributionForInstance(instance);
}
     }
   }

   if ((getAttribute() == -1) || (returnedDist == null)) {
     // Node is a leaf or successor is empty
     return getClassProbabilities();
   } 
   else {
     return returnedDist;
   }
 }