org.dmg.pmml.FieldRef Java Examples
The following examples show how to use
org.dmg.pmml.FieldRef.
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: ExpressionUtilTest.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 6 votes |
@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 #2
Source File: FormulaUtil.java From jpmml-r with GNU Affero General Public License v3.0 | 6 votes |
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: ExpressionTranslatorTest.java From jpmml-r with GNU Affero General Public License v3.0 | 6 votes |
@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 #4
Source File: ExpressionTranslatorTest.java From jpmml-r with GNU Affero General Public License v3.0 | 6 votes |
@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 #5
Source File: ExpressionTranslatorTest.java From jpmml-r with GNU Affero General Public License v3.0 | 6 votes |
@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 #6
Source File: ExpressionTranslatorTest.java From jpmml-r with GNU Affero General Public License v3.0 | 6 votes |
@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 #7
Source File: TfidfVectorizer.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 6 votes |
@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 #8
Source File: HingeClassification.java From jpmml-xgboost with GNU Affero General Public License v3.0 | 6 votes |
@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 #9
Source File: ExpressionTranslatorTest.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 6 votes |
@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 #10
Source File: ExpressionTranslatorTest.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 6 votes |
@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 #11
Source File: ExpressionTranslatorTest.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 6 votes |
@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 File: LinearSVCModelConverter.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 6 votes |
@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 #13
Source File: NeuralNetworkEvaluator.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
private Expression getOutputExpression(NeuralOutput neuralOutput){ DerivedField derivedField = neuralOutput.getDerivedField(); if(derivedField == null){ throw new MissingElementException(neuralOutput, PMMLElements.NEURALOUTPUT_DERIVEDFIELD); } Expression expression = ExpressionUtil.ensureExpression(derivedField); if(expression instanceof FieldRef){ FieldRef fieldRef = (FieldRef)expression; FieldName name = fieldRef.getField(); if(name == null){ throw new MissingAttributeException(fieldRef, org.dmg.pmml.PMMLAttributes.FIELDREF_FIELD); } Field<?> field = resolveField(name); if(field == null){ throw new MissingFieldException(name, fieldRef); } // End if if(field instanceof DataField){ return expression; } else if(field instanceof DerivedField){ DerivedField targetDerivedField = (DerivedField)field; Expression targetExpression = ExpressionUtil.ensureExpression(targetDerivedField); return targetExpression; } else { throw new InvalidAttributeException(fieldRef, org.dmg.pmml.PMMLAttributes.FIELDREF_FIELD, name); } } return expression; }
Example #14
Source File: ExpressionTranslatorTest.java From jpmml-r with GNU Affero General Public License v3.0 | 5 votes |
@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 #15
Source File: ExpressionUtil.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
static public FieldValue evaluateFieldRef(FieldRef fieldRef, EvaluationContext context){ FieldValue value = context.evaluate(ensureField(fieldRef)); if(FieldValueUtil.isMissing(value)){ return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, fieldRef.getMapMissingTo()); } return value; }
Example #16
Source File: ExpressionTranslatorTest.java From jpmml-r with GNU Affero General Public License v3.0 | 5 votes |
@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 #17
Source File: Formula.java From jpmml-r with GNU Affero General Public License v3.0 | 5 votes |
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 #18
Source File: PreProcessEncoder.java From jpmml-r with GNU Affero General Public License v3.0 | 5 votes |
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 #19
Source File: FunctionTransformerTest.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
static private Object evaluate(String function, Object value){ UFunc ufunc = new UFunc("numpy.core", "_ufunc_reconstruct"); ufunc.__init__(new String[]{"numpy", function}); FieldName name = FieldName.create("x"); DataType dataType; if(value instanceof Integer){ dataType = DataType.INTEGER; } else if(value instanceof Float){ dataType = DataType.FLOAT; } else { dataType = DataType.DOUBLE; } EvaluationContext context = new VirtualEvaluationContext(); context.declare(name, FieldValueUtil.create(dataType, OpType.CONTINUOUS, value)); Expression expression = UFuncUtil.encodeUFunc(ufunc, Collections.singletonList(new FieldRef(name))); FieldValue result = ExpressionUtil.evaluate(expression, context); return FieldValueUtil.getValue(result); }
Example #20
Source File: BSplineTransformer.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
/** * 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 #21
Source File: OneClassSVM.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
@Override public SupportVectorMachineModel encodeModel(Schema schema){ Transformation outlier = new OutlierTransformation(){ @Override public Expression createExpression(FieldRef fieldRef){ return PMMLUtil.createApply(PMMLFunctions.LESSOREQUAL, fieldRef, PMMLUtil.createConstant(0d)); } }; SupportVectorMachineModel supportVectorMachineModel = super.encodeModel(schema) .setOutput(ModelUtil.createPredictedOutput(FieldName.create("decisionFunction"), OpType.CONTINUOUS, DataType.DOUBLE, outlier)); Output output = supportVectorMachineModel.getOutput(); List<OutputField> outputFields = output.getOutputFields(); if(outputFields.size() != 2){ throw new IllegalArgumentException(); } OutputField decisionFunctionOutputField = outputFields.get(0); if(!decisionFunctionOutputField.isFinalResult()){ decisionFunctionOutputField.setFinalResult(true); } return supportVectorMachineModel; }
Example #22
Source File: ExpressionTranslatorTest.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 5 votes |
@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 #23
Source File: CountVectorizerModelConverter.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 5 votes |
@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 #24
Source File: ClusteringModelConverter.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 4 votes |
@Override public List<OutputField> registerOutputFields(Label label, org.dmg.pmml.Model pmmlModel, SparkMLEncoder encoder){ T model = getTransformer(); List<Integer> clusters = LabelUtil.createTargetCategories(getNumberOfClusters()); String predictionCol = model.getPredictionCol(); OutputField pmmlPredictedOutputField = ModelUtil.createPredictedField(FieldName.create("pmml(" + predictionCol + ")"), OpType.CATEGORICAL, DataType.STRING) .setFinalResult(false); DerivedOutputField pmmlPredictedField = encoder.createDerivedField(pmmlModel, pmmlPredictedOutputField, true); OutputField predictedOutputField = new OutputField(FieldName.create(predictionCol), OpType.CATEGORICAL, DataType.INTEGER) .setResultFeature(ResultFeature.TRANSFORMED_VALUE) .setExpression(new FieldRef(pmmlPredictedField.getName())); DerivedOutputField predictedField = encoder.createDerivedField(pmmlModel, predictedOutputField, true); encoder.putOnlyFeature(predictionCol, new IndexFeature(encoder, predictedField, clusters)); return Collections.emptyList(); }
Example #25
Source File: SVMConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 4 votes |
@Override public SupportVectorMachineModel encodeModel(Schema schema){ RGenericVector svm = getObject(); RDoubleVector type = svm.getDoubleElement("type"); RDoubleVector kernel = svm.getDoubleElement("kernel"); RDoubleVector degree = svm.getDoubleElement("degree"); RDoubleVector gamma = svm.getDoubleElement("gamma"); RDoubleVector coef0 = svm.getDoubleElement("coef0"); RGenericVector yScale = svm.getGenericElement("y.scale"); RIntegerVector nSv = svm.getIntegerElement("nSV"); RDoubleVector sv = svm.getDoubleElement("SV"); RDoubleVector rho = svm.getDoubleElement("rho"); RDoubleVector coefs = svm.getDoubleElement("coefs"); Type svmType = Type.values()[ValueUtil.asInt(type.asScalar())]; Kernel svmKernel = Kernel.values()[ValueUtil.asInt(kernel.asScalar())]; org.dmg.pmml.support_vector_machine.Kernel pmmlKernel = svmKernel.createKernel(degree.asScalar(), gamma.asScalar(), coef0.asScalar()); SupportVectorMachineModel supportVectorMachineModel; switch(svmType){ case C_CLASSIFICATION: case NU_CLASSIFICATION: { supportVectorMachineModel = encodeClassification(pmmlKernel, sv, nSv, rho, coefs, schema); } break; case ONE_CLASSIFICATION: { Transformation outlier = new OutlierTransformation(){ @Override public Expression createExpression(FieldRef fieldRef){ return PMMLUtil.createApply(PMMLFunctions.LESSOREQUAL, fieldRef, PMMLUtil.createConstant(0d)); } }; supportVectorMachineModel = encodeRegression(pmmlKernel, sv, rho, coefs, schema) .setOutput(ModelUtil.createPredictedOutput(FieldName.create("decisionFunction"), OpType.CONTINUOUS, DataType.DOUBLE, outlier)); if(yScale != null && yScale.size() > 0){ throw new IllegalArgumentException(); } } break; case EPS_REGRESSION: case NU_REGRESSION: { supportVectorMachineModel = encodeRegression(pmmlKernel, sv, rho, coefs, schema); if(yScale != null && yScale.size() > 0){ RDoubleVector yScaledCenter = yScale.getDoubleElement("scaled:center"); RDoubleVector yScaledScale = yScale.getDoubleElement("scaled:scale"); supportVectorMachineModel.setTargets(ModelUtil.createRescaleTargets(-1d * yScaledScale.asScalar(), yScaledCenter.asScalar(), (ContinuousLabel)schema.getLabel())); } } break; default: throw new IllegalArgumentException(); } return supportVectorMachineModel; }
Example #26
Source File: FieldReferenceFinder.java From jpmml-model with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public VisitorAction visit(FieldRef fieldRef){ process(fieldRef.getField()); return super.visit(fieldRef); }
Example #27
Source File: CountVectorizer.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
public DefineFunction encodeDefineFunction(){ String analyzer = getAnalyzer(); List<String> stopWords = getStopWords(); Object[] nGramRange = getNGramRange(); Boolean binary = getBinary(); Object preprocessor = getPreprocessor(); String stripAccents = getStripAccents(); Splitter tokenizer = getTokenizer(); switch(analyzer){ case "word": break; default: throw new IllegalArgumentException(analyzer); } if(preprocessor != null){ throw new IllegalArgumentException(); } // End if if(stripAccents != null){ throw new IllegalArgumentException(stripAccents); } 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(tokenizer.getSeparatorRE()) .setLocalTermWeights(binary ? TextIndex.LocalTermWeights.BINARY : null); if((stopWords != null && stopWords.size() > 0) && !Arrays.equals(nGramRange, new Integer[]{1, 1})){ Map<String, List<String>> data = new LinkedHashMap<>(); data.put("string", Collections.singletonList("(^|\\s+)\\p{Punct}*(" + JOINER.join(stopWords) + ")\\p{Punct}*(\\s+|$)")); data.put("stem", Collections.singletonList(" ")); data.put("regex", Collections.singletonList("true")); TextIndexNormalization textIndexNormalization = new TextIndexNormalization(null, PMMLUtil.createInlineTable(data)) .setRecursive(Boolean.TRUE); // Handles consecutive matches. See http://stackoverflow.com/a/25085385 textIndex.addTextIndexNormalizations(textIndexNormalization); } String name = functionName() + "@" + String.valueOf(CountVectorizer.SEQUENCE.getAndIncrement()); DefineFunction defineFunction = new DefineFunction(name, OpType.CONTINUOUS, DataType.DOUBLE, null, textIndex) .addParameterFields(documentField, termField); return defineFunction; }
Example #28
Source File: ExpressionUtil.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
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 #29
Source File: ExpressionUtilTest.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 3 votes |
@Test public void evaluateFieldRef(){ FieldName name = FieldName.create("x"); FieldRef fieldRef = new FieldRef(name); assertEquals("3", evaluate(fieldRef, name, "3")); assertEquals(null, evaluate(fieldRef, name, null)); fieldRef.setMapMissingTo("Missing"); assertEquals("Missing", evaluate(fieldRef, name, null)); }
Example #30
Source File: ExpressionTranslatorTest.java From jpmml-r with GNU Affero General Public License v3.0 | 2 votes |
@Test public void translateFunctionExpression(){ String string = "parent(first = child(A, log(A)), child(1 + B, right = 0), \"third\" = child(left = 0, c(A, B, C)))"; FunctionExpression functionExpression = (FunctionExpression)ExpressionTranslator.translateExpression(string); checkFunctionExpression(functionExpression, "parent", "first", null, "third"); FunctionExpression.Argument first = functionExpression.getArgument("first"); FunctionExpression.Argument second; try { second = functionExpression.getArgument("second"); fail(); } catch(IllegalArgumentException iae){ second = functionExpression.getArgument(1); } FunctionExpression.Argument third = functionExpression.getArgument("third"); assertEquals("first = child(A, log(A))", first.format()); assertEquals("child(A, log(A))", first.formatExpression()); List<Expression> expressions = checkFunctionExpression((FunctionExpression)first.getExpression(), "child", null, null); assertTrue(ReflectionUtil.equals(new FieldRef(FieldName.create("A")), expressions.get(0))); assertTrue(ReflectionUtil.equals(PMMLUtil.createApply(PMMLFunctions.LN, new FieldRef(FieldName.create("A"))), expressions.get(1))); assertEquals("child(1 + B, right = 0)", second.format()); assertEquals("child(1 + B, right = 0)", second.formatExpression()); expressions = checkFunctionExpression((FunctionExpression)second.getExpression(), "child", null, "right"); assertTrue(ReflectionUtil.equals(PMMLUtil.createApply(PMMLFunctions.ADD, PMMLUtil.createConstant("1", DataType.INTEGER), new FieldRef(FieldName.create("B"))), expressions.get(0))); assertTrue(ReflectionUtil.equals(PMMLUtil.createConstant("0", DataType.INTEGER), expressions.get(1))); assertEquals("\"third\" = child(left = 0, c(A, B, C))", third.format()); assertEquals("child(left = 0, c(A, B, C))", third.formatExpression()); expressions = checkFunctionExpression((FunctionExpression)third.getExpression(), "child", "left", null); assertTrue(ReflectionUtil.equals(PMMLUtil.createConstant("0", DataType.INTEGER), expressions.get(0))); checkFunctionExpression((FunctionExpression)expressions.get(1), "c", null, null, null); }