org.dmg.pmml.Apply Java Examples

The following examples show how to use org.dmg.pmml.Apply. 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-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 #2
Source File: FormulaUtil.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private FieldName prepareInputField(FunctionExpression.Argument argument, OpType opType, DataType dataType, RExpEncoder encoder){
	Expression expression = argument.getExpression();

	if(expression instanceof FieldRef){
		FieldRef fieldRef = (FieldRef)expression;

		return fieldRef.getField();
	} else

	if(expression instanceof Apply){
		Apply apply = (Apply)expression;

		DerivedField derivedField = encoder.createDerivedField(FieldName.create((argument.formatExpression()).trim()), opType, dataType, apply);

		return derivedField.getName();
	} else

	{
		throw new IllegalArgumentException();
	}
}
 
Example #3
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 #4
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 #5
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 #6
Source File: Formula.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private boolean checkApply(Apply apply, String function, Class<? extends Expression>... expressionClazzes){

	if((function).equals(apply.getFunction())){
		List<Expression> expressions = apply.getExpressions();

		if(expressionClazzes.length == expressions.size()){

			for(int i = 0; i < expressionClazzes.length; i++){
				Class<? extends Expression> expressionClazz = expressionClazzes[i];
				Expression expression = expressions.get(i);

				if(!(expressionClazz).isInstance(expression)){
					return false;
				}
			}

			return true;
		}
	}

	return false;
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
Source File: VersionInspector.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public VisitorAction visit(Apply apply){
	String function = apply.getFunction();

	Version version = VersionInspector.functionVersions.get(function);
	if(version != null){
		updateMinimum(version);
	}

	return super.visit(apply);
}
 
Example #21
Source File: TfidfVectorizer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Apply encodeApply(String function, Feature feature, int index, String term){
	TfidfTransformer transformer = getTransformer();

	Apply apply = super.encodeApply(function, feature, index, term);

	Boolean useIdf = transformer.getUseIdf();
	if(useIdf){
		Number weight = transformer.getWeight(index);

		apply.addExpressions(PMMLUtil.createConstant(weight));
	}

	return apply;
}
 
Example #22
Source File: ExpressionFilterer.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public VisitorAction visit(Apply apply){

	if(apply.hasExpressions()){
		filterAll(apply.getExpressions());
	}

	return super.visit(apply);
}
 
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: TermFeature.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
public Apply createApply(){
	DefineFunction defineFunction = getDefineFunction();
	Feature feature = getFeature();
	String value = getValue();

	Constant constant = PMMLUtil.createConstant(value, DataType.STRING);

	return PMMLUtil.createApply(defineFunction.getName(), feature.ref(), constant);
}
 
Example #25
Source File: WeightedTermFeature.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Apply createApply(){
	Number weight = getWeight();

	Apply apply = super.createApply()
		.addExpressions(PMMLUtil.createConstant(weight));

	return apply;
}
 
Example #26
Source File: ExpressionUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
FieldValue evaluateExpression(Expression expression, EvaluationContext context){

	if(expression instanceof Constant){
		return evaluateConstant((Constant)expression);
	} else

	if(expression instanceof FieldRef){
		return evaluateFieldRef((FieldRef)expression, context);
	} else

	if(expression instanceof NormContinuous){
		return evaluateNormContinuous((NormContinuous)expression, context);
	} else

	if(expression instanceof NormDiscrete){
		return evaluateNormDiscrete((NormDiscrete)expression, context);
	} else

	if(expression instanceof Discretize){
		return evaluateDiscretize((Discretize)expression, context);
	} else

	if(expression instanceof MapValues){
		return evaluateMapValues((MapValues)expression, context);
	} else

	if(expression instanceof TextIndex){
		return evaluateTextIndex((TextIndex)expression, context);
	} else

	if(expression instanceof Apply){
		return evaluateApply((Apply)expression, context);
	} else

	if(expression instanceof Aggregate){
		return evaluateAggregate((Aggregate)expression, context);
	} // End if

	if(expression instanceof JavaExpression){
		return evaluateJavaExpression((JavaExpression)expression, context);
	}

	throw new UnsupportedElementException(expression);
}
 
Example #27
Source File: BSplineTransformer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	BSpline bspline = getBSpline();

	ClassDictUtil.checkSize(1, features);

	Feature feature = features.get(0);

	ContinuousFeature continuousFeature = feature.toContinuousFeature();

	DefineFunction defineFunction = createBSplineFunction(bspline, encoder);

	Apply apply = PMMLUtil.createApply(defineFunction.getName())
		.addExpressions(continuousFeature.ref());

	DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("bspline", feature), apply);

	return Collections.singletonList(new ContinuousFeature(encoder, derivedField));
}
 
Example #28
Source File: ScalerTest.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
void assertTransformedFeature(Transformer transformer, String function){
	SkLearnEncoder encoder = new SkLearnEncoder();

	DataField dataField = encoder.createDataField(FieldName.create("x"));

	Feature inputFeature = new WildcardFeature(encoder, dataField);
	Feature outputFeature = Iterables.getOnlyElement(transformer.encodeFeatures(Collections.singletonList(inputFeature), encoder));

	assertNotSame(inputFeature, outputFeature);

	DerivedField derivedField = (DerivedField)encoder.getField(outputFeature.getName());

	Apply apply = (Apply)derivedField.getExpression();

	assertEquals(function, apply.getFunction());
}
 
Example #29
Source File: ConcatTransformer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	String separator = getSeparator();

	Apply apply = PMMLUtil.createApply(PMMLFunctions.CONCAT);

	List<Expression> expressions = apply.getExpressions();

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

		if((i > 0) && !("").equals(separator)){
			expressions.add(PMMLUtil.createConstant(separator, DataType.STRING));
		}

		expressions.add(feature.ref());
	}

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

	return Collections.singletonList(new StringFeature(encoder, derivedField));
}
 
Example #30
Source File: LabelBinarizer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	List<?> classes = getClasses();

	Number negLabel = getNegLabel();
	Number posLabel = getPosLabel();

	ClassDictUtil.checkSize(1, features);

	Feature feature = features.get(0);

	List<Object> categories = new ArrayList<>();
	categories.addAll(classes);

	List<Number> labelCategories = new ArrayList<>();
	labelCategories.add(negLabel);
	labelCategories.add(posLabel);

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

	classes = prepareClasses(classes);

	for(int i = 0; i < classes.size(); i++){
		Object value = classes.get(i);

		if(ValueUtil.isZero(negLabel) && ValueUtil.isOne(posLabel)){
			result.add(new BinaryFeature(encoder, feature, value));
		} else

		{
			// "($name == value) ? pos_label : neg_label"
			Apply apply = PMMLUtil.createApply(PMMLFunctions.IF)
				.addExpressions(PMMLUtil.createApply(PMMLFunctions.EQUAL, feature.ref(), PMMLUtil.createConstant(value, feature.getDataType())))
				.addExpressions(PMMLUtil.createConstant(posLabel), PMMLUtil.createConstant(negLabel));

			FieldName name = (classes.size() > 1 ? FeatureUtil.createName("label_binarizer", feature, i) : FeatureUtil.createName("label_binarizer", feature));

			DerivedField derivedField = encoder.createDerivedField(name, apply);

			result.add(new CategoricalFeature(encoder, derivedField, labelCategories));
		}
	}

	encoder.toCategorical(feature.getName(), categories);

	return result;
}