Exemplo n.º 1
0
  /**
   * Classify the input.
   *
   * @param input The input to classify.
   */
  @Override
  public int classify(MLData input) {

    if (this.classificationTarget < 0 || this.classificationTarget >= this.events.size()) {
      throw new BayesianError(
          "Must specify classification target by calling setClassificationTarget.");
    }

    int[] d = this.determineClasses(input);

    // properly tag all of the events
    for (int i = 0; i < this.events.size(); i++) {
      BayesianEvent event = this.events.get(i);
      if (i == this.classificationTarget) {
        this.query.defineEventType(event, EventType.Outcome);
      } else if (this.inputPresent[i]) {
        this.query.defineEventType(event, EventType.Evidence);
        this.query.setEventValue(event, d[i]);
      } else {
        this.query.defineEventType(event, EventType.Hidden);
        this.query.setEventValue(event, d[i]);
      }
    }

    // loop over and try each outcome choice
    BayesianEvent outcomeEvent = this.events.get(this.classificationTarget);
    this.classificationProbabilities = new double[outcomeEvent.getChoices().size()];
    for (int i = 0; i < outcomeEvent.getChoices().size(); i++) {
      this.query.setEventValue(outcomeEvent, i);
      this.query.execute();
      classificationProbabilities[i] = this.query.getProbability();
    }

    return EngineArray.maxIndex(this.classificationProbabilities);
  }
 public int winner(MLData output) {
   return EngineArray.maxIndex(output.getData());
 }