org.dmg.pmml.ResultFeature Java Examples

The following examples show how to use org.dmg.pmml.ResultFeature. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Map<String, Set<ResultFeature>> load(Output output){
	Map<String, Set<ResultFeature>> result = new LinkedHashMap<>();

	List<org.dmg.pmml.OutputField> pmmlOutputFields = output.getOutputFields();
	for(org.dmg.pmml.OutputField pmmlOutputField : pmmlOutputFields){
		String segmentId = pmmlOutputField.getSegmentId();

		if(segmentId == null){
			continue;
		}

		Set<ResultFeature> resultFeatures = result.get(segmentId);
		if(resultFeatures == null){
			resultFeatures = EnumSet.noneOf(ResultFeature.class);

			result.put(segmentId, resultFeatures);
		}

		resultFeatures.add(pmmlOutputField.getResultFeature());
	}

	result.replaceAll((key, value) -> Sets.immutableEnumSet(value));

	return ImmutableMap.copyOf(result);
}
 
Example #2
Source File: TargetCategoryParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public VisitorAction visit(OutputField outputField){
	ResultFeature resultFeature = outputField.getResultFeature();

	switch(resultFeature){
		case PROBABILITY:
		case CONFIDENCE:
		case AFFINITY:
			{
				outputField.setValue(parseTargetValue(outputField.getTargetField(), outputField.getValue()));
			}
			break;
		default:
			break;
	}

	return super.visit(outputField);
}
 
Example #3
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Set<ResultFeature> load(Output output){
	Set<ResultFeature> result = EnumSet.noneOf(ResultFeature.class);

	List<org.dmg.pmml.OutputField> pmmlOutputFields = output.getOutputFields();
	for(org.dmg.pmml.OutputField pmmlOutputField : pmmlOutputFields){
		String segmentId = pmmlOutputField.getSegmentId();

		if(segmentId != null){
			continue;
		}

		result.add(pmmlOutputField.getResultFeature());
	}

	return Sets.immutableEnumSet(result);
}
 
Example #4
Source File: ModelChainCompositionTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void getResultFields() throws Exception {
	ModelEvaluator<?> evaluator = createModelEvaluator();

	assertFalse(evaluator.hasResultFeature(ResultFeature.ENTITY_ID));
	assertTrue(evaluator.hasResultFeature(ResultFeature.TRANSFORMED_VALUE));

	checkResultFields(Arrays.asList("Class", "PollenIndex"), Arrays.asList("Setosa Pollen Index", "Versicolor Pollen Index", "Virginica Pollen Index", "Pollen Index", "Segment Id", "Class Node", "Class Score", "Class Score Code"), evaluator);
}
 
Example #5
Source File: UnsupportedMarkupInspector.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public VisitorAction visit(OutputField outputField){
	ResultFeature resultFeature = outputField.getResultFeature();

	switch(resultFeature){
		case STANDARD_ERROR:
			report(new UnsupportedAttributeException(outputField, resultFeature));
			break;
		default:
			break;
	}

	return super.visit(outputField);
}
 
Example #6
Source File: ModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
protected <V extends Number> Classification<Object, V> createClassification(ValueMap<Object, V> values){
	Set<ResultFeature> resultFeatures = getResultFeatures();

	if(resultFeatures.contains(ResultFeature.PROBABILITY) || resultFeatures.contains(ResultFeature.RESIDUAL)){
		return new ProbabilityDistribution<>(values);
	} else

	if(resultFeatures.contains(ResultFeature.CONFIDENCE)){
		return new ConfidenceDistribution<>(values);
	} else

	{
		return new VoteDistribution<>(values);
	}
}
 
Example #7
Source File: FieldNameFilterer.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public VisitorAction visit(OutputField outputField){
	ResultFeature resultFeature = outputField.getResultFeature();

	switch(resultFeature){
		case TRANSFORMED_VALUE:
		case DECISION:
			{
				String segmentId = outputField.getSegmentId();

				if(segmentId != null){
					Object value = outputField.getValue();

					if(value instanceof String){
						value = filter((String)value);
					}

					outputField.setValue(value);
				}
			}
			break;
		default:
			break;
	}

	return super.visit(outputField);
}
 
Example #8
Source File: TransformerBuilder.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private List<OutputField> getProbabilityFields(Evaluator evaluator, TargetField targetField){
	List<OutputField> outputFields = evaluator.getOutputFields();

	Predicate<OutputField> predicate = new Predicate<OutputField>(){

		@Override
		public boolean test(OutputField outputField){
			org.dmg.pmml.OutputField pmmlOutputField = outputField.getField();

			ResultFeature resultFeature = pmmlOutputField.getResultFeature();
			switch(resultFeature){
				case PROBABILITY:
					FieldName targetFieldName = pmmlOutputField.getTargetField();

					return Objects.equals(targetFieldName, null) || Objects.equals(targetFieldName, targetField.getName());
				default:
					return false;
			}
		}
	};

	List<OutputField> probabilityOutputFields = outputFields.stream()
		.filter(predicate)
		.collect(Collectors.toList());

	if(probabilityOutputFields.size() < 1){
		throw new IllegalArgumentException("Model does not have probability-type output fields");
	}

	return probabilityOutputFields;
}
 
Example #9
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
public void addResultFeatures(Set<ResultFeature> resultFeatures){
	this.resultFeatures = Sets.immutableEnumSet(Iterables.concat(this.resultFeatures, resultFeatures));
}
 
Example #10
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
protected Set<ResultFeature> getResultFeatures(){
	return this.resultFeatures;
}
 
Example #11
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
protected List<InputField> createInputFields(){
	List<InputField> inputFields = getActiveFields();

	List<OutputField> outputFields = getOutputFields();
	if(!outputFields.isEmpty()){
		List<ResidualInputField> residualInputFields = null;

		for(OutputField outputField : outputFields){
			org.dmg.pmml.OutputField pmmlOutputField = outputField.getField();

			if(!(ResultFeature.RESIDUAL).equals(pmmlOutputField.getResultFeature())){
				continue;
			}

			int depth = outputField.getDepth();
			if(depth > 0){
				throw new UnsupportedElementException(pmmlOutputField);
			}

			FieldName targetName = pmmlOutputField.getTargetField();
			if(targetName == null){
				targetName = getTargetName();
			}

			DataField dataField = getDataField(targetName);
			if(dataField == null){
				throw new MissingFieldException(targetName, pmmlOutputField);
			}

			MiningField miningField = getMiningField(targetName);
			if(miningField == null){
				throw new InvisibleFieldException(targetName, pmmlOutputField);
			}

			ResidualInputField residualInputField = new ResidualInputField(dataField, miningField);

			if(residualInputFields == null){
				residualInputFields = new ArrayList<>();
			}

			residualInputFields.add(residualInputField);
		}

		if(residualInputFields != null && !residualInputFields.isEmpty()){
			inputFields = ImmutableList.copyOf(Iterables.concat(inputFields, residualInputFields));
		}
	}

	return inputFields;
}
 
Example #12
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
protected Set<ResultFeature> getSegmentResultFeatures(String segmentId){
	return this.segmentResultFeatures.get(segmentId);
}
 
Example #13
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
private ModelEvaluator<?> createSegmentModelEvaluator(String segmentId, Model model){
	MiningModel miningModel = getModel();

	MiningFunction miningFunction = miningModel.getMiningFunction();

	Segmentation segmentation = miningModel.getSegmentation();

	Configuration configuration = ensureConfiguration();

	ModelEvaluatorFactory modelEvaluatorFactory = configuration.getModelEvaluatorFactory();

	ModelEvaluator<?> modelEvaluator = modelEvaluatorFactory.newModelEvaluator(getPMML(), model);

	Segmentation.MultipleModelMethod multipleModelMethod = segmentation.getMultipleModelMethod();
	switch(multipleModelMethod){
		case SELECT_FIRST:
		case SELECT_ALL:
		case MODEL_CHAIN:
			{
				Set<ResultFeature> resultFeatures = getResultFeatures();

				if(!resultFeatures.isEmpty()){
					modelEvaluator.addResultFeatures(resultFeatures);
				}
			}
			// Falls through
		default:
			{
				Set<ResultFeature> segmentResultFeatures = getSegmentResultFeatures(segmentId);

				if(segmentResultFeatures != null && !segmentResultFeatures.isEmpty()){
					modelEvaluator.addResultFeatures(segmentResultFeatures);
				}
			}
			break;
	}

	MiningFunction segmentMiningFunction = model.getMiningFunction();

	if((MiningFunction.CLASSIFICATION).equals(miningFunction) && (MiningFunction.CLASSIFICATION).equals(segmentMiningFunction)){
		List<TargetField> targetFields = getTargetFields();
		List<TargetField> segmentTargetFields = modelEvaluator.getTargetFields();

		if(targetFields.size() == 1 && segmentTargetFields.size() == 1){
			TargetField targetField = targetFields.get(0);
			TargetField segmentTargetField = segmentTargetFields.get(0);

			if(segmentTargetField instanceof DefaultTargetField){
				DefaultTargetField defaultTargetField = (DefaultTargetField)segmentTargetField;

				modelEvaluator.setDefaultDataField(new DataField(Evaluator.DEFAULT_TARGET_NAME, OpType.CATEGORICAL, targetField.getDataType()));
			}
		}
	}

	modelEvaluator.configure(configuration);

	return modelEvaluator;
}
 
Example #14
Source File: MultiTargetTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void evaluateWithProbability() throws Exception {
	ModelEvaluator<?> evaluator = createModelEvaluator();

	evaluator.addResultFeatures(EnumSet.of(ResultFeature.PROBABILITY));

	Map<FieldName, ?> arguments = createArguments("x", -1.0d);

	Map<FieldName, ?> results = evaluator.evaluate(arguments);

	Classification<?, ?> classification = (Classification<?, ?>)results.get(FieldName.create("y1"));

	assertTrue(classification instanceof NodeScoreDistribution);

	arguments = createArguments("x", 1.0d);

	results = evaluator.evaluate(arguments);

	classification = (Classification<?, ?>)results.get(FieldName.create("y2"));

	assertTrue(classification instanceof ProbabilityDistribution);
}
 
Example #15
Source File: ClusteringModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public List<OutputField> registerOutputFields(Label label, org.dmg.pmml.Model pmmlModel, SparkMLEncoder encoder){
	T model = getTransformer();

	List<Integer> clusters = LabelUtil.createTargetCategories(getNumberOfClusters());

	String predictionCol = model.getPredictionCol();

	OutputField pmmlPredictedOutputField = ModelUtil.createPredictedField(FieldName.create("pmml(" + predictionCol + ")"), OpType.CATEGORICAL, DataType.STRING)
		.setFinalResult(false);

	DerivedOutputField pmmlPredictedField = encoder.createDerivedField(pmmlModel, pmmlPredictedOutputField, true);

	OutputField predictedOutputField = new OutputField(FieldName.create(predictionCol), OpType.CATEGORICAL, DataType.INTEGER)
		.setResultFeature(ResultFeature.TRANSFORMED_VALUE)
		.setExpression(new FieldRef(pmmlPredictedField.getName()));

	DerivedOutputField predictedField = encoder.createDerivedField(pmmlModel, predictedOutputField, true);

	encoder.putOnlyFeature(predictionCol, new IndexFeature(encoder, predictedField, clusters));

	return Collections.emptyList();
}
 
Example #16
Source File: MultiTargetTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
@Test
public void evaluate() throws Exception {
	ModelEvaluator<?> evaluator = createModelEvaluator();

	assertTrue(evaluator.hasResultFeature(ResultFeature.PREDICTED_VALUE));

	checkResultFields(Arrays.asList("y1", "y2"), Arrays.asList("decision"), evaluator);

	Map<FieldName, ?> arguments = createArguments("x", -1.0d);

	Map<FieldName, ?> results = evaluator.evaluate(arguments);

	assertNotNull(getTarget(results, "y1"));
	assertNull(getTarget(results, "y2"));

	Classification<?, ?> classification = (Classification<?, ?>)results.get(FieldName.create("y1"));

	assertTrue(classification instanceof NodeScoreDistribution);

	assertEquals(0, getOutput(results, "decision"));

	arguments = createArguments("x", 1.0d);

	results = evaluator.evaluate(arguments);

	assertNull(getTarget(results, "y1"));
	assertNotNull(getTarget(results, "y2"));

	classification = (Classification<?, ?>)results.get(FieldName.create("y2"));

	assertFalse(classification instanceof ProbabilityDistribution);

	assertEquals(1, getOutput(results, "decision"));
}
 
Example #17
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * <p>
 * Indicates if this model evaluator provides the specified result feature.
 * </p>
 *
 * <p>
 * A result feature is first and foremost manifested through output fields.
 * However, selected result features may make a secondary manifestation through a target field.
 * </p>
 *
 * @see org.dmg.pmml.OutputField#getResultFeature()
 */
public boolean hasResultFeature(ResultFeature resultFeature){
	Set<ResultFeature> resultFeatures = getResultFeatures();

	return resultFeatures.contains(resultFeature);
}
 
Example #18
Source File: ClassificationModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 2 votes vote down vote up
@Override
public List<OutputField> registerOutputFields(Label label, Model pmmlModel, SparkMLEncoder encoder){
	T model = getTransformer();

	CategoricalLabel categoricalLabel = (CategoricalLabel)label;

	List<Integer> categories = LabelUtil.createTargetCategories(categoricalLabel.size());

	String predictionCol = model.getPredictionCol();

	Boolean keepPredictionCol = (Boolean)getOption(HasPredictionModelOptions.OPTION_KEEP_PREDICTIONCOL, Boolean.TRUE);

	OutputField pmmlPredictedOutputField = ModelUtil.createPredictedField(FieldName.create("pmml(" + predictionCol + ")"), OpType.CATEGORICAL, categoricalLabel.getDataType())
		.setFinalResult(false);

	DerivedOutputField pmmlPredictedField = encoder.createDerivedField(pmmlModel, pmmlPredictedOutputField, keepPredictionCol);

	MapValues mapValues = PMMLUtil.createMapValues(pmmlPredictedField.getName(), categoricalLabel.getValues(), categories)
		.setDataType(DataType.DOUBLE);

	OutputField predictedOutputField = new OutputField(FieldName.create(predictionCol), OpType.CONTINUOUS, DataType.DOUBLE)
		.setResultFeature(ResultFeature.TRANSFORMED_VALUE)
		.setExpression(mapValues);

	DerivedOutputField predictedField = encoder.createDerivedField(pmmlModel, predictedOutputField, keepPredictionCol);

	encoder.putOnlyFeature(predictionCol, new IndexFeature(encoder, predictedField, categories));

	List<OutputField> result = new ArrayList<>();

	if(model instanceof HasProbabilityCol){
		HasProbabilityCol hasProbabilityCol = (HasProbabilityCol)model;

		String probabilityCol = hasProbabilityCol.getProbabilityCol();

		List<Feature> features = new ArrayList<>();

		for(int i = 0; i < categoricalLabel.size(); i++){
			Object value = categoricalLabel.getValue(i);

			OutputField probabilityField = ModelUtil.createProbabilityField(FieldName.create(probabilityCol + "(" + value + ")"), DataType.DOUBLE, value);

			result.add(probabilityField);

			features.add(new ContinuousFeature(encoder, probabilityField));
		}

		// XXX
		encoder.putFeatures(probabilityCol, features);
	}

	return result;
}