org.jpmml.converter.PMMLUtil Java Examples

The following examples show how to use org.jpmml.converter.PMMLUtil. 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: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void translate(){
	String string = "(1.0 + log(A / B)) ^ 2";

	Expression expected = PMMLUtil.createApply(PMMLFunctions.POW)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.ADD)
			.addExpressions(PMMLUtil.createConstant("1.0", DataType.DOUBLE))
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.LN)
				.addExpressions(PMMLUtil.createApply(PMMLFunctions.DIVIDE)
					.addExpressions(new FieldRef(FieldName.create("A")), new FieldRef(FieldName.create("B")))
				)
			)
		)
		.addExpressions(PMMLUtil.createConstant("2", DataType.INTEGER));

	Expression actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #2
Source File: TfidfVectorizer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public DefineFunction encodeDefineFunction(){
	TfidfTransformer transformer = getTransformer();

	DefineFunction defineFunction = super.encodeDefineFunction();

	Expression expression = defineFunction.getExpression();

	Boolean sublinearTf = transformer.getSublinearTf();
	if(sublinearTf){
		expression = PMMLUtil.createApply(PMMLFunctions.ADD, PMMLUtil.createApply(PMMLFunctions.LN, expression), PMMLUtil.createConstant(1d));
	} // End if

	Boolean useIdf = transformer.getUseIdf();
	if(useIdf){
		ParameterField weight = new ParameterField(FieldName.create("weight"));

		defineFunction.addParameterFields(weight);

		expression = PMMLUtil.createApply(PMMLFunctions.MULTIPLY, expression, new FieldRef(weight.getName()));
	}

	defineFunction.setExpression(expression);

	return defineFunction;
}
 
Example #3
Source File: ImputerUtil.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
static
public Feature encodeIndicatorFeature(Feature feature, Object missingValue, SkLearnEncoder encoder){
	Expression expression = feature.ref();

	if(missingValue != null){
		expression = PMMLUtil.createApply(PMMLFunctions.EQUAL, expression, PMMLUtil.createConstant(missingValue, feature.getDataType()));
	} else

	{
		expression = PMMLUtil.createApply(PMMLFunctions.ISMISSING, expression);
	}

	DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("missing_indicator", feature), OpType.CATEGORICAL, DataType.BOOLEAN, expression);

	return new BooleanFeature(encoder, derivedField);
}
 
Example #4
Source File: HingeClassification.java    From jpmml-xgboost with GNU Affero General Public License v3.0 6 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);

	Transformation transformation = new FunctionTransformation(PMMLFunctions.THRESHOLD){

		@Override
		public FieldName getName(FieldName name){
			return FieldName.create("hinge(" + name + ")");
		}

		@Override
		public Expression createExpression(FieldRef fieldRef){
			Apply apply = (Apply)super.createExpression(fieldRef);

			apply.addExpressions(PMMLUtil.createConstant(0f));

			return apply;
		}
	};

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

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

	Apply expected = PMMLUtil.createApply(PMMLFunctions.IF)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.ISIN)
			.addExpressions(new FieldRef(FieldName.create("status")))
			.addExpressions(PMMLUtil.createConstant(-1), PMMLUtil.createConstant(1))
		)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.NOTEQUAL)
			.addExpressions(new FieldRef(FieldName.create("x1")), PMMLUtil.createConstant(0, DataType.DOUBLE))
		)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.NOTEQUAL)
			.addExpressions(new FieldRef(FieldName.create("x2")), PMMLUtil.createConstant(0, DataType.DOUBLE))
		);

	checkExpression(expected, string);
}
 
Example #6
Source File: ExpressionTranslatorTest.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void translateCaseWhenExpression(){
	String string = "CASE WHEN x1 < 0 THEN x1 WHEN x2 > 0 THEN x2 ELSE 0 END";

	FieldRef first = new FieldRef(FieldName.create("x1"));
	FieldRef second = new FieldRef(FieldName.create("x2"));

	Constant zero = PMMLUtil.createConstant(0, DataType.DOUBLE);

	Apply expected = PMMLUtil.createApply(PMMLFunctions.IF)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.LESSTHAN)
			.addExpressions(first, zero)
		)
		.addExpressions(first)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.IF)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.GREATERTHAN)
				.addExpressions(second, zero)
			)
			.addExpressions(second)
			.addExpressions(zero)
		);

	checkExpression(expected, string);
}
 
Example #7
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 #8
Source File: SubstringTransformer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	Integer begin = getBegin();
	Integer end = getEnd();

	if((begin < 0) || (end < begin)){
		throw new IllegalArgumentException();
	}

	ClassDictUtil.checkSize(1, features);

	Feature feature = features.get(0);
	if(!(DataType.STRING).equals(feature.getDataType())){
		throw new IllegalArgumentException();
	}

	Apply apply = PMMLUtil.createApply(PMMLFunctions.SUBSTRING)
		.addExpressions(feature.ref())
		.addExpressions(PMMLUtil.createConstant(begin + 1, DataType.INTEGER), PMMLUtil.createConstant((end - begin), DataType.INTEGER));

	DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("substring", feature), OpType.CATEGORICAL, DataType.STRING, apply);

	return Collections.singletonList(new StringFeature(encoder, derivedField));
}
 
Example #9
Source File: FormulaUtil.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private Expression encodeIfElseExpression(FunctionExpression functionExpression, VariableMap expressionFields, RExpEncoder encoder){
	FunctionExpression.Argument testArgument = functionExpression.getArgument("test", 0);

	expressionFields.putAll(testArgument);

	FunctionExpression.Argument yesArgument = functionExpression.getArgument("yes", 1);
	FunctionExpression.Argument noArgument = functionExpression.getArgument("no", 2);

	expressionFields.putAll(yesArgument);
	expressionFields.putAll(noArgument);

	// XXX: "Missing values in test give missing values in the result"
	Apply apply = PMMLUtil.createApply(PMMLFunctions.IF)
		.addExpressions(prepareExpression(testArgument, expressionFields, encoder))
		.addExpressions(prepareExpression(yesArgument, expressionFields, encoder), prepareExpression(noArgument, expressionFields, encoder));

	return apply;
}
 
Example #10
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 #11
Source File: EarthConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private Apply createHingeFunction(int dir, Feature feature, double cut){
	Expression expression;

	switch(dir){
		case -1:
			expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, PMMLUtil.createConstant(cut), feature.ref());
			break;
		case 1:
			expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, feature.ref(), PMMLUtil.createConstant(cut));
			break;
		default:
			throw new IllegalArgumentException();
	}

	return PMMLUtil.createApply(PMMLFunctions.MAX, expression, PMMLUtil.createConstant(0d));
}
 
Example #12
Source File: KMeansModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public ClusteringModel encodeModel(Schema schema){
	KMeansModel model = getTransformer();

	List<Cluster> clusters = new ArrayList<>();

	Vector[] clusterCenters = model.clusterCenters();
	for(int i = 0; i < clusterCenters.length; i++){
		Cluster cluster = new Cluster(PMMLUtil.createRealArray(VectorUtil.toList(clusterCenters[i])))
			.setId(String.valueOf(i));

		clusters.add(cluster);
	}

	ComparisonMeasure comparisonMeasure = new ComparisonMeasure(ComparisonMeasure.Kind.DISTANCE, new SquaredEuclidean())
		.setCompareFunction(CompareFunction.ABS_DIFF);

	return new ClusteringModel(MiningFunction.CLUSTERING, ClusteringModel.ModelClass.CENTER_BASED, clusters.size(), ModelUtil.createMiningSchema(schema.getLabel()), comparisonMeasure, ClusteringModelUtil.createClusteringFields(schema.getFeatures()), clusters);
}
 
Example #13
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 #14
Source File: RegexTokenizerConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder){
	RegexTokenizer transformer = getTransformer();

	if(!transformer.getGaps()){
		throw new IllegalArgumentException("Expected splitter mode, got token matching mode");
	} // End if

	if(transformer.getMinTokenLength() != 1){
		throw new IllegalArgumentException("Expected 1 as minimum token length, got " + transformer.getMinTokenLength() + " as minimum token length");
	}

	Feature feature = encoder.getOnlyFeature(transformer.getInputCol());

	Field<?> field = feature.getField();

	if(transformer.getToLowercase()){
		Apply apply = PMMLUtil.createApply(PMMLFunctions.LOWERCASE, feature.ref());

		field = encoder.createDerivedField(FeatureUtil.createName("lowercase", feature), OpType.CATEGORICAL, DataType.STRING, apply);
	}

	return Collections.singletonList(new DocumentFeature(encoder, field, transformer.getPattern()));
}
 
Example #15
Source File: ReplaceTransformer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	String pattern = getPattern();
	String replacement = getReplacement();

	ClassDictUtil.checkSize(1, features);

	Feature feature = features.get(0);
	if(!(DataType.STRING).equals(feature.getDataType())){
		throw new IllegalArgumentException();
	}

	Apply apply = PMMLUtil.createApply(PMMLFunctions.REPLACE)
		.addExpressions(feature.ref())
		.addExpressions(PMMLUtil.createConstant(pattern, DataType.STRING), PMMLUtil.createConstant(replacement, DataType.STRING));

	DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("replace", feature), OpType.CATEGORICAL, DataType.STRING, apply);

	return Collections.singletonList(new StringFeature(encoder, derivedField));
}
 
Example #16
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void translateLogicalExpression(){
	String string = "a >= 0.0 & b >= 0.0 | c <= 0.0";

	Expression expected = PMMLUtil.createApply(PMMLFunctions.OR)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.AND)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.GREATEROREQUAL)
				.addExpressions(new FieldRef(FieldName.create("a")), PMMLUtil.createConstant("0.0", DataType.DOUBLE))
			)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.GREATEROREQUAL)
				.addExpressions(new FieldRef(FieldName.create("b")), PMMLUtil.createConstant("0.0", DataType.DOUBLE))
			)
		)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.LESSOREQUAL)
			.addExpressions(new FieldRef(FieldName.create("c")), PMMLUtil.createConstant("0.0", DataType.DOUBLE))
		);

	Expression actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #17
Source File: MatchesTransformer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	String pattern = getPattern();

	ClassDictUtil.checkSize(1, features);

	Feature feature = features.get(0);
	if(!(DataType.STRING).equals(feature.getDataType())){
		throw new IllegalArgumentException();
	}

	Apply apply = PMMLUtil.createApply(PMMLFunctions.MATCHES)
		.addExpressions(feature.ref())
		.addExpressions(PMMLUtil.createConstant(pattern, DataType.STRING));

	DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("matches", feature), OpType.CATEGORICAL, DataType.BOOLEAN, apply);

	return Collections.singletonList(new BooleanFeature(encoder, derivedField));
}
 
Example #18
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void translateRelationalExpression(){
	String string = "if(x < 0) \"negative\" else if(x > 0) \"positive\" else \"zero\"";

	Expression expected = PMMLUtil.createApply(PMMLFunctions.IF)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.LESSTHAN)
			.addExpressions(new FieldRef(FieldName.create("x")), PMMLUtil.createConstant("0", DataType.INTEGER))
		)
		.addExpressions(PMMLUtil.createConstant("negative", DataType.STRING))
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.IF)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.GREATERTHAN)
				.addExpressions(new FieldRef(FieldName.create("x")), PMMLUtil.createConstant("0", DataType.INTEGER))
			)
			.addExpressions(PMMLUtil.createConstant("positive", DataType.STRING))
			.addExpressions(PMMLUtil.createConstant("zero", DataType.STRING))
		);

	Expression actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #19
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void translateArithmeticExpressionChain(){
	String string = "A + B - X + C";

	Expression expected = PMMLUtil.createApply(PMMLFunctions.ADD)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.SUBTRACT)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.ADD)
				.addExpressions(new FieldRef(FieldName.create("A")), new FieldRef(FieldName.create("B")))
			)
			.addExpressions(new FieldRef(FieldName.create("X")))
		)
		.addExpressions(new FieldRef(FieldName.create("C")));

	Expression actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #20
Source File: Aggregator.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	String function = getFunction();

	if(features.size() <= 1){
		return features;
	}

	Apply apply = PMMLUtil.createApply(translateFunction(function));

	for(Feature feature : features){
		apply.addExpressions(feature.ref());
	}

	FieldName name = FeatureUtil.createName(function, features);

	DerivedField derivedField = encoder.createDerivedField(name, OpType.CONTINUOUS, DataType.DOUBLE, apply);

	return Collections.singletonList(new ContinuousFeature(encoder, derivedField));
}
 
Example #21
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void translateExponentiationExpression(){
	String string = "-2^-3";

	Expression expected = PMMLUtil.createApply(PMMLFunctions.MULTIPLY)
		.addExpressions(PMMLUtil.createConstant(-1))
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.POW)
			.addExpressions(PMMLUtil.createConstant("2", DataType.INTEGER), PMMLUtil.createConstant("-3", DataType.INTEGER))
		);

	Expression actual = ExpressionTranslator.translateExpression(string);

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

	string = "-2^-2*1.5";

	expected = PMMLUtil.createApply(PMMLFunctions.MULTIPLY)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.MULTIPLY)
			.addExpressions(PMMLUtil.createConstant(-1))
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.POW)
				.addExpressions(PMMLUtil.createConstant("2", DataType.INTEGER), PMMLUtil.createConstant("-2", DataType.INTEGER))
			)
		)
		.addExpressions(PMMLUtil.createConstant("1.5", DataType.DOUBLE));

	actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #22
Source File: CountVectorizerModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder){
	CountVectorizerModel transformer = getTransformer();

	DocumentFeature documentFeature = (DocumentFeature)encoder.getOnlyFeature(transformer.getInputCol());

	ParameterField documentField = new ParameterField(FieldName.create("document"));

	ParameterField termField = new ParameterField(FieldName.create("term"));

	TextIndex textIndex = new TextIndex(documentField.getName(), new FieldRef(termField.getName()))
		.setTokenize(Boolean.TRUE)
		.setWordSeparatorCharacterRE(documentFeature.getWordSeparatorRE())
		.setLocalTermWeights(transformer.getBinary() ? TextIndex.LocalTermWeights.BINARY : null);

	Set<DocumentFeature.StopWordSet> stopWordSets = documentFeature.getStopWordSets();
	for(DocumentFeature.StopWordSet stopWordSet : stopWordSets){

		if(stopWordSet.isEmpty()){
			continue;
		}

		String tokenRE;

		String wordSeparatorRE = documentFeature.getWordSeparatorRE();
		switch(wordSeparatorRE){
			case "\\s+":
				tokenRE = "(^|\\s+)\\p{Punct}*(" + JOINER.join(stopWordSet) + ")\\p{Punct}*(\\s+|$)";
				break;
			case "\\W+":
				tokenRE = "(\\W+)(" + JOINER.join(stopWordSet) + ")(\\W+)";
				break;
			default:
				throw new IllegalArgumentException("Expected \"\\s+\" or \"\\W+\" as splitter regex pattern, got \"" + wordSeparatorRE + "\"");
		}

		Map<String, List<String>> data = new LinkedHashMap<>();
		data.put("string", Collections.singletonList(tokenRE));
		data.put("stem", Collections.singletonList(" "));
		data.put("regex", Collections.singletonList("true"));

		TextIndexNormalization textIndexNormalization = new TextIndexNormalization(null, PMMLUtil.createInlineTable(data))
			.setCaseSensitive(stopWordSet.isCaseSensitive())
			.setRecursive(Boolean.TRUE); // Handles consecutive matches. See http://stackoverflow.com/a/25085385

		textIndex.addTextIndexNormalizations(textIndexNormalization);
	}

	DefineFunction defineFunction = new DefineFunction("tf" + "@" + String.valueOf(CountVectorizerModelConverter.SEQUENCE.getAndIncrement()), OpType.CONTINUOUS, DataType.INTEGER, null, textIndex)
		.addParameterFields(documentField, termField);

	encoder.addDefineFunction(defineFunction);

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

	String[] vocabulary = transformer.vocabulary();
	for(int i = 0; i < vocabulary.length; i++){
		String term = vocabulary[i];

		if(TermUtil.hasPunctuation(term)){
			throw new IllegalArgumentException("Punctuated vocabulary terms (" + term + ") are not supported");
		}

		result.add(new TermFeature(encoder, defineFunction, documentFeature, term));
	}

	return result;
}
 
Example #23
Source File: ExpressionTranslatorTest.java    From jpmml-r with 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 #24
Source File: MVRConverter.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
private void scaleFeatures(RExpEncoder encoder){
	RGenericVector mvr = getObject();

	RDoubleVector scale = mvr.getDoubleElement("scale", false);
	if(scale == null){
		return;
	}

	List<Feature> features = encoder.getFeatures();

	if(scale.size() != features.size()){
		throw new IllegalArgumentException();
	}

	for(int i = 0; i < features.size(); i++){
		Feature feature = features.get(i);
		Double factor = scale.getValue(i);

		if(ValueUtil.isOne(factor)){
			continue;
		}

		ContinuousFeature continuousFeature = feature.toContinuousFeature();

		Apply apply = PMMLUtil.createApply(PMMLFunctions.DIVIDE, continuousFeature.ref(), PMMLUtil.createConstant(factor));

		DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("scale", feature), OpType.CONTINUOUS, DataType.DOUBLE, apply);

		features.set(i, new ContinuousFeature(encoder, derivedField));
	}
}
 
Example #25
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 #26
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void translateParenthesizedExpression(){
	String string = "TRUE | TRUE & FALSE";

	Constant trueConstant = PMMLUtil.createConstant("true", DataType.BOOLEAN);
	Constant falseConstant = PMMLUtil.createConstant("false", DataType.BOOLEAN);

	Expression expected = PMMLUtil.createApply(PMMLFunctions.OR)
		.addExpressions(trueConstant)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.AND)
			.addExpressions(trueConstant, falseConstant)
		);

	Expression actual = ExpressionTranslator.translateExpression(string);

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

	string = "(TRUE | TRUE) & FALSE";

	expected = PMMLUtil.createApply(PMMLFunctions.AND)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.OR)
			.addExpressions(trueConstant, trueConstant)
		)
		.addExpressions(falseConstant);

	actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #27
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void translateIfExpression(){
	String string = "if(is.na(x)) TRUE else FALSE";

	Expression expected = PMMLUtil.createApply(PMMLFunctions.IF)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.ISMISSING)
			.addExpressions(new FieldRef(FieldName.create("x")))
		)
		.addExpressions(PMMLUtil.createConstant("true", DataType.BOOLEAN), PMMLUtil.createConstant("false", DataType.BOOLEAN));

	Expression actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #28
Source File: StringNormalizer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	String function = getFunction();
	Boolean trimBlanks = getTrimBlanks();

	if(function == null && !trimBlanks){
		return features;
	}

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

	for(Feature feature : features){
		Expression expression = feature.ref();

		if(function != null){
			expression = PMMLUtil.createApply(translateFunction(function), expression);
		} // End if

		if(trimBlanks){
			expression = PMMLUtil.createApply(PMMLFunctions.TRIMBLANKS, expression);
		}

		Field<?> field = encoder.toCategorical(feature.getName(), Collections.emptyList());

		// XXX: Should have been set by the previous transformer
		field.setDataType(DataType.STRING);

		DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("normalize", feature), OpType.CATEGORICAL, DataType.STRING, expression);

		feature = new StringFeature(encoder, derivedField);

		result.add(feature);
	}

	return result;
}
 
Example #29
Source File: BSplineTransformer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.BSpline.html
 */
static
private DefineFunction createBSplineFunction(BSpline bspline, SkLearnEncoder encoder){
	int k = bspline.getK();

	List<Number> c = bspline.getC();
	List<Number> t = bspline.getT();

	int n = (t.size() - k - 1);

	ParameterField paramterField = new ParameterField()
		.setName(FieldName.create("x"))
		.setOpType(OpType.CONTINUOUS)
		.setDataType(DataType.DOUBLE);

	Apply sumApply = PMMLUtil.createApply(PMMLFunctions.SUM);

	for(int i = 0; i < n; i++){

		for(int j = k; j >= 0; j--){
			createBFunction(t, i, j, encoder);
		}

		Apply apply = PMMLUtil.createApply(PMMLFunctions.MULTIPLY)
			.addExpressions(PMMLUtil.createConstant(c.get(i)))
			.addExpressions(PMMLUtil.createApply(formatBFunction(i, k), new FieldRef(paramterField.getName())));

		sumApply.addExpressions(apply);
	}

	DefineFunction defineFunction = new DefineFunction(formatBSplineFunction(k), OpType.CONTINUOUS, DataType.DOUBLE, null, sumApply)
		.addParameterFields(paramterField);

	encoder.addDefineFunction(defineFunction);

	return defineFunction;
}
 
Example #30
Source File: DiscreteDomain.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public DiscrStats createDiscrStats(DataType dataType, Object[] objects){
	List<Object> values = (List)asArray(objects[0]);
	List<Integer> counts = ValueUtil.asIntegers((List)asArray(objects[1]));

	ClassDictUtil.checkSize(values, counts);

	DiscrStats discrStats = new DiscrStats()
		.addArrays(PMMLUtil.createStringArray(standardizeValues(dataType, values)), PMMLUtil.createIntArray(counts));

	return discrStats;
}