@Test public void testBuildDataDictionary() { Map<Integer,Collection<String>> distinctValues = new HashMap<>(); distinctValues.put(1, Arrays.asList("one", "two", "three", "four", "five")); CategoricalValueEncodings categoricalValueEncodings = new CategoricalValueEncodings(distinctValues); DataDictionary dictionary = AppPMMLUtils.buildDataDictionary(buildTestSchema(), categoricalValueEncodings); assertEquals(4, dictionary.getNumberOfFields().intValue()); checkDataField(dictionary.getDataFields().get(0), "foo", null); checkDataField(dictionary.getDataFields().get(1), "bar", true); checkDataField(dictionary.getDataFields().get(2), "baz", null); checkDataField(dictionary.getDataFields().get(3), "bing", false); List<Value> dfValues = dictionary.getDataFields().get(1).getValues(); assertEquals(5, dfValues.size()); String[] categoricalValues = { "one", "two", "three", "four", "five" }; for (int i = 0; i < categoricalValues.length; i++) { assertEquals(categoricalValues[i], dfValues.get(i).getValue()); } }
sorted(Comparator.comparing(Map.Entry::getKey)). map(Map.Entry::getValue). forEach(value -> field.addValues(new Value(value)));
static private List<String> parseCategories(DataField dataField){ List<String> result = new ArrayList<>(); if(dataField.hasValues()){ List<Value> pmmlValues = dataField.getValues(); for(Value pmmlValue : pmmlValues){ String stringValue = pmmlValue.getValue(); if(stringValue == null){ throw new MissingAttributeException(pmmlValue, PMMLAttributes.VALUE_VALUE); } Value.Property property = pmmlValue.getProperty(); switch(property){ case VALID: result.add(stringValue); break; default: break; } } } return result; }
static private <F extends org.dmg.pmml.Field<F> & HasDiscreteDomain<F>> List<String> encodeDiscreteDomain(F field){ if(field.hasValues()){ List<Value> values = field.getValues(); return values.stream() .filter(value -> (Value.Property.VALID).equals(value.getProperty())) .map(Value::getValue) .collect(Collectors.toList()); } return Collections.emptyList(); } }
@Override public VisitorAction accept(Visitor visitor) { VisitorAction status = visitor.visit(this); if (status == VisitorAction.CONTINUE) { visitor.pushParent(this); if ((status == VisitorAction.CONTINUE)&&hasExtensions()) { status = org.dmg.pmml.PMMLObject.traverse(visitor, getExtensions()); } visitor.popParent(); } if (status == VisitorAction.TERMINATE) { return VisitorAction.TERMINATE; } return VisitorAction.CONTINUE; }
@Override public Value addExtensions(Extension... extensions) { getExtensions().addAll(Arrays.asList(extensions)); return this; }
String displayValue = value.getDisplayValue();
private Map<FieldValue, Value> parseValues(DataType dataType, OpType opType){ Map<FieldValue, Value> result = new LinkedHashMap<>(); List<Value> fieldValues = getValues(); for(Value fieldValue : fieldValues){ Value.Property property = fieldValue.getProperty(); switch(property){ case VALID: { FieldValue value = FieldValueUtil.create(dataType, opType, fieldValue.getValue()); result.put(value, fieldValue); } break; case INVALID: case MISSING: break; default: throw new UnsupportedFeatureException(fieldValue, property); } } return result; } }
@Override public VisitorAction accept(Visitor visitor) { VisitorAction status = visitor.visit(this); if (status == VisitorAction.CONTINUE) { visitor.pushParent(this); if ((status == VisitorAction.CONTINUE)&&hasExtensions()) { status = org.dmg.pmml.PMMLObject.traverse(visitor, getExtensions()); } visitor.popParent(); } if (status == VisitorAction.TERMINATE) { return VisitorAction.TERMINATE; } return VisitorAction.CONTINUE; }
@Override public Value addExtensions(Extension... extensions) { getExtensions().addAll(Arrays.asList(extensions)); return this; }
String stringValue = pmmlValue.getValue(); if(stringValue == null){ throw new MissingAttributeException(pmmlValue, PMMLAttributes.VALUE_VALUE); Value.Property property = pmmlValue.getProperty(); switch(property){ case VALID:
@Override public String getKey() { return getValue(); }
@Test public void testBuildCategoricalEncoding() { List<DataField> dataFields = new ArrayList<>(); dataFields.add(new DataField(FieldName.create("foo"), OpType.CONTINUOUS, DataType.DOUBLE)); DataField barField = new DataField(FieldName.create("bar"), OpType.CATEGORICAL, DataType.STRING); barField.addValues(new Value("b"), new Value("a")); dataFields.add(barField); DataDictionary dictionary = new DataDictionary(dataFields).setNumberOfFields(dataFields.size()); CategoricalValueEncodings encodings = AppPMMLUtils.buildCategoricalValueEncodings(dictionary); assertEquals(2, encodings.getValueCount(1)); assertEquals(0, encodings.getValueEncodingMap(1).get("b").intValue()); assertEquals(1, encodings.getValueEncodingMap(1).get("a").intValue()); assertEquals("b", encodings.getEncodingValueMap(1).get(0)); assertEquals("a", encodings.getEncodingValueMap(1).get(1)); assertEquals(Collections.singletonMap(1, 2), encodings.getCategoryCounts()); }
String stringValue = pmmlValue.getValue(); if(stringValue == null){ throw new MissingAttributeException(pmmlValue, PMMLAttributes.VALUE_VALUE); Value.Property property = pmmlValue.getProperty(); switch(property){ case VALID:
@Override public String getKey() { return getValue(); }
DataField predictor = new DataField(FieldName.create("color"), OpType.CATEGORICAL, DataType.STRING); predictor.addValues(new Value("yellow"), new Value("red")); dataFields.add(predictor); DataField target = new DataField(FieldName.create("fruit"), OpType.CATEGORICAL, DataType.STRING); target.addValues(new Value("banana"), new Value("apple")); dataFields.add(target); DataDictionary dataDictionary =
static private <F extends Field<F> & HasDiscreteDomain<F>> List<Object> parseValidValues(F field){ List<Object> result = new ArrayList<>(); DataType dataType = field.getDataType(); if(dataType == null){ throw new MissingAttributeException(MissingAttributeException.formatMessage(XPathUtil.formatElement(field.getClass()) + "@dataType"), field); } // End if if(field.hasValues()){ List<Value> pmmlValues = field.getValues(); for(Value pmmlValue : pmmlValues){ String stringValue = pmmlValue.getValue(); if(stringValue == null){ throw new MissingAttributeException(pmmlValue, PMMLAttributes.VALUE_VALUE); } Value.Property property = pmmlValue.getProperty(); switch(property){ case VALID: result.add(TypeUtil.parse(dataType, stringValue)); break; default: break; } } } return result; }
@Override public VisitorAction visit(Value pmmlValue){ Object value = values.get(pmmlValue.getValue()); if(value != null){ value = ScalarUtil.decode(value); addExtension(pmmlValue, ValueUtil.formatValue(value)); } return super.visit(pmmlValue); } };
/** * Create an instance of {@link Value } * */ public Value createValue() { return new Value(); }
if(pmmlValue != null && (Value.Property.VALID).equals(pmmlValue.getProperty())){ return pmmlValue; Value pmmlValue = pmmlValues.get(i); String stringValue = pmmlValue.getValue(); if(stringValue == null){ throw new MissingAttributeException(pmmlValue, PMMLAttributes.VALUE_VALUE); Value.Property property = pmmlValue.getProperty(); switch(property){ case VALID: