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

The following examples show how to use weka.core.Utils#kthSmallestValue() . 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: Vote.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Classifies the given test instance, returning the median from all
 * classifiers.
 * 
 * @param instance the instance to be classified
 * @return the predicted most likely class for the instance or
 *         Utils.missingValue() if no prediction is made
 * @throws Exception if an error occurred during the prediction
 */
protected double classifyInstanceMedian(Instance instance) throws Exception {
  double[] results = new double[m_Classifiers.length
      + m_preBuiltClassifiers.size()];
  double result;

  for (int i = 0; i < m_Classifiers.length; i++)
    results[i] = m_Classifiers[i].classifyInstance(instance);

  for (int i = 0; i < m_preBuiltClassifiers.size(); i++) {
    results[i + m_Classifiers.length] = m_preBuiltClassifiers.get(i)
        .classifyInstance(instance);
  }

  if (results.length == 0)
    result = 0;
  else if (results.length == 1)
    result = results[0];
  else
    result = Utils.kthSmallestValue(results, results.length / 2);

  return result;
}
 
Example 2
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;
}