org.dmg.pmml.PMMLFunctions Java Examples

The following examples show how to use org.dmg.pmml.PMMLFunctions. 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: 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 #2
Source File: UnsupportedMarkupInspector.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public VisitorAction visit(Apply apply){
	String function = apply.getFunction();

	switch(function){
		case PMMLFunctions.ERF:
		case PMMLFunctions.NORMALCDF:
		case PMMLFunctions.NORMALIDF:
		case PMMLFunctions.NORMALPDF:
		case PMMLFunctions.STDNORMALCDF:
		case PMMLFunctions.STDNORMALIDF:
		case PMMLFunctions.STDNORMALPDF:
			report(new UnsupportedAttributeException(apply, PMMLAttributes.APPLY_FUNCTION, function));
			break;
		default:
			break;
	}

	return super.visit(apply);
}
 
Example #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
Source File: StandardScalerTest.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void encode(){
	StandardScaler scaler = new StandardScaler("sklearn.preprocessing.data", "StandardScaler");
	scaler.put("with_mean", Boolean.FALSE);
	scaler.put("with_std", Boolean.FALSE);
	scaler.put("mean_", 6d);
	scaler.put("std_", 2d);

	assertSameFeature(scaler);

	scaler.put("with_mean", Boolean.TRUE);
	scaler.put("with_std", Boolean.TRUE);

	assertTransformedFeature(scaler, PMMLFunctions.DIVIDE);

	scaler.put("std_", 1d);

	assertTransformedFeature(scaler, PMMLFunctions.SUBTRACT);

	scaler.put("mean_", 0d);

	assertSameFeature(scaler);
}
 
Example #10
Source File: RobustScalerTest.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void encode(){
	RobustScaler scaler = new RobustScaler("sklearn.preprocessing.data", "RobustScaler");
	scaler.put("with_centering", Boolean.FALSE);
	scaler.put("with_scaling", Boolean.FALSE);
	scaler.put("center_", 6);
	scaler.put("scale_", 2);

	assertSameFeature(scaler);

	scaler.put("with_centering", Boolean.TRUE);
	scaler.put("with_scaling", Boolean.TRUE);

	assertTransformedFeature(scaler, PMMLFunctions.DIVIDE);

	scaler.put("scale_", 1);

	assertTransformedFeature(scaler, PMMLFunctions.SUBTRACT);

	scaler.put("center_", 0);

	assertSameFeature(scaler);
}
 
Example #11
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 #12
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 #13
Source File: Aggregator.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private String translateFunction(String function){

	switch(function){
		case "max":
			return PMMLFunctions.MAX;
		case "mean":
		case "avg":
			return PMMLFunctions.AVG;
		case "min":
			return PMMLFunctions.MIN;
		case "prod":
		case "product":
			return PMMLFunctions.PRODUCT;
		case "sum":
			return PMMLFunctions.SUM;
		default:
			throw new IllegalArgumentException(function);
	}
}
 
Example #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
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 #23
Source File: ExpressionTranslatorTest.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void translateLogicalExpression(){
	String string = "isnull(x1) and not(isnotnull(x2))";

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

	Apply expected = PMMLUtil.createApply(PMMLFunctions.AND)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.ISMISSING)
			.addExpressions(first)
		)
		// "not(isnotnull(..)) -> "isnull(..)"
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.ISMISSING)
			.addExpressions(second)
		);

	checkExpression(expected, string);

	string = "(x1 <= 0) or (x2 >= 0)";

	expected = PMMLUtil.createApply(PMMLFunctions.OR)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.LESSOREQUAL)
			.addExpressions(first, PMMLUtil.createConstant(0, DataType.DOUBLE))
		)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.GREATEROREQUAL)
			.addExpressions(second, PMMLUtil.createConstant(0, DataType.DOUBLE))
		);

	checkExpression(expected, string);
}
 
Example #24
Source File: Formula.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
public void addField(Field<?> field){
	RExpEncoder encoder = getEncoder();

	Feature feature = new ContinuousFeature(encoder, field);

	if(field instanceof DerivedField){
		DerivedField derivedField = (DerivedField)field;

		Expression expression = derivedField.getExpression();
		if(expression instanceof Apply){
			Apply apply = (Apply)expression;

			if(checkApply(apply, PMMLFunctions.POW, FieldRef.class, Constant.class)){
				List<Expression> expressions = apply.getExpressions();

				FieldRef fieldRef = (FieldRef)expressions.get(0);
				Constant constant = (Constant)expressions.get(1);

				try {
					String string = ValueUtil.asString(constant.getValue());

					int power = Integer.parseInt(string);

					feature = new PowerFeature(encoder, fieldRef.getField(), DataType.DOUBLE, power);
				} catch(NumberFormatException nfe){
					// Ignored
				}
			}
		}
	}

	putFeature(field.getName(), feature);

	this.fields.add(field);
}
 
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: 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 #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 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 #28
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 #29
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 #30
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;
}