/**
   * Computes average class values for each attribute and value
   *
   * @throws Exception
   */
  private void computeAverageClassValues() throws Exception {

    double totalCounts, sum;
    M5Instance instance;
    double[] counts;

    double[][] avgClassValues = new double[getInputFormat().numAttributes()][0];
    m_Indices = new int[getInputFormat().numAttributes()][0];
    for (int j = 0; j < getInputFormat().numAttributes(); j++) {
      M5Attribute att = getInputFormat().attribute(j);
      if (att.isNominal()) {
        avgClassValues[j] = new double[att.numValues()];
        counts = new double[att.numValues()];
        for (int i = 0; i < getInputFormat().numInstances(); i++) {
          instance = getInputFormat().instance(i);
          if (!instance.classIsMissing() && (!instance.isMissing(j))) {
            counts[(int) instance.value(j)] += instance.weight();
            avgClassValues[j][(int) instance.value(j)] += instance.weight() * instance.classValue();
          }
        }
        sum = M5StaticUtils.sum(avgClassValues[j]);
        totalCounts = M5StaticUtils.sum(counts);
        if (M5StaticUtils.gr(totalCounts, 0)) {
          for (int k = 0; k < att.numValues(); k++) {
            if (M5StaticUtils.gr(counts[k], 0)) {
              avgClassValues[j][k] /= (double) counts[k];
            } else {
              avgClassValues[j][k] = sum / (double) totalCounts;
            }
          }
        }
        m_Indices[j] = M5StaticUtils.sort(avgClassValues[j]);
      }
    }
  }
Beispiel #2
0
  /**
   * Main method for testing this class.
   *
   * @param ops some dummy options
   */
  public static void main(String[] ops) {

    double[] doubles = {4.5, 6.7, Double.NaN, 3.4, 4.8, 1.2, 3.4};
    int[] ints = {12, 6, 2, 18, 16, 6, 7, 5};

    try {

      // Option handling
      System.out.println("First option split up:");
      if (ops.length > 0) {
        String[] firstOptionSplitUp = M5StaticUtils.splitOptions(ops[0]);
        for (int i = 0; i < firstOptionSplitUp.length; i++) {
          System.out.println(firstOptionSplitUp[i]);
        }
      }
      System.out.println("Partitioned options: ");
      String[] partitionedOptions = M5StaticUtils.partitionOptions(ops);
      for (int i = 0; i < partitionedOptions.length; i++) {
        System.out.println(partitionedOptions[i]);
      }
      System.out.println("Get flag -f: " + M5StaticUtils.getFlag('f', ops));
      System.out.println("Get option -o: " + M5StaticUtils.getOption('o', ops));
      System.out.println("Checking for remaining options... ");
      M5StaticUtils.checkForRemainingOptions(ops);

      // Statistics
      System.out.println("Original array (doubles): ");
      for (int i = 0; i < doubles.length; i++) {
        System.out.print(doubles[i] + " ");
      }
      System.out.println();
      System.out.println("Original array (ints): ");
      for (int i = 0; i < ints.length; i++) {
        System.out.print(ints[i] + " ");
      }
      System.out.println();
      System.out.println(
          "Correlation: " + M5StaticUtils.correlation(doubles, doubles, doubles.length));
      System.out.println("Mean: " + M5StaticUtils.mean(doubles));
      System.out.println("Variance: " + M5StaticUtils.variance(doubles));
      System.out.println("Sum (doubles): " + M5StaticUtils.sum(doubles));
      System.out.println("Sum (ints): " + M5StaticUtils.sum(ints));
      System.out.println("Max index (doubles): " + M5StaticUtils.maxIndex(doubles));
      System.out.println("Max index (ints): " + M5StaticUtils.maxIndex(ints));
      System.out.println("Min index (doubles): " + M5StaticUtils.minIndex(doubles));
      System.out.println("Min index (ints): " + M5StaticUtils.minIndex(ints));

      // Sorting and normalizing
      System.out.println("Sorted array (doubles): ");
      int[] sorted = M5StaticUtils.sort(doubles);
      for (int i = 0; i < doubles.length; i++) {
        System.out.print(doubles[sorted[i]] + " ");
      }
      System.out.println();
      System.out.println("Normalized array (doubles): ");
      M5StaticUtils.normalize(doubles);
      for (int i = 0; i < doubles.length; i++) {
        System.out.print(doubles[i] + " ");
      }
      System.out.println();
      System.out.println("Normalized again (doubles): ");
      M5StaticUtils.normalize(doubles, M5StaticUtils.sum(doubles));
      for (int i = 0; i < doubles.length; i++) {
        System.out.print(doubles[i] + " ");
      }
      System.out.println();

      // Pretty-printing
      System.out.println("-4.58: " + M5StaticUtils.doubleToString(-4.57826535, 2));
      System.out.println("-6.78: " + M5StaticUtils.doubleToString(-6.78214234, 6, 2));

      // Comparisons
      System.out.println("5.70001 == 5.7 ? " + M5StaticUtils.eq(5.70001, 5.7));
      System.out.println("5.70001 > 5.7 ? " + M5StaticUtils.gr(5.70001, 5.7));
      System.out.println("5.70001 >= 5.7 ? " + M5StaticUtils.grOrEq(5.70001, 5.7));
      System.out.println("5.7 < 5.70001 ? " + M5StaticUtils.sm(5.7, 5.70001));
      System.out.println("5.7 <= 5.70001 ? " + M5StaticUtils.smOrEq(5.7, 5.70001));

      // Math
      System.out.println("Info (ints): " + M5StaticUtils.info(ints));
      System.out.println("log2(4.6): " + M5StaticUtils.log2(4.6));
      System.out.println("5 * log(5): " + M5StaticUtils.xlogx(5));
      System.out.println("5.5 rounded: " + M5StaticUtils.round(5.5));
      System.out.println(
          "5.55555 rounded to 2 decimal places: " + M5StaticUtils.roundDouble(5.55555, 2));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }