org.dmg.pmml.FieldName Java Examples

The following examples show how to use org.dmg.pmml.FieldName. 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: AdaConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	RGenericVector ada = getObject();

	RGenericVector model = ada.getGenericElement("model");

	RGenericVector trees = model.getGenericElement("trees");
	RDoubleVector alpha = model.getDoubleElement("alpha");

	List<TreeModel> treeModels = encodeTreeModels(trees);

	MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(null))
		.setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.WEIGHTED_SUM, treeModels, alpha.getValues()))
		.setOutput(ModelUtil.createPredictedOutput(FieldName.create("adaValue"), OpType.CONTINUOUS, DataType.DOUBLE));

	return MiningModelUtil.createBinaryLogisticClassification(miningModel, 2d, 0d, RegressionModel.NormalizationMethod.LOGIT, true, schema);
}
 
Example #2
Source File: ExpressionTranslatorTest.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void translateArithmeticExpression(){
	String string = "-((x1 - 1) / (x2 + 1))";

	Apply expected = PMMLUtil.createApply(PMMLFunctions.MULTIPLY)
		.addExpressions(PMMLUtil.createConstant(-1))
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.DIVIDE)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.SUBTRACT)
				.addExpressions(new FieldRef(FieldName.create("x1")), PMMLUtil.createConstant(1, DataType.DOUBLE))
			)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.ADD)
				.addExpressions(new FieldRef(FieldName.create("x2")), PMMLUtil.createConstant(1, DataType.DOUBLE))
			)
		);

	checkExpression(expected, string);
}
 
Example #3
Source File: MissingValueStrategyTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void defaultChildMultiplePenalties() throws Exception {
	Map<FieldName, ?> arguments = createArguments("outlook", null, "temperature", null, "humidity", 70d);

	NodeScoreDistribution<?> targetValue = evaluate(TreeModel.MissingValueStrategy.DEFAULT_CHILD, 0.8d, arguments);

	assertEquals("3", targetValue.getEntityId());

	assertEquals((Double)0.9d, targetValue.getProbability("will play"));
	assertEquals((Double)0.05d, targetValue.getProbability("may play"));
	assertEquals((Double)0.05d, targetValue.getProbability("no play"));

	double missingValuePenalty = (0.8d * 0.8d);

	assertEquals((Double)(0.9d * missingValuePenalty), targetValue.getConfidence("will play"));
	assertEquals((Double)(0.05d * missingValuePenalty), targetValue.getConfidence("may play"));
	assertEquals((Double)(0.05d * missingValuePenalty), targetValue.getConfidence("no play"));
}
 
Example #4
Source File: Classification.java    From jpmml-lightgbm with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Label encodeLabel(FieldName targetField, List<?> targetCategories, PMMLEncoder encoder){
	DataField dataField;

	if(targetCategories == null){
		targetCategories = LabelUtil.createTargetCategories(this.num_class_);

		dataField = encoder.createDataField(targetField, OpType.CATEGORICAL, DataType.INTEGER, targetCategories);
	} else

	{
		if(targetCategories.size() != this.num_class_){
			throw new IllegalArgumentException("Expected " + this.num_class_ + " target categories, got " + targetCategories.size() + " target categories");
		}

		dataField = encoder.createDataField(targetField, OpType.CATEGORICAL, DataType.STRING, targetCategories);
	}

	return new CategoricalLabel(dataField);
}
 
Example #5
Source File: DefineFunctionEvaluationContext.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public FieldValue prepare(FieldName name, Object value){
	ParameterField parameterField = findParameterField(name);
	if(parameterField == null){
		throw new MissingFieldException(name);
	}

	DataType dataType = parameterField.getDataType();
	if(dataType == null){
		throw new MissingAttributeException(parameterField, PMMLAttributes.PARAMETERFIELD_DATATYPE);
	}

	OpType opType = parameterField.getOpType();
	if(opType == null){
		throw new MissingAttributeException(parameterField, PMMLAttributes.PARAMETERFIELD_OPTYPE);
	}

	return FieldValueUtil.create(dataType, opType, value);
}
 
Example #6
Source File: FormulaUtil.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private MapValues createMapValues(FieldName name, Map<String, String> mapping, List<String> categories){
	Set<String> inputs = new LinkedHashSet<>(mapping.keySet());
	Set<String> outputs = new LinkedHashSet<>(mapping.values());

	for(String category : categories){

		// Assume disjoint input and output value spaces
		if(outputs.contains(category)){
			continue;
		}

		mapping.put(category, category);
	}

	return PMMLUtil.createMapValues(name, mapping);
}
 
Example #7
Source File: MultiLookupTransformer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected List<String> formatColumns(List<Feature> features){
	List<String> result = new ArrayList<>();

	for(Feature feature : features){
		FieldName name = feature.getName();

		result.add("data:" + XMLUtil.createTagName(name.getValue()));
	}

	if(result.contains("data:output")){
		throw new IllegalArgumentException();
	}

	result.add("data:output");

	return result;
}
 
Example #8
Source File: LinearSVCModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public MiningModel encodeModel(Schema schema){
	LinearSVCModel model = getTransformer();

	Transformation transformation = new AbstractTransformation(){

		@Override
		public Expression createExpression(FieldRef fieldRef){
			return PMMLUtil.createApply(PMMLFunctions.THRESHOLD)
				.addExpressions(fieldRef, PMMLUtil.createConstant(model.getThreshold()));
		}
	};

	Schema segmentSchema = schema.toAnonymousRegressorSchema(DataType.DOUBLE);

	Model linearModel = LinearModelUtil.createRegression(this, model.coefficients(), model.intercept(), segmentSchema)
		.setOutput(ModelUtil.createPredictedOutput(FieldName.create("margin"), OpType.CONTINUOUS, DataType.DOUBLE, transformation));

	return MiningModelUtil.createBinaryLogisticClassification(linearModel, 1d, 0d, RegressionModel.NormalizationMethod.NONE, false, schema);
}
 
Example #9
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
private Function<Integer, String> createIdentifierResolver(FieldName name, Table<Integer, FieldName, FieldValue> table){
	Function<Integer, String> function = new Function<Integer, String>(){

		@Override
		public String apply(Integer row){
			FieldValue value = table.get(row, name);
			if(FieldValueUtil.isMissing(value)){
				throw new MissingValueException(name);
			}

			return value.asString();
		}
	};

	return function;
}
 
Example #10
Source File: ExpressionUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void evaluateApplyJavaFunction(){
	FieldName name = FieldName.create("x");

	FieldRef fieldRef = new FieldRef(name);

	Apply apply = new Apply(EchoFunction.class.getName())
		.addExpressions(fieldRef);

	try {
		evaluate(apply);

		fail();
	} catch(EvaluationException ee){
		assertEquals(fieldRef, ee.getContext());
	}

	assertEquals("Hello World!", evaluate(apply, name, "Hello World!"));
}
 
Example #11
Source File: AssociationSchemaTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
private void evaluate(List<String> items, List<String> recommendations, List<String> exclusiveRecommendations, List<String> ruleAssociations) throws Exception {
	Evaluator evaluator = createModelEvaluator();

	checkTargetFields(Collections.emptyList(), evaluator);

	Map<FieldName, ?> arguments = createItemArguments(items);

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

	assertEquals(recommendations, getOutput(results, "Recommendation"));
	assertEquals(exclusiveRecommendations, getOutput(results, "Exclusive_Recommendation"));
	assertEquals(ruleAssociations, getOutput(results, "Rule_Association"));

	assertEquals(Iterables.getFirst(recommendations, null), getOutput(results, "Top Recommendation"));
	assertEquals(Iterables.getFirst(exclusiveRecommendations, null), getOutput(results, "Top Exclusive_Recommendation"));
	assertEquals(Iterables.getFirst(ruleAssociations, null), getOutput(results, "Top Rule_Association"));
}
 
Example #12
Source File: TargetUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public <V extends Number> Map<FieldName, ?> evaluateRegressionDefault(ValueFactory<V> valueFactory, TargetField targetField){
	Target target = targetField.getTarget();

	if(target != null && target.hasTargetValues()){
		Value<V> value = getDefaultValue(valueFactory, target);

		if(value != null){
			return evaluateRegression(targetField, value);
		}
	}

	return Collections.singletonMap(targetField.getFieldName(), null);
}
 
Example #13
Source File: FieldReferenceFinder.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Set<FieldName> getFieldNames(){

		if(this.names == null){
			return Collections.emptySet();
		}

		return Collections.unmodifiableSet(this.names);
	}
 
Example #14
Source File: PreProcessEncoder.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
private Expression encodeExpression(FieldName name, Expression expression){
	List<Double> ranges = this.ranges.get(name);
	if(ranges != null){
		Double min = ranges.get(0);
		Double max = ranges.get(1);

		if(!ValueUtil.isZero(min)){
			expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, expression, PMMLUtil.createConstant(min));
		} // End if

		if(!ValueUtil.isOne(max - min)){
			expression = PMMLUtil.createApply(PMMLFunctions.DIVIDE, expression, PMMLUtil.createConstant(max - min));
		}
	}

	Double mean = this.mean.get(name);
	if(mean != null && !ValueUtil.isZero(mean)){
		expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, expression, PMMLUtil.createConstant(mean));
	}

	Double std = this.std.get(name);
	if(std != null && !ValueUtil.isOne(std)){
		expression = PMMLUtil.createApply(PMMLFunctions.DIVIDE, expression, PMMLUtil.createConstant(std));
	}

	Double median = this.median.get(name);
	if(median != null){
		expression = PMMLUtil.createApply(PMMLFunctions.IF)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.ISNOTMISSING, new FieldRef(name)))
			.addExpressions(expression, PMMLUtil.createConstant(median));
	}

	return expression;
}
 
Example #15
Source File: BatchedPmmlInferenceObservable.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Override
public void addInput(@NonNull List<Map<FieldName, Object>> input) {
    synchronized (locker) {
        if (this.input == null)
            this.input = new ArrayList<>();
        this.input.addAll(input);
        position.set(counter.getAndIncrement());

        if (isReadLocked.get())
            realLocker.readLock().unlock();
    }
    this.input = input;
}
 
Example #16
Source File: PMMLPipeline.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private List<Feature> initFeatures(List<String> activeFields, OpType opType, DataType dataType, SkLearnEncoder encoder){
	List<Feature> result = new ArrayList<>();

	for(String activeField : activeFields){
		DataField dataField = encoder.createDataField(FieldName.create(activeField), opType, dataType);

		result.add(new WildcardFeature(encoder, dataField));
	}

	return result;
}
 
Example #17
Source File: FieldNameUtil.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static
public Set<FieldName> create(Set<FieldName> parent, String... values){
	Set<FieldName> result = new LinkedHashSet<>(parent);

	for(String value : values){
		result.add(FieldName.create(value));
	}

	return result;
}
 
Example #18
Source File: ConfigurationBuilder.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
public Configuration build(){
	Configuration configuration = new Configuration();

	ModelEvaluatorFactory modelEvaluatorFactory = getModelEvaluatorFactory();
	if(modelEvaluatorFactory == null){
		modelEvaluatorFactory = ModelEvaluatorFactory.newInstance();
	}

	configuration.setModelEvaluatorFactory(modelEvaluatorFactory);

	ValueFactoryFactory valueFactoryFactory = getValueFactoryFactory();
	if(valueFactoryFactory == null){
		valueFactoryFactory = ValueFactoryFactory.newInstance();
	}

	configuration.setValueFactoryFactory(valueFactoryFactory);

	OutputFilter outputFilter = getOutputFilter();
	if(outputFilter == null){
		outputFilter = OutputFilters.KEEP_ALL;
	}

	configuration.setOutputFilter(outputFilter);

	SymbolTable<FieldName> derivedFieldGuard = getDerivedFieldGuard();
	SymbolTable<String> functionGuard = getFunctionGuard();

	configuration.setDerivedFieldGuard(derivedFieldGuard);
	configuration.setFunctionGuard(functionGuard);

	return configuration;
}
 
Example #19
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
protected ListMultimap<FieldName, Field<?>> getVisibleFields(){

		if(this.visibleFields == null){
			this.visibleFields = collectVisibleFields();
		}

		return this.visibleFields;
	}
 
Example #20
Source File: FieldNameAdapterTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void marshal(){
	FieldNameAdapter adapter = new FieldNameAdapter();

	assertEquals(null, adapter.marshal(null));
	assertEquals("x", adapter.marshal(FieldName.create("x")));
}
 
Example #21
Source File: PredicateUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Boolean evaluate(Predicate predicate, Map<FieldName, ?> arguments){
	EvaluationContext context = new VirtualEvaluationContext();
	context.declareAll(arguments);

	return PredicateUtil.evaluate(predicate, context);
}
 
Example #22
Source File: GeneralizedLinearRegression.java    From jpmml-xgboost with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public MiningModel encodeMiningModel(List<RegTree> trees, List<Float> weights, float base_score, Integer ntreeLimit, Schema schema){
	Schema segmentSchema = schema.toAnonymousSchema();

	MiningModel miningModel = createMiningModel(trees, weights, base_score, ntreeLimit, segmentSchema)
		.setOutput(ModelUtil.createPredictedOutput(FieldName.create("xgbValue"), OpType.CONTINUOUS, DataType.FLOAT));

	return MiningModelUtil.createRegression(miningModel, RegressionModel.NormalizationMethod.EXP, schema);
}
 
Example #23
Source File: BinomialLogisticRegression.java    From jpmml-xgboost with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public MiningModel encodeMiningModel(List<RegTree> trees, List<Float> weights, float base_score, Integer ntreeLimit, Schema schema){
	Schema segmentSchema = schema.toAnonymousRegressorSchema(DataType.FLOAT);

	MiningModel miningModel = createMiningModel(trees, weights, base_score, ntreeLimit, segmentSchema)
		.setOutput(ModelUtil.createPredictedOutput(FieldName.create("xgbValue"), OpType.CONTINUOUS, DataType.FLOAT));

	return MiningModelUtil.createBinaryLogisticClassification(miningModel, 1d, 0d, RegressionModel.NormalizationMethod.LOGIT, true, schema);
}
 
Example #24
Source File: RichDataFieldTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void getValueMapping(){
	Value invalidValue = createValue("0", Value.Property.INVALID);
	Value validValueOne = createValue("1", Value.Property.VALID);
	Value validValueTwo = createValue("2", null);
	Value validValueThree = createValue("3", null);
	Value missingValue = createValue("N/A", Value.Property.MISSING);

	DataField dataField = new DataField(FieldName.create("x"), OpType.CATEGORICAL, DataType.STRING)
		.addValues(invalidValue, validValueOne, validValueTwo, validValueThree, missingValue);

	RichDataField richDataField = new RichDataField(dataField);

	Map<?, Integer> valueMap = richDataField.getMap();

	assertEquals(5, valueMap.size());

	assertEquals(FieldValue.STATUS_UNKNOWN_INVALID, valueMap.get("0"));
	assertEquals((Integer)1, valueMap.get("1"));
	assertEquals((Integer)2, valueMap.get("2"));
	assertEquals((Integer)3, valueMap.get("3"));
	assertEquals(FieldValue.STATUS_MISSING, valueMap.get("N/A"));

	dataField.setDataType(DataType.INTEGER);

	richDataField = new RichDataField(dataField);

	valueMap = richDataField.getMap();

	assertEquals(4, valueMap.size());

	assertEquals(FieldValue.STATUS_UNKNOWN_INVALID, valueMap.get(0));
	assertEquals((Integer)1, valueMap.get(1));
	assertEquals((Integer)2, valueMap.get(2));
	assertEquals((Integer)3, valueMap.get(3));
}
 
Example #25
Source File: StreamlineJPMMLModelRunner.java    From streamline with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, List<Object>> scoredTuplePerStream(Tuple input) {
    final Map<FieldName, Object> rawInputs = extractRawInputs(input);
    final Map<FieldName, FieldValue> preProcInputs = preProcessInputs(rawInputs);
    final Map<FieldName, ?> predScores = predictScores(preProcInputs);

    return toStreamLineEvents(predScores, input);
}
 
Example #26
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 #27
Source File: SparkMLEncoder.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
public DataField createDataField(FieldName name){
	StructType schema = getSchema();

	StructField field = schema.apply(name.getValue());

	org.apache.spark.sql.types.DataType sparkDataType = field.dataType();

	if(sparkDataType instanceof StringType){
		return createDataField(name, OpType.CATEGORICAL, DataType.STRING);
	} else

	if(sparkDataType instanceof IntegralType){
		return createDataField(name, OpType.CONTINUOUS, DataType.INTEGER);
	} else

	if(sparkDataType instanceof DoubleType){
		return createDataField(name, OpType.CONTINUOUS, DataType.DOUBLE);
	} else

	if(sparkDataType instanceof BooleanType){
		return createDataField(name, OpType.CATEGORICAL, DataType.BOOLEAN);
	} else

	{
		throw new IllegalArgumentException("Expected string, integral, double or boolean data type, got " + sparkDataType.typeName() + " data type");
	}
}
 
Example #28
Source File: MissingValueStrategyTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void nullPrediction() throws Exception {
	Map<FieldName, ?> arguments = createArguments("outlook", "sunny", "temperature", null, "humidity", null);

	NodeScoreDistribution<?> targetValue = evaluate(TreeModel.MissingValueStrategy.NULL_PREDICTION, arguments);

	assertNull(targetValue);
}
 
Example #29
Source File: TermFeature.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
public TermFeature(PMMLEncoder encoder, DefineFunction defineFunction, Feature feature, String value){
	super(encoder, FieldName.create(defineFunction.getName() + "(" + value + ")"), defineFunction.getDataType());

	setDefineFunction(defineFunction);

	setFeature(feature);
	setValue(value);
}
 
Example #30
Source File: ModelChainCompositionTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void evaluateSetosa() throws Exception {
	Map<FieldName, ?> result = evaluateExample(1.4, 0.2);

	assertEquals(0.3, getOutput(result, "Setosa Pollen Index"));
	assertEquals("2.1", getOutput(result, "Segment Id"));
	assertEquals("2", getOutput(result, "Class Node"));
	assertEquals("setosa", getOutput(result, "Class Score"));
	assertEquals("SE", getOutput(result, "Class Score Code"));
}