Java Code Examples for org.dmg.pmml.FieldName

The following examples show how to use org.dmg.pmml.FieldName. These examples are extracted from open source projects. 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 Project: jpmml-r   Author: jpmml   File: AdaConverter.java    License: 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 Project: jpmml-evaluator   Author: jpmml   File: ExpressionUtilTest.java    License: 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 #3
Source Project: jpmml-sklearn   Author: jpmml   File: MultiLookupTransformer.java    License: 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 #4
Source Project: jpmml-r   Author: jpmml   File: FormulaUtil.java    License: 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 #5
Source Project: jpmml-lightgbm   Author: jpmml   File: Classification.java    License: 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 #6
@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 #7
Source Project: jpmml-evaluator   Author: jpmml   File: AssociationSchemaTest.java    License: 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 #8
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 #9
Source Project: jpmml-sparkml   Author: jpmml   File: LinearSVCModelConverter.java    License: 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 #10
@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 #11
Source Project: jpmml-sparkml   Author: jpmml   File: ExpressionTranslatorTest.java    License: 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 #12
Source Project: jpmml-evaluator   Author: jpmml   File: PredicateUtilTest.java    License: 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 #13
Source Project: jpmml-r   Author: jpmml   File: ExpressionTranslatorTest.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void translateLogicalExpressionChain(){
	String string = "(x == 0) | ((x == 1) | (x == 2)) | x == 3";

	Apply left = PMMLUtil.createApply(PMMLFunctions.EQUAL)
		.addExpressions(new FieldRef(FieldName.create("x")), PMMLUtil.createConstant("0", DataType.INTEGER));

	Apply middleLeft = PMMLUtil.createApply(PMMLFunctions.EQUAL)
		.addExpressions(new FieldRef(FieldName.create("x")), PMMLUtil.createConstant("1", DataType.INTEGER));

	Apply middleRight = PMMLUtil.createApply(PMMLFunctions.EQUAL)
		.addExpressions(new FieldRef(FieldName.create("x")), PMMLUtil.createConstant("2", DataType.INTEGER));

	Apply right = PMMLUtil.createApply(PMMLFunctions.EQUAL)
		.addExpressions(new FieldRef(FieldName.create("x")), PMMLUtil.createConstant("3", DataType.INTEGER));

	Expression expected = PMMLUtil.createApply(PMMLFunctions.OR)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.OR)
			.addExpressions(left)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.OR)
				.addExpressions(middleLeft, middleRight)
			)
		)
		.addExpressions(right);

	Expression actual = ExpressionTranslator.translateExpression(string, false);

	assertTrue(ReflectionUtil.equals(expected, actual));

	expected = PMMLUtil.createApply(PMMLFunctions.OR)
		.addExpressions(left, middleLeft, middleRight, right);

	actual = ExpressionTranslator.translateExpression(string, true);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #14
private PredictorHandler(PPCell ppCell){
	setPPCell(ppCell);

	FieldName name = ppCell.getField();
	if(name == null){
		throw new MissingAttributeException(ppCell, PMMLAttributes.PPCELL_FIELD);
	}
}
 
Example #15
@Test
public void evaluate() throws Exception {
	Map<FieldName, ?> result = evaluateExample();

	assertEquals(29d, getOutput(result, "Final Score"));

	assertEquals("RC2_3", getOutput(result, "Reason Code 1"));
	assertEquals("RC1", getOutput(result, "Reason Code 2"));
	assertEquals(null, getOutput(result, "Reason Code 3"));
}
 
Example #16
Source Project: oryx   Author: OryxProject   File: RDFUpdate.java    License: Apache License 2.0 5 votes vote down vote up
private Predicate buildPredicate(Split split,
                                 CategoricalValueEncodings categoricalValueEncodings) {
  if (split == null) {
    // Left child always applies, but is evaluated second
    return new True();
  }

  int featureIndex = inputSchema.predictorToFeatureIndex(split.feature());
  FieldName fieldName = FieldName.create(inputSchema.getFeatureNames().get(featureIndex));

  if (split.featureType().equals(FeatureType.Categorical())) {
    // Note that categories in MLlib model select the *left* child but the
    // convention here will be that the predicate selects the *right* child
    // So the predicate will evaluate "not in" this set
    // More ugly casting
    @SuppressWarnings("unchecked")
    Collection<Double> javaCategories = (Collection<Double>) (Collection<?>)
        JavaConversions.seqAsJavaList(split.categories());
    Set<Integer> negativeEncodings = javaCategories.stream().map(Double::intValue).collect(Collectors.toSet());

    Map<Integer,String> encodingToValue =
        categoricalValueEncodings.getEncodingValueMap(featureIndex);
    List<String> negativeValues = negativeEncodings.stream().map(encodingToValue::get).collect(Collectors.toList());

    String joinedValues = TextUtils.joinPMMLDelimited(negativeValues);
    return new SimpleSetPredicate(fieldName,
                                  SimpleSetPredicate.BooleanOperator.IS_NOT_IN,
                                  new Array(Array.Type.STRING, joinedValues));

  } else {
    // For MLlib, left means <= threshold, so right means >
    return new SimplePredicate(fieldName,
        SimplePredicate.Operator.GREATER_THAN,
        Double.toString(split.threshold()));
  }
}
 
Example #17
Source Project: jpmml-evaluator   Author: jpmml   File: ValueParser.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public VisitorAction visit(SimplePredicate simplePredicate){
	FieldName name = simplePredicate.getField();
	if(name == null){
		throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_FIELD);
	} // End if

	if(simplePredicate.hasValue()){
		parseValue(name, simplePredicate);
	}

	return super.visit(simplePredicate);
}
 
Example #18
Source Project: konduit-serving   Author: KonduitAI   File: RegressionOutputAdapter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RegressionOutput adapt(Object input, RoutingContext routingContext) {
    if (input instanceof INDArray) {
        INDArray arr = (INDArray) input;
        return adapt(arr, routingContext);
    } else if (input instanceof List) {
        List<? extends Map<FieldName, ?>> pmmlExamples = (List<? extends Map<FieldName, ?>>) input;
        return adapt(pmmlExamples, routingContext);
    }

    throw new UnsupportedOperationException("Unable to convert input of type " + input);
}
 
Example #19
Source Project: jpmml-evaluator   Author: jpmml   File: ModelEvaluator.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
static
private <F extends ModelField> List<F> updateNames(List<F> fields, java.util.function.Function<FieldName, FieldName> mapper){

	for(F field : fields){
		FieldName name = field.getFieldName();

		FieldName mappedName = mapper.apply(name);
		if(mappedName != null && !Objects.equals(mappedName, name)){
			field.setName(mappedName);
		}
	}

	return fields;
}
 
Example #20
Source Project: jpmml-evaluator   Author: jpmml   File: ConfigurationBuilder.java    License: 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 #21
Source Project: jpmml-evaluator   Author: jpmml   File: ModelManager.java    License: 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 #22
Source Project: jpmml-model   Author: jpmml   File: FieldReferenceFinder.java    License: 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 #23
Source Project: jpmml-evaluator   Author: jpmml   File: ModelManager.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public DataField getDataField(FieldName name){

		if(Objects.equals(Evaluator.DEFAULT_TARGET_NAME, name)){
			return getDefaultDataField();
		}

		return this.dataFields.get(name);
	}
 
Example #24
Source Project: jpmml-evaluator   Author: jpmml   File: TreeModelEvaluator.java    License: 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 #25
Source Project: jpmml-evaluator   Author: jpmml   File: ExpressionUtilTest.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void evaluateTextIndexNormalization(){
	FieldName name = FieldName.create("x");

	TextIndexNormalization stepOne = new TextIndexNormalization();

	List<List<String>> cells = Arrays.asList(
		Arrays.asList("interfaces?", "interface", "true"),
		Arrays.asList("is|are|seem(ed|s?)|were", "be", "true"),
		Arrays.asList("user friendl(y|iness)", "user_friendly", "true")
	);

	stepOne.setInlineTable(createInlineTable(cells, stepOne));

	TextIndexNormalization stepTwo = new TextIndexNormalization()
		.setInField("re")
		.setOutField("feature");

	cells = Arrays.asList(
		Arrays.asList("interface be (user_friendly|well designed|excellent)", "ui_good", "true")
	);

	stepTwo.setInlineTable(createInlineTable(cells, stepTwo));

	TextIndex textIndex = new TextIndex(name, new Constant("ui_good"))
		.setLocalTermWeights(TextIndex.LocalTermWeights.BINARY)
		.setCaseSensitive(false)
		.addTextIndexNormalizations(stepOne, stepTwo);

	assertEquals(1, evaluate(textIndex, name, "Testing the app for a few days convinced me the interfaces are excellent!"));
}
 
Example #26
Source Project: jpmml-xgboost   Author: jpmml   File: LogisticRegression.java    License: 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.LOGIT, schema);
}
 
Example #27
Source Project: jpmml-model   Author: jpmml   File: FieldNameUtil.java    License: 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 #28
Source Project: jpmml-sklearn   Author: jpmml   File: PMMLPipeline.java    License: 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 #29
Source Project: konduit-serving   Author: KonduitAI   File: BatchedPmmlInferenceObservable.java    License: 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 #30
Source Project: jpmml-r   Author: jpmml   File: PreProcessEncoder.java    License: 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;
}