/**
   * Iterates over all models and returns the class with maximum likelihood.
   *
   * @param origExampleSet the set of examples to be classified
   */
  @Override
  public ExampleSet performPrediction(ExampleSet origExampleSet, Attribute predictedLabel)
      throws OperatorException {
    final String attributePrefix = "AdaBoostModelPrediction";
    final int numLabels = predictedLabel.getMapping().size();
    final Attribute[] specialAttributes = new Attribute[numLabels];
    OperatorProgress progress = null;
    if (getShowProgress() && getOperator() != null && getOperator().getProgress() != null) {
      progress = getOperator().getProgress();
      progress.setTotal(100);
    }
    for (int i = 0; i < numLabels; i++) {
      specialAttributes[i] =
          com.rapidminer.example.Tools.createSpecialAttribute(
              origExampleSet, attributePrefix + i, Ontology.NUMERICAL);
      if (progress != null) {
        progress.setCompleted((int) (25.0 * (i + 1) / numLabels));
      }
    }

    Iterator<Example> reader = origExampleSet.iterator();
    int progressCounter = 0;
    while (reader.hasNext()) {
      Example example = reader.next();
      for (int i = 0; i < specialAttributes.length; i++) {
        example.setValue(specialAttributes[i], 0);
      }
      if (progress != null && ++progressCounter % OPERATOR_PROGRESS_STEPS == 0) {
        progress.setCompleted((int) (25.0 * progressCounter / origExampleSet.size()) + 25);
      }
    }

    reader = origExampleSet.iterator();
    for (int modelNr = 0; modelNr < this.getNumberOfModels(); modelNr++) {
      Model model = this.getModel(modelNr);
      ExampleSet exampleSet = (ExampleSet) origExampleSet.clone();
      exampleSet = model.apply(exampleSet);
      this.updateEstimates(exampleSet, modelNr, specialAttributes);
      PredictionModel.removePredictedLabel(exampleSet);
      if (progress != null) {
        progress.setCompleted((int) (25.0 * (modelNr + 1) / this.getNumberOfModels()) + 50);
      }
    }

    // Turn prediction weights into confidences and a crisp predcition:
    this.evaluateSpecialAttributes(origExampleSet, specialAttributes);

    // Clean up attributes:
    for (int i = 0; i < numLabels; i++) {
      origExampleSet.getAttributes().remove(specialAttributes[i]);
      origExampleSet.getExampleTable().removeAttribute(specialAttributes[i]);
      if (progress != null) {
        progress.setCompleted((int) (25.0 * (i + 1) / numLabels) + 75);
      }
    }

    return origExampleSet;
  }
  public IOObject[] apply() throws OperatorException {
    ExampleSet exampleSet = getInput(ExampleSet.class);
    if (exampleSet.getAttributes().getLabel() == null) {
      throw new UserError(this, 105);
    }
    if (!exampleSet.getAttributes().getLabel().isNominal()) {
      throw new UserError(this, 101, "ROC Charts", exampleSet.getAttributes().getLabel());
    }
    if (exampleSet.getAttributes().getLabel().getMapping().getValues().size() != 2) {
      throw new UserError(this, 114, "ROC Charts", exampleSet.getAttributes().getLabel());
    }

    if (exampleSet.getAttributes().getPredictedLabel() != null
        && getParameterAsBoolean(PARAMETER_USE_MODEL)) {
      logWarning("Input example already has a predicted label which will be removed.");
      PredictionModel.removePredictedLabel(exampleSet);
    }
    if (exampleSet.getAttributes().getPredictedLabel() == null
        && !getParameterAsBoolean(PARAMETER_USE_MODEL)) {
      throw new UserError(this, 107);
    }
    Model model = null;
    if (getParameterAsBoolean(PARAMETER_USE_MODEL)) {
      model = getInput(Model.class);
      exampleSet = model.apply(exampleSet);
    }
    if (exampleSet.getAttributes().getPredictedLabel() == null) {
      throw new UserError(this, 107);
    }

    ROCDataGenerator rocDataGenerator = new ROCDataGenerator(1.0d, 1.0d);
    ROCData rocPoints =
        rocDataGenerator.createROCData(
            exampleSet, getParameterAsBoolean(PARAMETER_USE_EXAMPLE_WEIGHTS));
    rocDataGenerator.createROCPlotDialog(rocPoints);

    PredictionModel.removePredictedLabel(exampleSet);
    if (getParameterAsBoolean(PARAMETER_USE_MODEL)) {
      return new IOObject[] {exampleSet, model};
    } else return new IOObject[] {exampleSet};
  }
 /**
  * Setting the parameter <code>MAX_MODEL_NUMBER</code> allows to discard all but the first n
  * models for specified n.
  */
 @Override
 public void setParameter(String name, Object value) throws OperatorException {
   if (name.equalsIgnoreCase(MAX_MODEL_NUMBER)) {
     String stringValue = (String) value;
     try {
       this.maxModelNumber = Integer.parseInt(stringValue);
       return;
     } catch (NumberFormatException e) {
     }
   } else {
     super.setParameter(name, value);
   }
 }
Esempio n. 4
0
  /**
   * Applies the applier and evaluator (= second encapsulated inner operator). In order to reuse
   * possibly created predicted label attributes, we do the following: We compare the predicted
   * label of <code>testSet</code> before and after applying the inner operator. If it changed, the
   * predicted label is removed again. No outer operator could ever see it. The same applies for the
   * confidence attributes in case of classification learning.
   */
  public IOContainer evaluate(ExampleSet testSet, IOContainer learnResult)
      throws OperatorException {
    if (learnResult == null) {
      throw new RuntimeException(
          "Wrong use of ValidationChain.evaluate(ExampleSet): "
              + "No preceding invocation of learn(ExampleSet)!");
    }
    Attribute predictedBefore = testSet.getAttributes().getPredictedLabel();
    IOContainer evalInput = learnResult.append(new IOObject[] {testSet});
    IOContainer result = getEvaluator().apply(evalInput);
    Attribute predictedAfter = testSet.getAttributes().getPredictedLabel();

    // remove predicted label and confidence attributes if there is a new prediction which is not
    // equal to an old one
    if ((predictedAfter != null)
        && ((predictedBefore == null)
            || (predictedBefore.getTableIndex() != predictedAfter.getTableIndex()))) {
      PredictionModel.removePredictedLabel(testSet);
    }
    return result;
  }