org.dmg.pmml.MiningSchema Java Examples
The following examples show how to use
org.dmg.pmml.MiningSchema.
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: TargetUtilTest.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 6 votes |
static private TreeModelEvaluator createTreeModelEvaluator(MiningFunction miningFunction, MathContext mathContext, Target target){ Node root = new LeafNode(null, False.INSTANCE); Targets targets = new Targets() .addTargets(target); TreeModel treeModel = new TreeModel(miningFunction, new MiningSchema(), root) .setSplitCharacteristic(TreeModel.SplitCharacteristic.BINARY_SPLIT) .setMathContext(mathContext) .setTargets(targets); PMML pmml = new PMML(Version.PMML_4_3.getVersion(), new Header(), new DataDictionary()) .addModels(treeModel); ModelEvaluatorBuilder modelEvaluatorBuilder = new ModelEvaluatorBuilder(pmml); return (TreeModelEvaluator)modelEvaluatorBuilder.build(); }
Example #2
Source File: KMeansPMMLUtils.java From oryx with Apache License 2.0 | 6 votes |
/** * Validates that the encoded PMML model received matches expected schema. * * @param pmml {@link PMML} encoding of KMeans Clustering * @param schema expected schema attributes of KMeans Clustering */ public static void validatePMMLVsSchema(PMML pmml, InputSchema schema) { List<Model> models = pmml.getModels(); Preconditions.checkArgument(models.size() == 1, "Should have exactly one model, but had %s", models.size()); Model model = models.get(0); Preconditions.checkArgument(model instanceof ClusteringModel); Preconditions.checkArgument(model.getMiningFunction() == MiningFunction.CLUSTERING); DataDictionary dictionary = pmml.getDataDictionary(); Preconditions.checkArgument( schema.getFeatureNames().equals(AppPMMLUtils.getFeatureNames(dictionary)), "Feature names in schema don't match names in PMML"); MiningSchema miningSchema = model.getMiningSchema(); Preconditions.checkArgument(schema.getFeatureNames().equals( AppPMMLUtils.getFeatureNames(miningSchema))); }
Example #3
Source File: AppPMMLUtilsTest.java From oryx with Apache License 2.0 | 6 votes |
@Test public void testBuildMiningSchema() { MiningSchema miningSchema = AppPMMLUtils.buildMiningSchema(buildTestSchema()); List<MiningField> miningFields = miningSchema.getMiningFields(); assertEquals(4, miningFields.size()); String[] fieldNames = { "foo", "bar", "baz", "bing" }; for (int i = 0; i < fieldNames.length; i++) { assertEquals(fieldNames[i], miningFields.get(i).getName().getValue()); } assertEquals(MiningField.UsageType.SUPPLEMENTARY, miningFields.get(0).getUsageType()); assertEquals(MiningField.UsageType.PREDICTED, miningFields.get(1).getUsageType()); assertEquals(MiningField.UsageType.SUPPLEMENTARY, miningFields.get(2).getUsageType()); assertEquals(MiningField.UsageType.ACTIVE, miningFields.get(3).getUsageType()); assertEquals(OpType.CATEGORICAL, miningFields.get(1).getOpType()); assertEquals(OpType.CONTINUOUS, miningFields.get(3).getOpType()); }
Example #4
Source File: ModelEvaluatorBuilder.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 6 votes |
protected void checkSchema(ModelEvaluator<?> modelEvaluator){ Model model = modelEvaluator.getModel(); MiningSchema miningSchema = model.getMiningSchema(); List<InputField> inputFields = modelEvaluator.getInputFields(); List<InputField> groupFields = Collections.emptyList(); if(modelEvaluator instanceof HasGroupFields){ HasGroupFields hasGroupFields = (HasGroupFields)modelEvaluator; groupFields = hasGroupFields.getGroupFields(); } // End if if((inputFields.size() + groupFields.size()) > 1000){ throw new InvalidElementException("Model has too many input fields", miningSchema); } List<TargetField> targetFields = modelEvaluator.getTargetFields(); List<OutputField> outputFields = modelEvaluator.getOutputFields(); if((targetFields.size() + outputFields.size()) < 1){ throw new InvalidElementException("Model does not have any target or output fields", miningSchema); } }
Example #5
Source File: AppPMMLUtils.java From oryx with Apache License 2.0 | 5 votes |
/** * @param schema {@link InputSchema} whose information should be encoded in PMML * @param importances optional feature importances. May be {@code null}, or else the size * of the array must match the number of predictors in the schema, which may be * less than the total number of features. * @return a {@link MiningSchema} representing the information contained in an * {@link InputSchema} */ public static MiningSchema buildMiningSchema(InputSchema schema, double[] importances) { Preconditions.checkArgument( importances == null || (importances.length == schema.getNumPredictors())); List<String> featureNames = schema.getFeatureNames(); List<MiningField> miningFields = new ArrayList<>(); for (int featureIndex = 0; featureIndex < featureNames.size(); featureIndex++) { String featureName = featureNames.get(featureIndex); MiningField field = new MiningField(FieldName.create(featureName)); if (schema.isNumeric(featureName)) { field.setOpType(OpType.CONTINUOUS); field.setUsageType(MiningField.UsageType.ACTIVE); } else if (schema.isCategorical(featureName)) { field.setOpType(OpType.CATEGORICAL); field.setUsageType(MiningField.UsageType.ACTIVE); } else { // ID, or ignored field.setUsageType(MiningField.UsageType.SUPPLEMENTARY); } if (schema.hasTarget() && schema.isTarget(featureName)) { // Override to PREDICTED field.setUsageType(MiningField.UsageType.PREDICTED); } // Will be active if and only if it's a predictor if (field.getUsageType() == MiningField.UsageType.ACTIVE && importances != null) { int predictorIndex = schema.featureToPredictorIndex(featureIndex); field.setImportance(importances[predictorIndex]); } miningFields.add(field); } return new MiningSchema(miningFields); }
Example #6
Source File: IntegrationTestBatch.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
protected void validatePMML(PMML pmml) throws Exception { List<Visitor> visitors = Arrays.<Visitor>asList( new UnsupportedMarkupInspector(), new InvalidMarkupInspector(){ @Override public VisitorAction visit(Application application){ String name = application.getName(); if(name == null){ return VisitorAction.SKIP; } return super.visit(application); } @Override public VisitorAction visit(MiningSchema miningSchema){ if(!miningSchema.hasMiningFields()){ return VisitorAction.SKIP; } return super.visit(miningSchema); } } ); for(Visitor visitor : visitors){ visitor.applyTo(pmml); } }
Example #7
Source File: ModelEvaluatorBuilderTest.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
static private ModelEvaluatorBuilder createModelEvaluatorBuilder(){ Node root = new LeafNode(null, False.INSTANCE); TreeModel treeModel = new TreeModel(MiningFunction.REGRESSION, new MiningSchema(), root); PMML pmml = new PMML() .addModels(treeModel); return new ModelEvaluatorBuilder(pmml, treeModel); }
Example #8
Source File: NodeResolverTest.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
@Test public void resolve(){ Node leftChild = new LeafNode() .setId("1"); Node rightChild = new LeafNode() .setId("2"); Node root = new BranchNode(null, True.INSTANCE) .setId("0") .setDefaultChild(rightChild.getId()) .addNodes(leftChild, rightChild); TreeModel treeModel = new TreeModel(MiningFunction.REGRESSION, new MiningSchema(), null) .setNode(root); PMML pmml = new PMML(Version.PMML_4_3.getVersion(), new Header(), new DataDictionary()) .addModels(treeModel); NodeResolver resolver = new NodeResolver(); resolver.applyTo(pmml); assertEquals(rightChild.getId(), root.getDefaultChild()); treeModel.setMissingValueStrategy(TreeModel.MissingValueStrategy.DEFAULT_CHILD); resolver.applyTo(pmml); assertSame(rightChild, root.getDefaultChild()); }
Example #9
Source File: MiningFieldInterner.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
@Override public VisitorAction visit(MiningSchema miningSchema){ if(miningSchema.hasMiningFields()){ List<MiningField> miningFields = miningSchema.getMiningFields(); for(ListIterator<MiningField> it = miningFields.listIterator(); it.hasNext(); ){ it.set(intern(it.next())); } } return super.visit(miningSchema); }
Example #10
Source File: TargetCategoryParser.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
private void processModel(Model model){ MiningSchema miningSchema = model.getMiningSchema(); if(miningSchema == null){ throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(model.getClass()) + "/" + XPathUtil.formatElement(MiningSchema.class)), model); } Map<FieldName, DataType> targetDataTypes = new LinkedHashMap<>(); if(miningSchema.hasMiningFields()){ List<MiningField> miningFields = miningSchema.getMiningFields(); for(MiningField miningField : miningFields){ FieldName name = miningField.getName(); if(name == null){ throw new MissingAttributeException(miningField, PMMLAttributes.MININGFIELD_NAME); } MiningField.UsageType usageType = miningField.getUsageType(); switch(usageType){ case PREDICTED: case TARGET: DataType dataType = resolveTargetDataType(name); targetDataTypes.put(name, dataType); break; default: break; } } } this.targetDataTypes.push(targetDataTypes); this.dataType = getDataType(); }
Example #11
Source File: ModelManager.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
protected List<InputField> createInputFields(MiningField.UsageType usageType){ M model = getModel(); MiningSchema miningSchema = model.getMiningSchema(); List<InputField> inputFields = new ArrayList<>(); if(miningSchema.hasMiningFields()){ List<MiningField> miningFields = miningSchema.getMiningFields(); for(MiningField miningField : miningFields){ FieldName name = miningField.getName(); if(!(miningField.getUsageType()).equals(usageType)){ continue; } Field<?> field = getDataField(name); if(field == null){ field = new VariableField(name); } InputField inputField = new InputField(field, miningField); inputFields.add(inputField); } } return ImmutableList.copyOf(inputFields); }
Example #12
Source File: AbstractAppMLlibIT.java From oryx with Apache License 2.0 | 5 votes |
protected static void checkMiningSchema(InputSchema schema, MiningSchema miningSchema) { assertNotNull(miningSchema); List<MiningField> miningFields = miningSchema.getMiningFields(); List<String> expectedFeatureNames = schema.getFeatureNames(); assertEquals("Wrong number of features", expectedFeatureNames.size(), miningFields.size()); for (int i = 0; i < expectedFeatureNames.size(); i++) { MiningField miningField = miningFields.get(i); String expectedFeature = expectedFeatureNames.get(i); String featureName = miningField.getName().getValue(); assertEquals("Wrong feature at position " + i, expectedFeature, featureName); if (schema.isNumeric(expectedFeature) || schema.isCategorical(expectedFeature)) { assertEquals("Wrong op type for feature + " + featureName, schema.isNumeric(expectedFeature) ? OpType.CONTINUOUS : OpType.CATEGORICAL, miningField.getOpType()); if (schema.isTarget(expectedFeature)) { assertEquals("Wrong usage type for feature " + featureName, MiningField.UsageType.PREDICTED, miningField.getUsageType()); } else { assertEquals("Wrong usage type for feature " + featureName, MiningField.UsageType.ACTIVE, miningField.getUsageType()); assertRange(miningField.getImportance().doubleValue(), 0.0, 1.0); } } else { assertEquals("Wrong usage type for feature " + featureName, MiningField.UsageType.SUPPLEMENTARY, miningField.getUsageType()); } } }
Example #13
Source File: RDFPMMLUtils.java From oryx with Apache License 2.0 | 5 votes |
/** * Validates that the encoded PMML model received matches expected schema. * * @param pmml {@link PMML} encoding of a decision forest * @param schema expected schema attributes of decision forest */ public static void validatePMMLVsSchema(PMML pmml, InputSchema schema) { List<Model> models = pmml.getModels(); Preconditions.checkArgument(models.size() == 1, "Should have exactly one model, but had %s", models.size()); Model model = models.get(0); MiningFunction function = model.getMiningFunction(); if (schema.isClassification()) { Preconditions.checkArgument(function == MiningFunction.CLASSIFICATION, "Expected classification function type but got %s", function); } else { Preconditions.checkArgument(function == MiningFunction.REGRESSION, "Expected regression function type but got %s", function); } DataDictionary dictionary = pmml.getDataDictionary(); Preconditions.checkArgument( schema.getFeatureNames().equals(AppPMMLUtils.getFeatureNames(dictionary)), "Feature names in schema don't match names in PMML"); MiningSchema miningSchema = model.getMiningSchema(); Preconditions.checkArgument(schema.getFeatureNames().equals( AppPMMLUtils.getFeatureNames(miningSchema))); Integer pmmlIndex = AppPMMLUtils.findTargetIndex(miningSchema); if (schema.hasTarget()) { int schemaIndex = schema.getTargetFeatureIndex(); Preconditions.checkArgument( pmmlIndex != null && schemaIndex == pmmlIndex, "Configured schema expects target at index %s, but PMML has target at index %s", schemaIndex, pmmlIndex); } else { Preconditions.checkArgument(pmmlIndex == null); } }
Example #14
Source File: AppPMMLUtils.java From oryx with Apache License 2.0 | 5 votes |
/** * @param miningSchema {@link MiningSchema} from a model * @return index of the {@link MiningField.UsageType#PREDICTED} feature */ public static Integer findTargetIndex(MiningSchema miningSchema) { List<MiningField> miningFields = miningSchema.getMiningFields(); for (int i = 0; i < miningFields.size(); i++) { if (miningFields.get(i).getUsageType() == MiningField.UsageType.PREDICTED) { return i; } } return null; }
Example #15
Source File: GolfingTreeModelExample.java From jpmml-model with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public PMML produce(){ FieldName temperature = FieldName.create("temperature"); FieldName humidity = FieldName.create("humidity"); FieldName windy = FieldName.create("windy"); FieldName outlook = FieldName.create("outlook"); FieldName whatIdo = FieldName.create("whatIDo"); Header header = new Header() .setCopyright("www.dmg.org") .setDescription("A very small binary tree model to show structure."); DataDictionary dataDictionary = new DataDictionary() .addDataFields( new DataField(temperature, OpType.CONTINUOUS, DataType.DOUBLE), new DataField(humidity, OpType.CONTINUOUS, DataType.DOUBLE), new DataField(windy, OpType.CATEGORICAL, DataType.STRING) .addValues(createValues("true", "false")), new DataField(outlook, OpType.CATEGORICAL, DataType.STRING) .addValues(createValues("sunny", "overcast", "rain")), new DataField(whatIdo, OpType.CATEGORICAL, DataType.STRING) .addValues(createValues("will play", "may play", "no play")) ); dataDictionary.setNumberOfFields((dataDictionary.getDataFields()).size()); MiningSchema miningSchema = new MiningSchema() .addMiningFields( new MiningField(temperature), new MiningField(humidity), new MiningField(windy), new MiningField(outlook), new MiningField(whatIdo) .setUsageType(MiningField.UsageType.TARGET) ); Node root = new BranchNode("will play", True.INSTANCE); // Upper half of the tree root.addNodes( new BranchNode("will play", new SimplePredicate(outlook, Operator.EQUAL, "sunny")) .addNodes( new BranchNode("will play", createCompoundPredicate(BooleanOperator.AND, new SimplePredicate(temperature, Operator.LESS_THAN, "90"), new SimplePredicate(temperature, Operator.GREATER_THAN, "50")) ) .addNodes( new LeafNode("will play", new SimplePredicate(humidity, Operator.LESS_THAN, "80")), new LeafNode("no play", new SimplePredicate(humidity, Operator.GREATER_OR_EQUAL, "80")) ), new LeafNode("no play", createCompoundPredicate(BooleanOperator.OR, new SimplePredicate(temperature, Operator.GREATER_OR_EQUAL, "90"), new SimplePredicate(temperature, Operator.LESS_OR_EQUAL, "50")) ) ) ); // Lower half of the tree root.addNodes( new BranchNode("may play", createCompoundPredicate(BooleanOperator.OR, new SimplePredicate(outlook, Operator.EQUAL, "overcast"), new SimplePredicate(outlook, Operator.EQUAL, "rain")) ) .addNodes( new LeafNode("may play", createCompoundPredicate(BooleanOperator.AND, new SimplePredicate(temperature, Operator.GREATER_THAN, "60"), new SimplePredicate(temperature, Operator.LESS_THAN, "100"), new SimplePredicate(outlook, Operator.EQUAL, "overcast"), new SimplePredicate(humidity, Operator.LESS_THAN, "70"), new SimplePredicate(windy, Operator.EQUAL, "false")) ), new LeafNode("no play", createCompoundPredicate(BooleanOperator.AND, new SimplePredicate(outlook, Operator.EQUAL, "rain"), new SimplePredicate(humidity, Operator.LESS_THAN, "70")) ) ) ); TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, miningSchema, root) .setModelName("golfing"); PMML pmml = new PMML(Version.PMML_4_4.getVersion(), header, dataDictionary) .addModels(treeModel); return pmml; }
Example #16
Source File: ModelManagerFactoryTest.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
@Test public void newModelManager(){ ModelManagerFactory<ModelManager<?>> modelManagerFactory = new ModelManagerFactory<ModelManager<?>>(null){ @Override public List<Class<? extends ModelManager<?>>> getServiceProviderClasses(Class<? extends Model> modelClazz){ return Arrays.asList(RegressorManager.class, ClassifierManager.class); } }; TreeModel treeModel = new TreeModel() .setMiningFunction(null) .setMiningSchema(new MiningSchema()); PMML pmml = new PMML() .setHeader(new Header()) .setDataDictionary(new DataDictionary()) .addModels(treeModel); ModelManager<?> modelManager; try { modelManager = modelManagerFactory.newModelManager(pmml, treeModel); fail(); } catch(InvalidMarkupException ime){ // Ignored } treeModel.setMiningFunction(MiningFunction.REGRESSION); modelManager = modelManagerFactory.newModelManager(pmml, treeModel); assertTrue(modelManager instanceof RegressorManager); treeModel.setMiningFunction(MiningFunction.CLASSIFICATION); modelManager = modelManagerFactory.newModelManager(pmml, treeModel); assertTrue(modelManager instanceof ClassifierManager); }
Example #17
Source File: JavaModel.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
@ValueConstructor public JavaModel(@Property("miningFunction") MiningFunction miningFunction, @Property("miningSchema") MiningSchema miningSchema){ setMiningFunction(miningFunction); setMiningSchema(miningSchema); }
Example #18
Source File: JavaModel.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
@Override public MiningSchema getMiningSchema(){ return this.miningSchema; }
Example #19
Source File: JavaModel.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
@Override public JavaModel setMiningSchema(@Property("miningSchema") MiningSchema miningSchema){ this.miningSchema = miningSchema; return this; }
Example #20
Source File: ModelManager.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
protected ModelManager(PMML pmml, M model){ setPMML(Objects.requireNonNull(pmml)); setModel(Objects.requireNonNull(model)); DataDictionary dataDictionary = pmml.getDataDictionary(); if(dataDictionary == null){ throw new MissingElementException(pmml, PMMLElements.PMML_DATADICTIONARY); } // End if if(dataDictionary.hasDataFields()){ this.dataFields = CacheUtil.getValue(dataDictionary, ModelManager.dataFieldCache); } TransformationDictionary transformationDictionary = pmml.getTransformationDictionary(); if(transformationDictionary != null && transformationDictionary.hasDerivedFields()){ this.derivedFields = CacheUtil.getValue(transformationDictionary, ModelManager.derivedFieldCache); } // End if if(transformationDictionary != null && transformationDictionary.hasDefineFunctions()){ this.defineFunctions = CacheUtil.getValue(transformationDictionary, ModelManager.defineFunctionCache); } MiningFunction miningFunction = model.getMiningFunction(); if(miningFunction == null){ throw new MissingAttributeException(MissingAttributeException.formatMessage(XPathUtil.formatElement(model.getClass()) + "@miningFunction"), model); } MiningSchema miningSchema = model.getMiningSchema(); if(miningSchema == null){ throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(model.getClass()) + "/" + XPathUtil.formatElement(MiningSchema.class)), model); } // End if if(miningSchema.hasMiningFields()){ List<MiningField> miningFields = miningSchema.getMiningFields(); for(MiningField miningField : miningFields){ FieldName name = miningField.getName(); if(name == null){ throw new MissingAttributeException(miningField, PMMLAttributes.MININGFIELD_NAME); } } this.miningFields = CacheUtil.getValue(miningSchema, ModelManager.miningFieldCache); } LocalTransformations localTransformations = model.getLocalTransformations(); if(localTransformations != null && localTransformations.hasDerivedFields()){ this.localDerivedFields = CacheUtil.getValue(localTransformations, ModelManager.localDerivedFieldCache); } Targets targets = model.getTargets(); if(targets != null && targets.hasTargets()){ this.targets = CacheUtil.getValue(targets, ModelManager.targetCache); } Output output = model.getOutput(); if(output != null && output.hasOutputFields()){ this.outputFields = CacheUtil.getValue(output, ModelManager.outputFieldCache); this.resultFeatures = CacheUtil.getValue(output, ModelManager.resultFeaturesCache); } }
Example #21
Source File: AppPMMLUtils.java From oryx with Apache License 2.0 | 4 votes |
/** * @param miningSchema {@link MiningSchema} from a model * @return names of features in order */ public static List<String> getFeatureNames(MiningSchema miningSchema) { return miningSchema.getMiningFields().stream().map(field -> field.getName().getValue()) .collect(Collectors.toList()); }
Example #22
Source File: RDFPMMLUtilsTest.java From oryx with Apache License 2.0 | 4 votes |
public static PMML buildDummyRegressionModel() { PMML pmml = PMMLUtils.buildSkeletonPMML(); List<DataField> dataFields = new ArrayList<>(); dataFields.add(new DataField(FieldName.create("foo"), OpType.CONTINUOUS, DataType.DOUBLE)); dataFields.add(new DataField(FieldName.create("bar"), OpType.CONTINUOUS, DataType.DOUBLE)); DataDictionary dataDictionary = new DataDictionary(dataFields).setNumberOfFields(dataFields.size()); pmml.setDataDictionary(dataDictionary); List<MiningField> miningFields = new ArrayList<>(); MiningField predictorMF = new MiningField(FieldName.create("foo")) .setOpType(OpType.CONTINUOUS) .setUsageType(MiningField.UsageType.ACTIVE) .setImportance(0.5); miningFields.add(predictorMF); MiningField targetMF = new MiningField(FieldName.create("bar")) .setOpType(OpType.CONTINUOUS) .setUsageType(MiningField.UsageType.PREDICTED); miningFields.add(targetMF); MiningSchema miningSchema = new MiningSchema(miningFields); double dummyCount = 2.0; Node rootNode = new ComplexNode().setId("r").setRecordCount(dummyCount).setPredicate(new True()); double halfCount = dummyCount / 2; Node left = new ComplexNode() .setId("r-") .setRecordCount(halfCount) .setPredicate(new True()) .setScore("-2.0"); Node right = new ComplexNode().setId("r+").setRecordCount(halfCount) .setPredicate(new SimplePredicate(FieldName.create("foo"), SimplePredicate.Operator.GREATER_THAN, "3.14")) .setScore("2.0"); rootNode.addNodes(right, left); TreeModel treeModel = new TreeModel(MiningFunction.REGRESSION, miningSchema, rootNode) .setSplitCharacteristic(TreeModel.SplitCharacteristic.BINARY_SPLIT) .setMissingValueStrategy(TreeModel.MissingValueStrategy.DEFAULT_CHILD) .setMiningSchema(miningSchema); pmml.addModels(treeModel); return pmml; }
Example #23
Source File: ModelManager.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
@Override public Map<FieldName, MiningField> load(MiningSchema miningSchema){ return IndexableUtil.buildMap(miningSchema.getMiningFields()); }
Example #24
Source File: RDFPMMLUtilsTest.java From oryx with Apache License 2.0 | 4 votes |
private static PMML buildDummyClassificationModel(int numTrees) { PMML pmml = PMMLUtils.buildSkeletonPMML(); List<DataField> dataFields = new ArrayList<>(); 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 = new DataDictionary(dataFields).setNumberOfFields(dataFields.size()); pmml.setDataDictionary(dataDictionary); List<MiningField> miningFields = new ArrayList<>(); MiningField predictorMF = new MiningField(FieldName.create("color")) .setOpType(OpType.CATEGORICAL) .setUsageType(MiningField.UsageType.ACTIVE) .setImportance(0.5); miningFields.add(predictorMF); MiningField targetMF = new MiningField(FieldName.create("fruit")) .setOpType(OpType.CATEGORICAL) .setUsageType(MiningField.UsageType.PREDICTED); miningFields.add(targetMF); MiningSchema miningSchema = new MiningSchema(miningFields); double dummyCount = 2.0; Node rootNode = new ComplexNode().setId("r").setRecordCount(dummyCount).setPredicate(new True()); double halfCount = dummyCount / 2; Node left = new ComplexNode().setId("r-").setRecordCount(halfCount).setPredicate(new True()); left.addScoreDistributions(new ScoreDistribution("apple", halfCount)); Node right = new ComplexNode().setId("r+").setRecordCount(halfCount) .setPredicate(new SimpleSetPredicate(FieldName.create("color"), SimpleSetPredicate.BooleanOperator.IS_NOT_IN, new Array(Array.Type.STRING, "red"))); right.addScoreDistributions(new ScoreDistribution("banana", halfCount)); rootNode.addNodes(right, left); TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, miningSchema, rootNode) .setSplitCharacteristic(TreeModel.SplitCharacteristic.BINARY_SPLIT) .setMissingValueStrategy(TreeModel.MissingValueStrategy.DEFAULT_CHILD); if (numTrees > 1) { MiningModel miningModel = new MiningModel(MiningFunction.CLASSIFICATION, miningSchema); List<Segment> segments = new ArrayList<>(); for (int i = 0; i < numTrees; i++) { segments.add(new Segment() .setId(Integer.toString(i)) .setPredicate(new True()) .setModel(treeModel) .setWeight(1.0)); } miningModel.setSegmentation( new Segmentation(Segmentation.MultipleModelMethod.WEIGHTED_MAJORITY_VOTE, segments)); pmml.addModels(miningModel); } else { pmml.addModels(treeModel); } return pmml; }
Example #25
Source File: KMeansPMMLUtilsTest.java From oryx with Apache License 2.0 | 4 votes |
public static PMML buildDummyClusteringModel() { PMML pmml = PMMLUtils.buildSkeletonPMML(); List<DataField> dataFields = new ArrayList<>(); dataFields.add(new DataField(FieldName.create("x"), OpType.CONTINUOUS, DataType.DOUBLE)); dataFields.add(new DataField(FieldName.create("y"), OpType.CONTINUOUS, DataType.DOUBLE)); DataDictionary dataDictionary = new DataDictionary(dataFields).setNumberOfFields(dataFields.size()); pmml.setDataDictionary(dataDictionary); List<MiningField> miningFields = new ArrayList<>(); MiningField xMF = new MiningField(FieldName.create("x")) .setOpType(OpType.CONTINUOUS).setUsageType(MiningField.UsageType.ACTIVE); miningFields.add(xMF); MiningField yMF = new MiningField(FieldName.create("y")) .setOpType(OpType.CONTINUOUS).setUsageType(MiningField.UsageType.ACTIVE); miningFields.add(yMF); MiningSchema miningSchema = new MiningSchema(miningFields); List<ClusteringField> clusteringFields = new ArrayList<>(); clusteringFields.add(new ClusteringField( FieldName.create("x")).setCenterField(ClusteringField.CenterField.TRUE)); clusteringFields.add(new ClusteringField( FieldName.create("y")).setCenterField(ClusteringField.CenterField.TRUE)); List<Cluster> clusters = new ArrayList<>(); clusters.add(new Cluster().setId("0").setSize(1).setArray(AppPMMLUtils.toArray(1.0, 0.0))); clusters.add(new Cluster().setId("1").setSize(2).setArray(AppPMMLUtils.toArray(2.0, -1.0))); clusters.add(new Cluster().setId("2").setSize(3).setArray(AppPMMLUtils.toArray(-1.0, 0.0))); pmml.addModels(new ClusteringModel( MiningFunction.CLUSTERING, ClusteringModel.ModelClass.CENTER_BASED, clusters.size(), miningSchema, new ComparisonMeasure(ComparisonMeasure.Kind.DISTANCE, new SquaredEuclidean()), clusteringFields, clusters)); return pmml; }
Example #26
Source File: AppPMMLUtilsTest.java From oryx with Apache License 2.0 | 4 votes |
@Test public void testListFeaturesMS() { MiningSchema miningSchema = AppPMMLUtils.buildMiningSchema(buildTestSchema()); List<String> featureNames = AppPMMLUtils.getFeatureNames(miningSchema); assertEquals(Arrays.asList("foo", "bar", "baz", "bing"), featureNames); }
Example #27
Source File: HasNodeRegistryTest.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
@Test public void getPath(){ Node node1a = new BranchNode(); Node node2a = new BranchNode(); Node node2b = new BranchNode(); node1a.addNodes(node2a, node2b); Node node3a = new BranchNode(); Node node3b = new BranchNode(); node2a.addNodes(node3a, node3b); Node node3c = new LeafNode(); Node node3d = new LeafNode(); node2b.addNodes(node3c, node3d); PMML pmml = new PMML(Version.PMML_4_3.getVersion(), new Header(), new DataDictionary()) .addModels(new TreeModel(MiningFunction.REGRESSION, new MiningSchema(), node1a)); HasNodeRegistry hasNodeRegistry = new TreeModelEvaluator(pmml); BiMap<Node, String> nodeRegistry = (hasNodeRegistry.getEntityRegistry()).inverse(); String id1a = nodeRegistry.get(node1a); String id2a = nodeRegistry.get(node2a); String id2b = nodeRegistry.get(node2b); String id3a = nodeRegistry.get(node3a); String id3b = nodeRegistry.get(node3b); String id3c = nodeRegistry.get(node3c); String id3d = nodeRegistry.get(node3d); assertEquals(Arrays.asList(node1a), hasNodeRegistry.getPath(id1a)); assertEquals(Arrays.asList(node1a, node2a), hasNodeRegistry.getPath(id2a)); assertEquals(Arrays.asList(node1a, node2a, node3a), hasNodeRegistry.getPath(id3a)); assertEquals(Arrays.asList(node1a), hasNodeRegistry.getPathBetween(id1a, id1a)); assertEquals(Arrays.asList(node1a, node2a), hasNodeRegistry.getPathBetween(id1a, id2a)); assertNull(hasNodeRegistry.getPathBetween(id2a, id1a)); assertEquals(Arrays.asList(node2a, node3a), hasNodeRegistry.getPathBetween(id2a, id3a)); assertEquals(Arrays.asList(node2a, node3b), hasNodeRegistry.getPathBetween(id2a, id3b)); assertNull(hasNodeRegistry.getPathBetween(id2a, id2b)); assertNull(hasNodeRegistry.getPathBetween(id2a, id3c)); assertNull(hasNodeRegistry.getPathBetween(id2a, id3d)); }
Example #28
Source File: ValueParserTest.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
@Test public void parseRegressionModel(){ Value falseValue = new Value("false"); Value trueValue = new Value("true"); Value invalidValue = new Value("N/A"); DataField dataField = new DataField(FieldName.create("x1"), OpType.CATEGORICAL, DataType.STRING) .addValues(falseValue, trueValue, invalidValue); DataDictionary dataDictionary = new DataDictionary() .addDataFields(dataField); CategoricalPredictor falseTerm = new CategoricalPredictor(dataField.getName(), "false", -1d); CategoricalPredictor trueTerm = new CategoricalPredictor(dataField.getName(), "true", 1d); RegressionTable regressionTable = new RegressionTable() .addCategoricalPredictors(falseTerm, trueTerm); MiningField miningField = new MiningField(dataField.getName()) .setMissingValueReplacement("false") .setInvalidValueReplacement("N/A"); MiningSchema miningSchema = new MiningSchema() .addMiningFields(miningField); RegressionModel regressionModel = new RegressionModel(MiningFunction.REGRESSION, miningSchema, null) .addRegressionTables(regressionTable); PMML pmml = new PMML(Version.PMML_4_3.getVersion(), new Header(), dataDictionary) .addModels(regressionModel); List<DataField> dataFields = dataDictionary.getDataFields(); ValueParser parser = new ValueParser(ValueParser.Mode.STRICT); parser.applyTo(pmml); dataField = dataFields.get(0); assertEquals("false", falseValue.getValue()); assertEquals("true", trueValue.getValue()); assertEquals("N/A", invalidValue.getValue()); assertEquals("false", falseTerm.getValue()); assertEquals("true", trueTerm.getValue()); assertEquals("false", miningField.getMissingValueReplacement()); assertEquals("N/A", miningField.getInvalidValueReplacement()); dataField.setDataType(DataType.BOOLEAN); parser.applyTo(pmml); assertEquals(Boolean.FALSE, falseValue.getValue()); assertEquals(Boolean.TRUE, trueValue.getValue()); assertEquals("N/A", invalidValue.getValue()); assertEquals(Boolean.FALSE, falseTerm.getValue()); assertEquals(Boolean.TRUE, trueTerm.getValue()); assertEquals(Boolean.FALSE, miningField.getMissingValueReplacement()); assertEquals("N/A", miningField.getInvalidValueReplacement()); }
Example #29
Source File: TreePathFinderTest.java From jpmml-model with BSD 3-Clause "New" or "Revised" License | 3 votes |
@Test public void find(){ Node node1a = new BranchNode(); Node node2a = new LeafNode(); Node node2b = new BranchNode(); Node node2c = new BranchNode(); node1a.addNodes(node2a, node2b, node2c); Node node3a = new BranchNode(); Node node3b = new LeafNode(); node2b.addNodes(node3a); node2c.addNodes(node3b); Node node4a = new LeafNode(); node3a.addNodes(node4a); TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, new MiningSchema(), node1a); TreePathFinder finder = new TreePathFinder(); finder.applyTo(treeModel); Map<Node, List<Node>> paths = finder.getPaths(); assertEquals(3, paths.size()); assertEquals(Arrays.asList(node1a, node2a), paths.get(node2a)); assertEquals(Arrays.asList(node1a, node2b, node3a, node4a), paths.get(node4a)); assertEquals(Arrays.asList(node1a, node2c, node3b), paths.get(node3b)); }
Example #30
Source File: ModelManager.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 3 votes |
protected EvaluationException createMiningSchemaException(String message){ M model = getModel(); MiningSchema miningSchema = model.getMiningSchema(); return new EvaluationException(message, miningSchema); }