private static void checkDataField(DataField field, String name, Boolean categorical) { assertEquals(name, field.getName().getValue()); if (categorical == null) { assertNull(field.getOpType()); assertNull(field.getDataType()); } else if (categorical) { assertEquals(OpType.CATEGORICAL, field.getOpType()); assertEquals(DataType.STRING, field.getDataType()); } else { assertEquals(OpType.CONTINUOUS, field.getOpType()); assertEquals(DataType.DOUBLE, field.getDataType()); } }
dataType = null; DataField field = new DataField(FieldName.create(featureName), opType, dataType); if (schema.isCategorical(featureName)) { Objects.requireNonNull(categoricalValueEncodings); sorted(Comparator.comparing(Map.Entry::getKey)). map(Map.Entry::getValue). forEach(value -> field.addValues(new Value(value)));
@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()); } }
if(dataField.hasValues()){ DataType dataType = dataField.getDataType(); if(dataType == null){ throw new MissingAttributeException(dataField, PMMLAttributes.DATAFIELD_DATATYPE); HasParsedValueMapping<?> hasParsedValueMapping = (HasParsedValueMapping<?>)dataField; OpType opType = dataField.getOpType(); if(opType == null){ throw new MissingAttributeException(dataField, PMMLAttributes.DATAFIELD_OPTYPE); List<Value> pmmlValues = dataField.getValues(); for(int i = 0, max = pmmlValues.size(); i < max; i++){ Value pmmlValue = pmmlValues.get(i);
/** * @param dictionary {@link DataDictionary} from model * @return names of features in order */ public static List<String> getFeatureNames(DataDictionary dictionary) { List<DataField> dataFields = dictionary.getDataFields(); Preconditions.checkArgument(dataFields != null && !dataFields.isEmpty(), "No fields in DataDictionary"); return dataFields.stream().map(field -> field.getName().getValue()).collect(Collectors.toList()); }
@Override public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){ List<Feature> result = new ArrayList<>(); OpType opType = getOpType(); DataType dataType = getDataType(); for(Feature feature : features){ WildcardFeature wildcardFeature = (WildcardFeature)feature; DataField dataField = (DataField)encoder.getField(wildcardFeature.getName()); dataField .setOpType(opType) .setDataType(dataType); feature = new ObjectFeature(encoder, dataField.getName(), dataField.getDataType()); result.add(feature); } return super.encodeFeatures(result, encoder); } }
static public <V extends Number> Map<FieldName, ? extends Regression<V>> evaluateRegression(TargetField targetField, Regression<V> regression){ DataField dataField = targetField.getField(); regression.computeResult(dataField.getDataType()); return Collections.singletonMap(targetField.getName(), regression); }
private DataField convertColumnToDataField(ColumnConfig columnConfig) { DataField field = new DataField(); field.setName(FieldName.create(CommonUtils.getSimpleColumnName(columnConfig.getColumnName()))); field.setOptype(getOptype(columnConfig)); field.setDataType(getDataType(field.getOptype())); return field; }
@Override public VisitorAction accept(Visitor visitor) { VisitorAction status = visitor.visit(this); if (status == VisitorAction.CONTINUE) { visitor.pushParent(this); if ((status == VisitorAction.CONTINUE)&&hasExtensions()) { status = PMMLObject.traverse(visitor, getExtensions()); } if ((status == VisitorAction.CONTINUE)&&hasIntervals()) { status = PMMLObject.traverse(visitor, getIntervals()); } if ((status == VisitorAction.CONTINUE)&&hasValues()) { status = PMMLObject.traverse(visitor, getValues()); } visitor.popParent(); } if (status == VisitorAction.TERMINATE) { return VisitorAction.TERMINATE; } return VisitorAction.CONTINUE; }
public void setLabel(DataField dataField){ Label label; OpType opType = dataField.getOpType(); switch(opType){ case CATEGORICAL: label = new CategoricalLabel(dataField); break; case CONTINUOUS: label = new ContinuousLabel(dataField); break; default: throw new IllegalArgumentException(); } setLabel(label); }
@Test public void measure(){ DataField left = new DataField(null, null, null); DataField right = new DataField(FieldName.create("x"), OpType.CONTINUOUS, DataType.DOUBLE); assertEquals(getSize(left), getSize(right)); right.setName(new FieldName("x")); assertNotEquals(getSize(left), getSize(right)); }
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; }
private void renameDataField(FieldName name, FieldName renamedName){ DataField dataField = removeDataField(name); dataField.setName(renamedName); addDataField(dataField); }
boolean hasValidSpace = false; if(dataField.hasValues()){ DataType dataType = dataField.getDataType(); if(dataType == null){ throw new MissingAttributeException(dataField, PMMLAttributes.DATAFIELD_DATATYPE); HasParsedValueMapping<?> hasParsedValueMapping = (HasParsedValueMapping<?>)dataField; OpType opType = dataField.getOpType(); if(opType == null){ throw new MissingAttributeException(dataField, PMMLAttributes.DATAFIELD_OPTYPE); List<Value> pmmlValues = dataField.getValues(); for(int i = 0, max = pmmlValues.size(); i < max; i++){ Value pmmlValue = pmmlValues.get(i); if(dataField.hasIntervals()){ PMMLObject locatable = miningField; locatable = dataField; opType = dataField.getOpType();
/** * @param dictionary {@link DataDictionary} from model * @return names of features in order */ public static List<String> getFeatureNames(DataDictionary dictionary) { List<DataField> dataFields = dictionary.getDataFields(); Preconditions.checkArgument(dataFields != null && !dataFields.isEmpty(), "No fields in DataDictionary"); return dataFields.stream().map(field -> field.getName().getValue()).collect(Collectors.toList()); }
static public <V extends Number> Map<FieldName, ? extends Classification<V>> evaluateClassification(TargetField targetField, Classification<V> classification){ DataField dataField = targetField.getField(); classification.computeResult(dataField.getDataType()); return Collections.singletonMap(targetField.getName(), classification); }
@Override public VisitorAction accept(Visitor visitor) { VisitorAction status = visitor.visit(this); if (status == VisitorAction.CONTINUE) { visitor.pushParent(this); if ((status == VisitorAction.CONTINUE)&&hasExtensions()) { status = PMMLObject.traverse(visitor, getExtensions()); } if ((status == VisitorAction.CONTINUE)&&hasIntervals()) { status = PMMLObject.traverse(visitor, getIntervals()); } if ((status == VisitorAction.CONTINUE)&&hasValues()) { status = PMMLObject.traverse(visitor, getValues()); } visitor.popParent(); } if (status == VisitorAction.TERMINATE) { return VisitorAction.TERMINATE; } return VisitorAction.CONTINUE; }
public void addFeature(DataField dataField){ OpType opType = dataField.getOpType(); switch(opType){ case CONTINUOUS: addFeature(new ContinuousFeature(this, dataField)); break; case CATEGORICAL: addFeature(new CategoricalFeature(this, dataField)); break; default: throw new IllegalArgumentException(); } }