@Override
 public double predict(Example example) throws OperatorException {
   int i = 0;
   double distance = intercept;
   // using kernel for distance calculation
   double[] values = new double[example.getAttributes().size()];
   for (Attribute currentAttribute : example.getAttributes()) {
     values[i] = example.getValue(currentAttribute);
     i++;
   }
   distance += kernel.calculateDistance(values, coefficients);
   if (getLabel().isNominal()) {
     int positiveMapping = getLabel().getMapping().mapString(classPositive);
     int negativeMapping = getLabel().getMapping().mapString(classNegative);
     boolean isApplying = example.getAttributes().getPredictedLabel() != null;
     if (isApplying) {
       example.setConfidence(classPositive, 1.0d / (1.0d + java.lang.Math.exp(-distance)));
       example.setConfidence(classNegative, 1.0d / (1.0d + java.lang.Math.exp(distance)));
     }
     if (distance < 0) {
       return negativeMapping;
     } else {
       return positiveMapping;
     }
   } else {
     return distance;
   }
 }
  private void evaluateSpecialAttributes(ExampleSet exampleSet, Attribute[] specialAttributes) {
    Attribute predictedLabel = exampleSet.getAttributes().getPredictedLabel();
    Iterator<Example> reader = exampleSet.iterator();
    while (reader.hasNext()) {
      Example example = reader.next();
      double sum = 0;
      double[] confidences = new double[specialAttributes.length];
      double bestConf = -1;
      int bestLabel = 0;
      for (int n = 0; n < confidences.length; n++) {
        confidences[n] = example.getValue(specialAttributes[n]);
        if (confidences[n] > bestConf) {
          bestConf = confidences[n];
          bestLabel = n;
        }
      }

      example.setValue(
          predictedLabel,
          predictedLabel.getMapping().mapString(this.getLabel().getMapping().mapIndex(bestLabel)));

      for (int n = 0; n < confidences.length; n++) {
        confidences[n] = Math.exp(confidences[n] - bestConf);
        // remember for normalization:
        sum += confidences[n];
      }

      // Normalize and set confidence values for all classes:
      if (Double.isInfinite(sum) || Double.isNaN(sum)) {
        int best = (int) example.getPredictedLabel();
        for (int k = 0; k < confidences.length; k++) {
          confidences[k] = 0;
        }
        confidences[best] = 1;
      } else {
        for (int k = 0; k < confidences.length; k++) {
          confidences[k] /= sum;
          example.setConfidence(predictedLabel.getMapping().mapIndex(k), confidences[k]);
        }
      }
    }
  }
  private double predictAsLeaf(Example example, ExtendedTree node) {
    double dPrediction = 0;
    Iterator<String> s = node.getCounterMap().keySet().iterator();
    int[] counts = new int[getLabel().getMapping().size()];
    int sum = 0;
    while (s.hasNext()) {
      String className = s.next();
      int count = node.getCount(className);
      int index = getLabel().getMapping().getIndex(className);
      counts[index] = count;
      sum += count;
    }
    for (int i = 0; i < counts.length; i++) {
      example.setConfidence(
          getLabel().getMapping().mapIndex(i), smoothedConfidence(counts[i], sum));
    }

    dPrediction = getLabel().getMapping().getIndex(node.getLabel());

    return dPrediction;
  }
Esempio n. 4
0
  @Override
  public ExampleSet performPrediction(ExampleSet exampleSet, Attribute predictedLabel)
      throws OperatorException {
    Attribute label = this.getLabel();
    final int posLabel = label.getMapping().getPositiveIndex();
    final int negLabel = label.getMapping().getNegativeIndex();
    final String posLabelS = label.getMapping().mapIndex(posLabel);
    final String negLabelS = label.getMapping().mapIndex(negLabel);
    exampleSet = model.apply(exampleSet);
    Iterator<Example> reader = exampleSet.iterator();
    while (reader.hasNext()) {
      Example example = reader.next();
      double predicted = PlattScaling.getLogOddsPosConfidence(example.getConfidence(posLabelS));
      double scaledPos =
          1.0d / (1.0d + Math.exp(predicted * parameters.getA() + parameters.getB()));
      double scaledNeg = 1.0d - scaledPos;

      example.setValue(predictedLabel, (scaledPos >= scaledNeg) ? posLabel : negLabel);
      example.setConfidence(posLabelS, scaledPos);
      example.setConfidence(negLabelS, scaledNeg);
    }
    return exampleSet;
  }