下面列出了怎么用org.dmg.pmml.FieldName的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Model encodeModel(Schema schema){
RGenericVector ada = getObject();
RGenericVector model = ada.getGenericElement("model");
RGenericVector trees = model.getGenericElement("trees");
RDoubleVector alpha = model.getDoubleElement("alpha");
List<TreeModel> treeModels = encodeTreeModels(trees);
MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(null))
.setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.WEIGHTED_SUM, treeModels, alpha.getValues()))
.setOutput(ModelUtil.createPredictedOutput(FieldName.create("adaValue"), OpType.CONTINUOUS, DataType.DOUBLE));
return MiningModelUtil.createBinaryLogisticClassification(miningModel, 2d, 0d, RegressionModel.NormalizationMethod.LOGIT, true, schema);
}
@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!"));
}
@Override
protected List<String> formatColumns(List<Feature> features){
List<String> result = new ArrayList<>();
for(Feature feature : features){
FieldName name = feature.getName();
result.add("data:" + XMLUtil.createTagName(name.getValue()));
}
if(result.contains("data:output")){
throw new IllegalArgumentException();
}
result.add("data:output");
return result;
}
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);
}
@Override
public Label encodeLabel(FieldName targetField, List<?> targetCategories, PMMLEncoder encoder){
DataField dataField;
if(targetCategories == null){
targetCategories = LabelUtil.createTargetCategories(this.num_class_);
dataField = encoder.createDataField(targetField, OpType.CATEGORICAL, DataType.INTEGER, targetCategories);
} else
{
if(targetCategories.size() != this.num_class_){
throw new IllegalArgumentException("Expected " + this.num_class_ + " target categories, got " + targetCategories.size() + " target categories");
}
dataField = encoder.createDataField(targetField, OpType.CATEGORICAL, DataType.STRING, targetCategories);
}
return new CategoricalLabel(dataField);
}
@Override
public FieldValue prepare(FieldName name, Object value){
ParameterField parameterField = findParameterField(name);
if(parameterField == null){
throw new MissingFieldException(name);
}
DataType dataType = parameterField.getDataType();
if(dataType == null){
throw new MissingAttributeException(parameterField, PMMLAttributes.PARAMETERFIELD_DATATYPE);
}
OpType opType = parameterField.getOpType();
if(opType == null){
throw new MissingAttributeException(parameterField, PMMLAttributes.PARAMETERFIELD_OPTYPE);
}
return FieldValueUtil.create(dataType, opType, value);
}
private void evaluate(List<String> items, List<String> recommendations, List<String> exclusiveRecommendations, List<String> ruleAssociations) throws Exception {
Evaluator evaluator = createModelEvaluator();
checkTargetFields(Collections.emptyList(), evaluator);
Map<FieldName, ?> arguments = createItemArguments(items);
Map<FieldName, ?> results = evaluator.evaluate(arguments);
assertEquals(recommendations, getOutput(results, "Recommendation"));
assertEquals(exclusiveRecommendations, getOutput(results, "Exclusive_Recommendation"));
assertEquals(ruleAssociations, getOutput(results, "Rule_Association"));
assertEquals(Iterables.getFirst(recommendations, null), getOutput(results, "Top Recommendation"));
assertEquals(Iterables.getFirst(exclusiveRecommendations, null), getOutput(results, "Top Exclusive_Recommendation"));
assertEquals(Iterables.getFirst(ruleAssociations, null), getOutput(results, "Top Rule_Association"));
}
private Function<Integer, String> createIdentifierResolver(FieldName name, Table<Integer, FieldName, FieldValue> table){
Function<Integer, String> function = new Function<Integer, String>(){
@Override
public String apply(Integer row){
FieldValue value = table.get(row, name);
if(FieldValueUtil.isMissing(value)){
throw new MissingValueException(name);
}
return value.asString();
}
};
return function;
}
@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);
}
@Test
public void defaultChildMultiplePenalties() throws Exception {
Map<FieldName, ?> arguments = createArguments("outlook", null, "temperature", null, "humidity", 70d);
NodeScoreDistribution<?> targetValue = evaluate(TreeModel.MissingValueStrategy.DEFAULT_CHILD, 0.8d, arguments);
assertEquals("3", targetValue.getEntityId());
assertEquals((Double)0.9d, targetValue.getProbability("will play"));
assertEquals((Double)0.05d, targetValue.getProbability("may play"));
assertEquals((Double)0.05d, targetValue.getProbability("no play"));
double missingValuePenalty = (0.8d * 0.8d);
assertEquals((Double)(0.9d * missingValuePenalty), targetValue.getConfidence("will play"));
assertEquals((Double)(0.05d * missingValuePenalty), targetValue.getConfidence("may play"));
assertEquals((Double)(0.05d * missingValuePenalty), targetValue.getConfidence("no play"));
}
@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);
}
static
private Boolean evaluate(Predicate predicate, Map<FieldName, ?> arguments){
EvaluationContext context = new VirtualEvaluationContext();
context.declareAll(arguments);
return PredicateUtil.evaluate(predicate, context);
}
@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));
}
private PredictorHandler(PPCell ppCell){
setPPCell(ppCell);
FieldName name = ppCell.getField();
if(name == null){
throw new MissingAttributeException(ppCell, PMMLAttributes.PPCELL_FIELD);
}
}
@Test
public void evaluate() throws Exception {
Map<FieldName, ?> result = evaluateExample();
assertEquals(29d, getOutput(result, "Final Score"));
assertEquals("RC2_3", getOutput(result, "Reason Code 1"));
assertEquals("RC1", getOutput(result, "Reason Code 2"));
assertEquals(null, getOutput(result, "Reason Code 3"));
}
private Predicate buildPredicate(Split split,
CategoricalValueEncodings categoricalValueEncodings) {
if (split == null) {
// Left child always applies, but is evaluated second
return new True();
}
int featureIndex = inputSchema.predictorToFeatureIndex(split.feature());
FieldName fieldName = FieldName.create(inputSchema.getFeatureNames().get(featureIndex));
if (split.featureType().equals(FeatureType.Categorical())) {
// Note that categories in MLlib model select the *left* child but the
// convention here will be that the predicate selects the *right* child
// So the predicate will evaluate "not in" this set
// More ugly casting
@SuppressWarnings("unchecked")
Collection<Double> javaCategories = (Collection<Double>) (Collection<?>)
JavaConversions.seqAsJavaList(split.categories());
Set<Integer> negativeEncodings = javaCategories.stream().map(Double::intValue).collect(Collectors.toSet());
Map<Integer,String> encodingToValue =
categoricalValueEncodings.getEncodingValueMap(featureIndex);
List<String> negativeValues = negativeEncodings.stream().map(encodingToValue::get).collect(Collectors.toList());
String joinedValues = TextUtils.joinPMMLDelimited(negativeValues);
return new SimpleSetPredicate(fieldName,
SimpleSetPredicate.BooleanOperator.IS_NOT_IN,
new Array(Array.Type.STRING, joinedValues));
} else {
// For MLlib, left means <= threshold, so right means >
return new SimplePredicate(fieldName,
SimplePredicate.Operator.GREATER_THAN,
Double.toString(split.threshold()));
}
}
@Override
public VisitorAction visit(SimplePredicate simplePredicate){
FieldName name = simplePredicate.getField();
if(name == null){
throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_FIELD);
} // End if
if(simplePredicate.hasValue()){
parseValue(name, simplePredicate);
}
return super.visit(simplePredicate);
}
@Override
public RegressionOutput adapt(Object input, RoutingContext routingContext) {
if (input instanceof INDArray) {
INDArray arr = (INDArray) input;
return adapt(arr, routingContext);
} else if (input instanceof List) {
List<? extends Map<FieldName, ?>> pmmlExamples = (List<? extends Map<FieldName, ?>>) input;
return adapt(pmmlExamples, routingContext);
}
throw new UnsupportedOperationException("Unable to convert input of type " + input);
}
static
private <F extends ModelField> List<F> updateNames(List<F> fields, java.util.function.Function<FieldName, FieldName> mapper){
for(F field : fields){
FieldName name = field.getFieldName();
FieldName mappedName = mapper.apply(name);
if(mappedName != null && !Objects.equals(mappedName, name)){
field.setName(mappedName);
}
}
return fields;
}
public Configuration build(){
Configuration configuration = new Configuration();
ModelEvaluatorFactory modelEvaluatorFactory = getModelEvaluatorFactory();
if(modelEvaluatorFactory == null){
modelEvaluatorFactory = ModelEvaluatorFactory.newInstance();
}
configuration.setModelEvaluatorFactory(modelEvaluatorFactory);
ValueFactoryFactory valueFactoryFactory = getValueFactoryFactory();
if(valueFactoryFactory == null){
valueFactoryFactory = ValueFactoryFactory.newInstance();
}
configuration.setValueFactoryFactory(valueFactoryFactory);
OutputFilter outputFilter = getOutputFilter();
if(outputFilter == null){
outputFilter = OutputFilters.KEEP_ALL;
}
configuration.setOutputFilter(outputFilter);
SymbolTable<FieldName> derivedFieldGuard = getDerivedFieldGuard();
SymbolTable<String> functionGuard = getFunctionGuard();
configuration.setDerivedFieldGuard(derivedFieldGuard);
configuration.setFunctionGuard(functionGuard);
return configuration;
}
protected ListMultimap<FieldName, Field<?>> getVisibleFields(){
if(this.visibleFields == null){
this.visibleFields = collectVisibleFields();
}
return this.visibleFields;
}
public Set<FieldName> getFieldNames(){
if(this.names == null){
return Collections.emptySet();
}
return Collections.unmodifiableSet(this.names);
}
public DataField getDataField(FieldName name){
if(Objects.equals(Evaluator.DEFAULT_TARGET_NAME, name)){
return getDefaultDataField();
}
return this.dataFields.get(name);
}
@Override
protected <V extends Number> Map<FieldName, ?> evaluateRegression(ValueFactory<V> valueFactory, EvaluationContext context){
TargetField targetField = getTargetField();
Trail trail = new Trail();
Node node = evaluateTree(trail, context);
if(node == null){
return TargetUtil.evaluateRegressionDefault(valueFactory, targetField);
}
NodeScore<V> result = createNodeScore(valueFactory, targetField, node);
return TargetUtil.evaluateRegression(targetField, result);
}
@Test
public void evaluateTextIndexNormalization(){
FieldName name = FieldName.create("x");
TextIndexNormalization stepOne = new TextIndexNormalization();
List<List<String>> cells = Arrays.asList(
Arrays.asList("interfaces?", "interface", "true"),
Arrays.asList("is|are|seem(ed|s?)|were", "be", "true"),
Arrays.asList("user friendl(y|iness)", "user_friendly", "true")
);
stepOne.setInlineTable(createInlineTable(cells, stepOne));
TextIndexNormalization stepTwo = new TextIndexNormalization()
.setInField("re")
.setOutField("feature");
cells = Arrays.asList(
Arrays.asList("interface be (user_friendly|well designed|excellent)", "ui_good", "true")
);
stepTwo.setInlineTable(createInlineTable(cells, stepTwo));
TextIndex textIndex = new TextIndex(name, new Constant("ui_good"))
.setLocalTermWeights(TextIndex.LocalTermWeights.BINARY)
.setCaseSensitive(false)
.addTextIndexNormalizations(stepOne, stepTwo);
assertEquals(1, evaluate(textIndex, name, "Testing the app for a few days convinced me the interfaces are excellent!"));
}
@Override
public MiningModel encodeMiningModel(List<RegTree> trees, List<Float> weights, float base_score, Integer ntreeLimit, Schema schema){
Schema segmentSchema = schema.toAnonymousSchema();
MiningModel miningModel = createMiningModel(trees, weights, base_score, ntreeLimit, segmentSchema)
.setOutput(ModelUtil.createPredictedOutput(FieldName.create("xgbValue"), OpType.CONTINUOUS, DataType.FLOAT));
return MiningModelUtil.createRegression(miningModel, RegressionModel.NormalizationMethod.LOGIT, schema);
}
static
public Set<FieldName> create(Set<FieldName> parent, String... values){
Set<FieldName> result = new LinkedHashSet<>(parent);
for(String value : values){
result.add(FieldName.create(value));
}
return result;
}
static
private List<Feature> initFeatures(List<String> activeFields, OpType opType, DataType dataType, SkLearnEncoder encoder){
List<Feature> result = new ArrayList<>();
for(String activeField : activeFields){
DataField dataField = encoder.createDataField(FieldName.create(activeField), opType, dataType);
result.add(new WildcardFeature(encoder, dataField));
}
return result;
}
@Override
public void addInput(@NonNull List<Map<FieldName, Object>> input) {
synchronized (locker) {
if (this.input == null)
this.input = new ArrayList<>();
this.input.addAll(input);
position.set(counter.getAndIncrement());
if (isReadLocked.get())
realLocker.readLock().unlock();
}
this.input = input;
}
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;
}