org.jpmml.evaluator.TargetField Java Examples

The following examples show how to use org.jpmml.evaluator.TargetField. 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: ScorecardEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private <V extends Number> ReasonCodeRanking<V> createReasonCodeRanking(TargetField targetField, Value<V> score, ValueMap<String, V> reasonCodePoints){
	score = TargetUtil.evaluateRegressionInternal(targetField, score);

	Collection<Map.Entry<String, Value<V>>> entrySet = reasonCodePoints.entrySet();
	for(Iterator<Map.Entry<String, Value<V>>> it = entrySet.iterator(); it.hasNext(); ){
		Map.Entry<String, Value<V>> entry = it.next();

		Value<V> value = entry.getValue();
		if(value.compareTo(Numbers.DOUBLE_ZERO) < 0){
			it.remove();
		}
	}

	return new ReasonCodeRanking<>(score, reasonCodePoints);
}
 
Example #2
Source File: AssociationModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected List<TargetField> createTargetFields(){
	List<TargetField> targetFields = super.createTargetFields();

	if(!targetFields.isEmpty()){
		throw createMiningSchemaException("Expected 0 target fields, got " + targetFields.size() + " target fields");
	}

	return targetFields;
}
 
Example #3
Source File: ModelUtil.java    From openscoring with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public Map<String, List<Field>> encodeSchema(Evaluator evaluator){
	Map<String, List<Field>> result = new LinkedHashMap<>();

	if(evaluator instanceof HasInputFields){
		HasInputFields hasInputFields = (HasInputFields)evaluator;

		List<InputField> inputFields = hasInputFields.getInputFields();
		if(!inputFields.isEmpty()){
			result.put("inputFields", encodeModelFields(inputFields));
		}
	} // End if

	if(evaluator instanceof HasGroupFields){
		HasGroupFields hasGroupFields = (HasGroupFields)evaluator;

		List<InputField> groupFields = hasGroupFields.getGroupFields();
		if(!groupFields.isEmpty()){
			result.put("groupFields", encodeModelFields(groupFields));
		}
	} // End if

	if(evaluator instanceof HasResultFields){
		HasResultFields hasResultFields = (HasResultFields)evaluator;

		List<TargetField> targetFields = hasResultFields.getTargetFields();
		if(!targetFields.isEmpty()){
			result.put("targetFields", encodeModelFields(targetFields));
		}

		List<OutputField> outputFields = hasResultFields.getOutputFields();
		if(!outputFields.isEmpty()){
			result.put("outputFields", encodeModelFields(outputFields));
		}
	}

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

	List<InputField> inputFields = evaluator.getInputFields();
	List<TargetField> targetFields = evaluator.getTargetFields();
	List<OutputField> outputFields = evaluator.getOutputFields();

	assertEquals(1, inputFields.size());
	assertEquals(0, targetFields.size());
	assertEquals(2, outputFields.size());

	try {
		evaluator.getTargetField();

		fail();
	} catch(EvaluationException ee){
		// Ignored
	}

	assertEquals(Evaluator.DEFAULT_TARGET_NAME, evaluator.getTargetName());

	Map<FieldName, ?> arguments = createArguments("input", 2d);

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

	assertEquals(3, results.size());

	assertEquals(2d * 2d, (Double)getTarget(results, Evaluator.DEFAULT_TARGET_NAME), 1e-8d);

	assertNotNull((Double)getOutput(results, "output"));
	assertNull(getOutput(results, "report(output)"));
}
 
Example #5
Source File: GeneralRegressionModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private <V extends Number> Map<FieldName, ?> evaluateGeneralRegression(ValueFactory<V> valueFactory, EvaluationContext context){
	GeneralRegressionModel generalRegressionModel = getModel();

	TargetField targetField = getTargetField();

	Value<V> result = computeDotProduct(valueFactory, context);
	if(result == null){
		return TargetUtil.evaluateRegressionDefault(valueFactory, targetField);
	}

	GeneralRegressionModel.ModelType modelType = generalRegressionModel.getModelType();
	switch(modelType){
		case REGRESSION:
		case GENERAL_LINEAR:
			break;
		case GENERALIZED_LINEAR:
			result = computeLink(result, context);
			break;
		case MULTINOMIAL_LOGISTIC:
		case ORDINAL_MULTINOMIAL:
		case COX_REGRESSION:
			throw new InvalidAttributeException(generalRegressionModel, modelType);
		default:
			throw new UnsupportedAttributeException(generalRegressionModel, modelType);
	}

	return TargetUtil.evaluateRegression(targetField, result);
}
 
Example #6
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public FieldName getTargetName(){
	List<TargetField> targetFields = super.getTargetFields();

	if(targetFields.isEmpty()){
		return Evaluator.DEFAULT_TARGET_NAME;
	}

	return super.getTargetName();
}
 
Example #7
Source File: TreeModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private <V extends Number> NodeScore<V> createNodeScore(ValueFactory<V> valueFactory, TargetField targetField, Node node){
	Object score = node.getScore();

	Value<V> value;

	if(score instanceof Number){
		value = valueFactory.newValue((Number)score);
	} else

	{
		value = valueFactory.newValue((String)score);
	}

	value = TargetUtil.evaluateRegressionInternal(targetField, value);

	NodeScore<V> result = new NodeScore<V>(value, node){

		@Override
		public BiMap<String, Node> getEntityRegistry(){
			return TreeModelEvaluator.this.getEntityRegistry();
		}

		@Override
		public List<Node> getDecisionPath(){
			return TreeModelEvaluator.this.getPath(getNode());
		}
	};

	return result;
}
 
Example #8
Source File: TreeModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected <V extends Number> Map<FieldName, ?> evaluateRegression(ValueFactory<V> valueFactory, EvaluationContext context){
	TargetField targetField = getTargetField();

	Trail trail = new Trail();

	Node node = evaluateTree(trail, context);
	if(node == null){
		return TargetUtil.evaluateRegressionDefault(valueFactory, targetField);
	}

	NodeScore<V> result = createNodeScore(valueFactory, targetField, node);

	return TargetUtil.evaluateRegression(targetField, result);
}
 
Example #9
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 #10
Source File: TransformerBuilder.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private TargetField getTargetField(Evaluator evaluator){
	List<TargetField> targetFields = evaluator.getTargetFields();

	if(targetFields.size() < 1){
		throw new IllegalArgumentException("Model does not have a target field");
	} else

	if(targetFields.size() > 1){
		throw new IllegalArgumentException("Model has multiple target fields (" + targetFields + ")");
	}

	return targetFields.get(0);
}
 
Example #11
Source File: TransformerBuilder.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <p>
 * Appends the probability distribution associated with the sole target field of a classification model.
 * </p>
 *
 * @param columnName The name of the probability column.
 * @param labels The ordering of class label elements in the vector.
 */
public TransformerBuilder withProbabilityCol(String columnName, List<String> labels){
	Evaluator evaluator = getEvaluator();

	TargetField targetField = getTargetField(evaluator);

	List<OutputField> probabilityOutputFields = getProbabilityFields(evaluator, targetField);

	List<String> targetCategories = probabilityOutputFields.stream()
		.map(probabilityOutputField -> {
			org.dmg.pmml.OutputField pmmlOutputField = probabilityOutputField.getField();

			String value = pmmlOutputField.getValue();
			if(value == null){
				throw new MissingAttributeException(pmmlOutputField, PMMLAttributes.OUTPUTFIELD_VALUE);
			}

			return value;
		})
		.collect(Collectors.toList());

	if((labels != null) && (labels.size() != targetCategories.size() || !labels.containsAll(targetCategories))){
		throw new IllegalArgumentException("Model has an incompatible set of probability-type output fields (expected " + labels + ", got " + targetCategories + ")");
	}

	this.columnProducers.add(new ProbabilityColumnProducer(targetField, columnName, labels != null ? labels : targetCategories));

	return this;
}
 
Example #12
Source File: TransformerBuilder.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <p>
 * Appends all target fields.
 * </p>
 *
 * @see Evaluator#getTargetFields()
 */
public TransformerBuilder withTargetCols(){
	Evaluator evaluator = getEvaluator();

	List<TargetField> targetFields = evaluator.getTargetFields();
	for(TargetField targetField : targetFields){
		this.columnProducers.add(new TargetColumnProducer(targetField, null));
	}

	return this;
}
 
Example #13
Source File: TargetColumnProducer.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private String getName(TargetField field){

	if(field.isSynthetic()){
		return "_target";
	}

	return (field.getName()).getValue();
}
 
Example #14
Source File: TargetColumnProducer.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public StructField init(Evaluator evaluator){
	TargetField field = getField();

	DataType dataType = field.getDataType();

	return DataTypes.createStructField(getColumnName(), SchemaUtil.translateDataType(dataType), false);
}
 
Example #15
Source File: ProbabilityColumnProducer.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 4 votes vote down vote up
ProbabilityColumnProducer(TargetField field, String columnName, List<String> labels){
	super(field, columnName != null ? columnName : "probability");

	setLabels(labels);
}
 
Example #16
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected <V extends Number> Map<FieldName, ?> evaluateRegression(ValueFactory<V> valueFactory, EvaluationContext context){
	MiningModel miningModel = getModel();

	List<SegmentResult> segmentResults = evaluateSegmentation((MiningModelEvaluationContext)context);

	Map<FieldName, ?> predictions = getSegmentationResult(REGRESSION_METHODS, segmentResults);
	if(predictions != null){
		return predictions;
	}

	TargetField targetField = getTargetField();

	Segmentation segmentation = miningModel.getSegmentation();

	Segmentation.MultipleModelMethod multipleModelMethod = segmentation.getMultipleModelMethod();
	Segmentation.MissingPredictionTreatment missingPredictionTreatment = segmentation.getMissingPredictionTreatment();
	Number missingThreshold = segmentation.getMissingThreshold();
	if(missingThreshold.doubleValue() < 0d || missingThreshold.doubleValue() > 1d){
		throw new InvalidAttributeException(segmentation, PMMLAttributes.SEGMENTATION_MISSINGTHRESHOLD, missingThreshold);
	}

	Value<V> value;

	switch(multipleModelMethod){
		case AVERAGE:
		case WEIGHTED_AVERAGE:
		case MEDIAN:
		case WEIGHTED_MEDIAN:
		case SUM:
		case WEIGHTED_SUM:
			value = MiningModelUtil.aggregateValues(valueFactory, multipleModelMethod, missingPredictionTreatment, missingThreshold, segmentResults);
			if(value == null){
				return TargetUtil.evaluateRegressionDefault(valueFactory, targetField);
			}
			break;
		case MAJORITY_VOTE:
		case WEIGHTED_MAJORITY_VOTE:
		case MAX:
		case SELECT_FIRST:
		case SELECT_ALL:
		case MODEL_CHAIN:
			throw new InvalidAttributeException(segmentation, multipleModelMethod);
		default:
			throw new UnsupportedAttributeException(segmentation, multipleModelMethod);
	}

	value = TargetUtil.evaluateRegressionInternal(targetField, value);

	Regression<V> result = new MiningScore<V>(value){

		@Override
		public Collection<? extends SegmentResult> getSegmentResults(){
			return segmentResults;
		}
	};

	return TargetUtil.evaluateRegression(targetField, result);
}
 
Example #17
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 #18
Source File: SegmentResult.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public List<TargetField> getTargetFields(){
	ModelEvaluator<?> modelEvaluator = getModelEvaluator();

	return modelEvaluator.getTargetFields();
}
 
Example #19
Source File: GeneralRegressionModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
private List<Object> parseTargetCategories(){
	GeneralRegressionModel generalRegressionModel = getModel();

	TargetField targetField = getTargetField();

	OpType opType = targetField.getOpType();
	switch(opType){
		case CATEGORICAL:
		case ORDINAL:
			break;
		default:
			throw new InvalidElementException(generalRegressionModel);
	}

	List<Object> targetCategories = targetField.getCategories();
	if(targetCategories == null || targetCategories.size() < 2){
		throw new InvalidElementException(generalRegressionModel);
	}

	Object targetReferenceCategory = generalRegressionModel.getTargetReferenceCategory();

	GeneralRegressionModel.ModelType modelType = generalRegressionModel.getModelType();
	switch(modelType){
		case GENERALIZED_LINEAR:
		case MULTINOMIAL_LOGISTIC:
			if(targetReferenceCategory == null){
				Map<?, List<PCell>> paramMatrixMap = getParamMatrixMap();

				// "The reference category is the one from DataDictionary that does not appear in the ParamMatrix"
				Set<?> targetReferenceCategories = targetCategories.stream()
					.filter(targetCategory -> !paramMatrixMap.containsKey(targetCategory))
					.collect(Collectors.toSet());

				if(targetReferenceCategories.size() != 1){
					ParamMatrix paramMatrix = generalRegressionModel.getParamMatrix();

					throw new InvalidElementException(paramMatrix);
				}

				targetReferenceCategory = Iterables.getOnlyElement(targetReferenceCategories);
			}
			break;
		case ORDINAL_MULTINOMIAL:
			break;
		case REGRESSION:
		case GENERAL_LINEAR:
		case COX_REGRESSION:
			throw new InvalidAttributeException(generalRegressionModel, modelType);
		default:
			throw new UnsupportedAttributeException(generalRegressionModel, modelType);
	}

	if(targetReferenceCategory != null){
		targetCategories = new ArrayList<>(targetCategories);

		// Move the element from any position to the last position
		if(targetCategories.remove(targetReferenceCategory)){
			targetCategories.add(targetReferenceCategory);
		}
	}

	return targetCategories;
}
 
Example #20
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected <V extends Number> Map<FieldName, AffinityDistribution<V>> evaluateMixed(ValueFactory<V> valueFactory, EvaluationContext context){
	NearestNeighborModel nearestNeighborModel = getModel();

	Table<Integer, FieldName, FieldValue> table = getTrainingInstances();

	List<InstanceResult<V>> instanceResults = evaluateInstanceRows(valueFactory, context);

	Ordering<InstanceResult<V>> ordering = (Ordering.natural()).reverse();

	List<InstanceResult<V>> nearestInstanceResults = ordering.sortedCopy(instanceResults);

	Integer numberOfNeighbors = nearestNeighborModel.getNumberOfNeighbors();
	if(numberOfNeighbors == null){
		throw new MissingAttributeException(nearestNeighborModel, PMMLAttributes.NEARESTNEIGHBORMODEL_NUMBEROFNEIGHBORS);
	}

	nearestInstanceResults = nearestInstanceResults.subList(0, numberOfNeighbors);

	Function<Integer, String> function = new Function<Integer, String>(){

		@Override
		public String apply(Integer row){
			return row.toString();
		}
	};

	FieldName instanceIdVariable = nearestNeighborModel.getInstanceIdVariable();
	if(instanceIdVariable != null){
		function = createIdentifierResolver(instanceIdVariable, table);
	}

	Map<FieldName, AffinityDistribution<V>> results = new LinkedHashMap<>();

	List<TargetField> targetFields = getTargetFields();
	for(TargetField targetField : targetFields){
		FieldName name = targetField.getFieldName();

		Object value;

		OpType opType = targetField.getOpType();
		switch(opType){
			case CONTINUOUS:
				value = calculateContinuousTarget(valueFactory, name, nearestInstanceResults, table);
				break;
			case CATEGORICAL:
				value = calculateCategoricalTarget(valueFactory, name, nearestInstanceResults, table);
				break;
			default:
				throw new InvalidElementException(nearestNeighborModel);
		}

		value = TypeUtil.parseOrCast(targetField.getDataType(), value);

		AffinityDistribution<V> result = createAffinityDistribution(instanceResults, function, value);

		results.put(name, result);
	}

	return results;
}
 
Example #21
Source File: RegressionModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected <V extends Number> Map<FieldName, ?> evaluateRegression(ValueFactory<V> valueFactory, EvaluationContext context){
	RegressionModel regressionModel = getModel();

	TargetField targetField = getTargetField();

	FieldName targetName = regressionModel.getTargetField();
	if(targetName != null && !Objects.equals(targetField.getFieldName(), targetName)){
		throw new InvalidAttributeException(regressionModel, PMMLAttributes.REGRESSIONMODEL_TARGETFIELD, targetName);
	}

	List<RegressionTable> regressionTables = regressionModel.getRegressionTables();
	if(regressionTables.size() != 1){
		throw new InvalidElementListException(regressionTables);
	}

	RegressionTable regressionTable = regressionTables.get(0);

	Value<V> result = evaluateRegressionTable(valueFactory, regressionTable, context);
	if(result == null){
		return TargetUtil.evaluateRegressionDefault(valueFactory, targetField);
	}

	RegressionModel.NormalizationMethod normalizationMethod = regressionModel.getNormalizationMethod();
	switch(normalizationMethod){
		case NONE:
		case SOFTMAX:
		case LOGIT:
		case EXP:
		case PROBIT:
		case CLOGLOG:
		case LOGLOG:
		case CAUCHIT:
			RegressionModelUtil.normalizeRegressionResult(normalizationMethod, result);
			break;
		case SIMPLEMAX:
			throw new InvalidAttributeException(regressionModel, normalizationMethod);
		default:
			throw new UnsupportedAttributeException(regressionModel, normalizationMethod);
	}

	return TargetUtil.evaluateRegression(targetField, result);
}
 
Example #22
Source File: TargetColumnProducer.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 4 votes vote down vote up
TargetColumnProducer(TargetField field, String columnName){
	super(field, columnName != null ? columnName : getName(field));
}
 
Example #23
Source File: TransformerBuilder.java    From jpmml-evaluator-spark with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * <p>
 * Appends the sole target field of a regression or classification model.
 * </p>
 *
 * @param columnName The name of the target column.
 */
public TransformerBuilder withLabelCol(String columnName){
	Evaluator evaluator = getEvaluator();

	TargetField targetField = getTargetField(evaluator);

	this.columnProducers.add(new TargetColumnProducer(targetField, columnName));

	return this;
}