org.dmg.pmml.OutputField Java Examples

The following examples show how to use org.dmg.pmml.OutputField. 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: GeneralizedLinearRegressionModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<OutputField> registerOutputFields(Label label, Model pmmlModel, SparkMLEncoder encoder){
	GeneralizedLinearRegressionModel model = getTransformer();

	List<OutputField> result = super.registerOutputFields(label, pmmlModel, encoder);

	MiningFunction miningFunction = getMiningFunction();
	switch(miningFunction){
		case CLASSIFICATION:
			CategoricalLabel categoricalLabel = (CategoricalLabel)label;

			result = new ArrayList<>(result);
			result.addAll(ModelUtil.createProbabilityFields(DataType.DOUBLE, categoricalLabel.getValues()));
			break;
		default:
			break;
	}

	return result;
}
 
Example #2
Source File: MapHolderParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public VisitorAction visit(Output output){

	if(output.hasOutputFields()){
		List<OutputField> outputFields = output.getOutputFields();

		for(ListIterator<OutputField> it = outputFields.listIterator(); it.hasNext(); ){
			OutputField outputField = it.next();

			if(outputField.hasValues()){
				it.set(new RichOutputField(outputField));
			}
		}
	}

	return super.visit(output);
}
 
Example #3
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 #4
Source File: OutputUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private String getEntityId(Object object, OutputField outputField){
	HasEntityId hasEntityId = TypeUtil.cast(HasEntityId.class, object);

	int rank = outputField.getRank();
	if(rank <= 0){
		throw new InvalidAttributeException(outputField, PMMLAttributes.OUTPUTFIELD_RANK, rank);
	} // End if

	if(rank > 1){
		HasEntityIdRanking hasEntityIdRanking = TypeUtil.cast(HasEntityIdRanking.class, object);

		OutputField.RankOrder rankOrder = outputField.getRankOrder();
		switch(rankOrder){
			case DESCENDING:
				break;
			default:
				throw new UnsupportedAttributeException(outputField, rankOrder);
		}

		return getElement(hasEntityIdRanking.getEntityIdRanking(), rank);
	}

	return hasEntityId.getEntityId();
}
 
Example #5
Source File: ModelEvaluationContext.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private Field<?> resolveField(FieldName name, MiningModelEvaluationContext context){

	while(context != null){
		OutputField outputField = context.getOutputField(name);
		if(outputField != null){
			return outputField;
		}

		DerivedField localDerivedField = context.getLocalDerivedField(name);
		if(localDerivedField != null){
			return localDerivedField;
		}

		context = context.getParent();
	}

	return null;
}
 
Example #6
Source File: ReflectionUtilTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void getGetterMethods(){
	Map<Field, Method> getterMethods = ReflectionUtil.getGetterMethods(OutputField.class);

	assertEquals(1 /* PMMLObject */ + 20 /* OutputField */, getterMethods.size());

	try {
		Field field = OutputField.class.getDeclaredField("DEFAULT_RANK");

		ReflectionUtil.getGetterMethod(field);

		fail();
	} catch(ReflectiveOperationException roe){
		fail();
	} catch(RuntimeException re){
		// Ignored
	}
}
 
Example #7
Source File: MLModelRegistryService.java    From streamline with Apache License 2.0 6 votes vote down vote up
private List<MLModelField> doGetOutputFieldsForPMMLStream(String pmmlContents) throws SAXException, JAXBException, UnsupportedEncodingException {
    List<MLModelField> fieldNames = new ArrayList<>();
    PMMLManager pmmlManager = new PMMLManager(IOUtil.unmarshal(new ByteArrayInputStream(pmmlContents.getBytes("UTF-8"))));
    Evaluator modelEvaluator = (ModelEvaluator<?>) pmmlManager.getModelManager(null, ModelEvaluatorFactory.getInstance());
    modelEvaluator.getPredictedFields().forEach((f) -> fieldNames.add(getModelField(modelEvaluator.getDataField(f))));

    modelEvaluator.getOutputFields().forEach((f) -> {
        OutputField outputField = modelEvaluator.getOutputField(f);
        ResultFeatureType resultFeatureType = outputField.getFeature();
        if (resultFeatureType != ResultFeatureType.PREDICTED_VALUE &&
                resultFeatureType != ResultFeatureType.PREDICTED_DISPLAY_VALUE) {
            fieldNames.add(getModelField(outputField));
        }
    });

    return fieldNames;
}
 
Example #8
Source File: MLModelRegistryService.java    From registry with Apache License 2.0 6 votes vote down vote up
private List<MLModelField> doGetOutputFieldsForPMMLStream(String pmmlContents) throws SAXException, JAXBException {
    List<MLModelField> fieldNames = new ArrayList<>();
    PMMLManager pmmlManager = new PMMLManager(IOUtil.unmarshal(new ByteArrayInputStream(pmmlContents.getBytes())));
    Evaluator modelEvaluator = (ModelEvaluator<?>) pmmlManager.getModelManager(null, ModelEvaluatorFactory.getInstance());
    modelEvaluator.getPredictedFields().forEach((f) -> fieldNames.add(getModelField(modelEvaluator.getDataField(f))));

    modelEvaluator.getOutputFields().forEach((f) -> {
        OutputField outputField = modelEvaluator.getOutputField(f);
        ResultFeatureType resultFeatureType = outputField.getFeature();
        if (resultFeatureType != ResultFeatureType.PREDICTED_VALUE &&
                resultFeatureType != ResultFeatureType.PREDICTED_DISPLAY_VALUE) {
            fieldNames.add(getModelField(outputField));
        }
    });

    return fieldNames;
}
 
Example #9
Source File: OneClassSVM.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public SupportVectorMachineModel encodeModel(Schema schema){
	Transformation outlier = new OutlierTransformation(){

		@Override
		public Expression createExpression(FieldRef fieldRef){
			return PMMLUtil.createApply(PMMLFunctions.LESSOREQUAL, fieldRef, PMMLUtil.createConstant(0d));
		}
	};

	SupportVectorMachineModel supportVectorMachineModel = super.encodeModel(schema)
		.setOutput(ModelUtil.createPredictedOutput(FieldName.create("decisionFunction"), OpType.CONTINUOUS, DataType.DOUBLE, outlier));

	Output output = supportVectorMachineModel.getOutput();

	List<OutputField> outputFields = output.getOutputFields();
	if(outputFields.size() != 2){
		throw new IllegalArgumentException();
	}

	OutputField decisionFunctionOutputField = outputFields.get(0);

	if(!decisionFunctionOutputField.isFinalResult()){
		decisionFunctionOutputField.setFinalResult(true);
	}

	return supportVectorMachineModel;
}
 
Example #10
Source File: MiningModelUtil.java    From pyramid with Apache License 2.0 5 votes vote down vote up
@Override
public Feature apply(Model model){
    Output output = model.getOutput();

    if(output == null || !output.hasOutputFields()){
        throw new IllegalArgumentException();
    }

    OutputField outputField = Iterables.getLast(output.getOutputFields());

    return new ContinuousFeature(null, outputField.getName(), outputField.getDataType());
}
 
Example #11
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 #12
Source File: MiningModelEvaluationContext.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
void putOutputField(FieldName name, OutputField outputField){

		if(this.outputFields == null){
			this.outputFields = new HashMap<>();
		}

		this.outputFields.put(name, outputField);
	}
 
Example #13
Source File: MiningModelEvaluationContext.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
public OutputField getOutputField(FieldName name){

		if(this.outputFields == null){
			return null;
		}

		return this.outputFields.get(name);
	}
 
Example #14
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 #15
Source File: OutputUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Double getProbability(Object object, OutputField outputField){
	HasProbability hasProbability = TypeUtil.cast(HasProbability.class, object);

	Object value = getCategoryValue(object, outputField);

	return hasProbability.getProbability(value);
}
 
Example #16
Source File: OutputUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Double getConfidence(Object object, OutputField outputField){
	HasConfidence hasConfidence = TypeUtil.cast(HasConfidence.class, object);

	Object value = getCategoryValue(object, outputField);

	return hasConfidence.getConfidence(value);
}
 
Example #17
Source File: OutputUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Object getCategoryValue(Object object, OutputField outputField){
	Object value = outputField.getValue();

	// "If the value attribute is not specified, then the predicted categorical value should be returned as a result"
	if(value == null){
		return getPredictedValue(object);
	}

	return value;
}
 
Example #18
Source File: OutputUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public Double getAffinity(Object object, OutputField outputField){
	HasAffinity hasAffinity = TypeUtil.cast(HasAffinity.class, object);

	int rank = outputField.getRank();
	if(rank <= 0){
		throw new InvalidAttributeException(outputField, PMMLAttributes.OUTPUTFIELD_RANK, rank);
	} // End if

	if(rank > 1){
		HasAffinityRanking hasAffinityRanking = TypeUtil.cast(HasAffinityRanking.class, object);

		OutputField.RankOrder rankOrder = outputField.getRankOrder();
		switch(rankOrder){
			case DESCENDING:
				break;
			default:
				throw new UnsupportedAttributeException(outputField, rankOrder);
		}

		return getElement(hasAffinityRanking.getAffinityRanking(), rank);
	}

	Object value = getCategoryValue(object, outputField);

	value = TypeUtil.format(value);

	return hasAffinity.getAffinity((String)value);
}
 
Example #19
Source File: OutputUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public String getReasonCode(Object object, OutputField outputField){
	HasReasonCodeRanking hasReasonCodeRanking = TypeUtil.cast(HasReasonCodeRanking.class, object);

	int rank = outputField.getRank();
	if(rank <= 0){
		throw new InvalidAttributeException(outputField, PMMLAttributes.OUTPUTFIELD_RANK, rank);
	}

	return getElement(hasReasonCodeRanking.getReasonCodeRanking(), rank);
}
 
Example #20
Source File: OutputFilters.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public boolean test(OutputField outputField){
	return outputField.isFinalResult();
}
 
Example #21
Source File: OutputFilters.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public boolean test(OutputField outputField){
	return true;
}
 
Example #22
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 #23
Source File: OutputUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings (
	value = {"unchecked"}
)
static
private Object getRuleFeature(HasRuleValues hasRuleValues, AssociationRule associationRule, OutputField outputField, OutputField.RuleFeature ruleFeature){

	switch(ruleFeature){
		case ANTECEDENT:
			return getItemValues(hasRuleValues, associationRule.getAntecedent());
		case CONSEQUENT:
			return getItemValues(hasRuleValues, associationRule.getConsequent());
		case RULE:
			{
				Joiner joiner = Joiner.on(',');

				StringBuilder sb = new StringBuilder();

				String left = joiner.join(getItemValues(hasRuleValues, associationRule.getAntecedent()));
				sb.append('{').append(left).append('}');

				sb.append("->");

				String right = joiner.join(getItemValues(hasRuleValues, associationRule.getConsequent()));
				sb.append('{').append(right).append('}');

				return sb.toString();
			}
		case RULE_ID:
			{
				HasEntityRegistry<AssociationRule> hasEntityRegistry = (HasEntityRegistry<AssociationRule>)hasRuleValues;

				return EntityUtil.getId(associationRule, hasEntityRegistry);
			}
		case CONFIDENCE:
			return associationRule.getConfidence();
		case SUPPORT:
			return associationRule.getSupport();
		case LIFT:
			return associationRule.getLift();
		case LEVERAGE:
			return associationRule.getLeverage();
		case AFFINITY:
			return associationRule.getAffinity();
		default:
			throw new UnsupportedAttributeException(outputField, ruleFeature);
	}
}
 
Example #24
Source File: OutputUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
public Object getRuleValue(Object object, OutputField outputField){
	return getRuleValue(object, outputField, outputField.getRuleFeature());
}
 
Example #25
Source File: ModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
public List<OutputField> registerOutputFields(Label label, org.dmg.pmml.Model model, SparkMLEncoder encoder){
	return null;
}
 
Example #26
Source File: ModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
public org.dmg.pmml.Model registerModel(SparkMLEncoder encoder){
	Schema schema = encodeSchema(encoder);

	Label label = schema.getLabel();

	org.dmg.pmml.Model model = encodeModel(schema);

	List<OutputField> sparkOutputFields = registerOutputFields(label, model, encoder);
	if(sparkOutputFields != null && sparkOutputFields.size() > 0){
		org.dmg.pmml.Model finalModel = MiningModelUtil.getFinalModel(model);

		Output output = ModelUtil.ensureOutput(finalModel);

		List<OutputField> outputFields = output.getOutputFields();

		outputFields.addAll(sparkOutputFields);
	}

	return model;
}
 
Example #27
Source File: RichOutputField.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
public RichOutputField(OutputField outputField){
	ReflectionUtil.copyState(outputField, this);
}
 
Example #28
Source File: RegressionModelConverter.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, Model pmmlModel, SparkMLEncoder encoder){
	T model = getTransformer();

	String predictionCol = model.getPredictionCol();

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

	OutputField predictedOutputField = ModelUtil.createPredictedField(FieldName.create(predictionCol), OpType.CONTINUOUS, label.getDataType());

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

	encoder.putOnlyFeature(predictionCol, new ContinuousFeature(encoder, predictedField));

	return Collections.emptyList();
}
 
Example #29
Source File: VersionInspectorTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
@Test
public void inspectFieldAnnotations(){
	PMML pmml = createPMML();

	AssociationModel model = new AssociationModel();

	pmml.addModels(model);

	assertVersionRange(pmml, Version.PMML_3_0, Version.PMML_4_4);

	Output output = new Output();

	model.setOutput(output);

	assertVersionRange(pmml, Version.PMML_4_0, Version.PMML_4_4);

	model.setScorable(Boolean.FALSE);

	assertVersionRange(pmml, Version.PMML_4_1, Version.PMML_4_4);

	model.setScorable(null);

	assertVersionRange(pmml, Version.PMML_4_0, Version.PMML_4_4);

	OutputField outputField = new OutputField()
		.setRuleFeature(OutputField.RuleFeature.AFFINITY);

	output.addOutputFields(outputField);

	assertVersionRange(pmml, Version.PMML_4_1, Version.PMML_4_2);

	outputField.setDataType(DataType.DOUBLE);

	assertVersionRange(pmml, Version.PMML_4_1, Version.PMML_4_4);

	model.setOutput(null);

	assertVersionRange(pmml, Version.PMML_3_0, Version.PMML_4_4);
}
 
Example #30
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;
}