示例#1
0
  private static ProbabilityDistribution getPriorProbabilities(Target target) {
    ProbabilityDistribution result = new ProbabilityDistribution();

    List<TargetValue> values = target.getTargetValues();
    for (TargetValue value : values) {

      // "The defaultValue attribute is used only if the optype of the field is continuous"
      if (value.getDefaultValue() != null) {
        throw new InvalidFeatureException(value);
      }

      String targetCategory = value.getValue();
      Double probability = value.getPriorProbability();

      if (targetCategory == null || probability == null) {
        continue;
      }

      result.put(targetCategory, probability);
    }

    if (result.isEmpty()) {
      return null;
    }

    return result;
  }
示例#2
0
  public static TargetValue getTargetValue(Target target, Object value) {
    DataType dataType = TypeUtil.getDataType(value);

    List<TargetValue> targetValues = target.getTargetValues();
    for (TargetValue targetValue : targetValues) {

      if (TypeUtil.equals(
          dataType, value, TypeUtil.parseOrCast(dataType, targetValue.getValue()))) {
        return targetValue;
      }
    }

    return null;
  }
示例#3
0
  private static Object getPredictedDisplayValue(
      Object object, DataField dataField, Target target) {

    if (object instanceof HasDisplayValue) {
      HasDisplayValue hasDisplayValue = TypeUtil.cast(HasDisplayValue.class, object);

      return hasDisplayValue.getDisplayValue();
    }

    object = getPredictedValue(object);

    if (target != null) {
      TargetValue targetValue = TargetUtil.getTargetValue(target, object);

      if (targetValue != null) {
        String displayValue = targetValue.getDisplayValue();

        if (displayValue != null) {
          return displayValue;
        }
      }
    }

    OpType opType = dataField.getOpType();
    switch (opType) {
      case CONTINUOUS:
        break;
      case CATEGORICAL:
      case ORDINAL:
        {
          Value value = FieldValueUtil.getValidValue(dataField, object);

          if (value != null) {
            String displayValue = value.getDisplayValue();

            if (displayValue != null) {
              return displayValue;
            }
          }
        }
        break;
      default:
        throw new UnsupportedFeatureException(dataField, opType);
    }

    // "If the display value is not specified explicitly, then the raw predicted value is used by
    // default"
    return object;
  }
示例#4
0
  private static Double getDefaultValue(Target target) {
    List<TargetValue> values = target.getTargetValues();

    if (values.isEmpty()) {
      return null;
    } // End if

    if (values.size() != 1) {
      throw new InvalidFeatureException(target);
    }

    TargetValue value = values.get(0);

    // "The value and priorProbability attributes are used only if the optype of the field is
    // categorical or ordinal"
    if (value.getValue() != null || value.getPriorProbability() != null) {
      throw new InvalidFeatureException(value);
    }

    return value.getDefaultValue();
  }