Java Code Examples for org.dmg.pmml.PMML#addModels()

The following examples show how to use org.dmg.pmml.PMML#addModels() . 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: AppPMMLUtilsTest.java    From oryx with Apache License 2.0 5 votes vote down vote up
private static PMML buildDummyModel() {
  Node node = new CountingLeafNode().setRecordCount(123.0);
  TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, null, node);
  PMML pmml = PMMLUtils.buildSkeletonPMML();
  pmml.addModels(treeModel);
  return pmml;
}
 
Example 2
Source File: KMeansUpdate.java    From oryx with Apache License 2.0 5 votes vote down vote up
/**
 * @param model {@link KMeansModel} to translate to PMML
 * @return PMML representation of a KMeans cluster model
 */
private PMML kMeansModelToPMML(KMeansModel model, Map<Integer,Long> clusterSizesMap) {
  ClusteringModel clusteringModel = pmmlClusteringModel(model, clusterSizesMap);
  PMML pmml = PMMLUtils.buildSkeletonPMML();
  pmml.setDataDictionary(AppPMMLUtils.buildDataDictionary(inputSchema, null));
  pmml.addModels(clusteringModel);
  return pmml;
}
 
Example 3
Source File: PMMLUtilsTest.java    From oryx with Apache License 2.0 5 votes vote down vote up
public static PMML buildDummyModel() {
  Node node = new CountingLeafNode().setRecordCount(123.0);
  TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, null, node);
  PMML pmml = PMMLUtils.buildSkeletonPMML();
  pmml.addModels(treeModel);
  return pmml;
}
 
Example 4
Source File: ReflectionUtilTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void copyState(){
	PMML pmml = new PMML(Version.PMML_4_4.getVersion(), new Header(), new DataDictionary());

	// Initialize a live list instance
	pmml.getModels();

	CustomPMML customPmml = new CustomPMML();

	ReflectionUtil.copyState(pmml, customPmml);

	assertSame(pmml.getVersion(), customPmml.getVersion());
	assertSame(pmml.getHeader(), customPmml.getHeader());
	assertSame(pmml.getDataDictionary(), customPmml.getDataDictionary());

	assertFalse(pmml.hasModels());
	assertFalse(customPmml.hasModels());

	pmml.addModels(new RegressionModel());

	assertTrue(pmml.hasModels());
	assertTrue(customPmml.hasModels());

	assertSame(pmml.getModels(), customPmml.getModels());

	try {
		ReflectionUtil.copyState(customPmml, pmml);

		fail();
	} catch(IllegalArgumentException iae){
		// Ignored
	}
}
 
Example 5
Source File: VersionInspectorTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void inspectTypeAnnotations(){
	PMML pmml = createPMML();

	assertVersionRange(pmml, Version.PMML_3_0, Version.PMML_4_4);

	pmml.addModels(new AssociationModel(),
		//new ClusteringModel(),
		//new GeneralRegressionModel(),
		//new MiningModel(),
		new NaiveBayesModel(),
		new NeuralNetwork(),
		new RegressionModel(),
		new RuleSetModel(),
		new SequenceModel(),
		//new SupportVectorMachineModel(),
		new TextModel(),
		new TreeModel());

	assertVersionRange(pmml, Version.PMML_3_0, Version.PMML_4_4);

	pmml.addModels(new TimeSeriesModel());

	assertVersionRange(pmml, Version.PMML_4_0, Version.PMML_4_4);

	pmml.addModels(new BaselineModel(),
		new Scorecard(),
		new NearestNeighborModel());

	assertVersionRange(pmml, Version.PMML_4_1, Version.PMML_4_4);

	pmml.addModels(new BayesianNetworkModel(),
		new GaussianProcessModel());

	assertVersionRange(pmml, Version.PMML_4_3, Version.PMML_4_4);
}
 
Example 6
Source File: MarshallerTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void marshal() throws Exception {
	PMML pmml = new PMML(Version.PMML_4_4.getVersion(), new Header(), new DataDictionary());

	RegressionModel regressionModel = new RegressionModel()
		.addRegressionTables(new RegressionTable());

	pmml.addModels(regressionModel);

	JAXBContext context = JAXBContextFactory.createContext(new Class[]{org.dmg.pmml.ObjectFactory.class, org.dmg.pmml.regression.ObjectFactory.class}, null);

	Marshaller marshaller = context.createMarshaller();

	String string;

	try(ByteArrayOutputStream os = new ByteArrayOutputStream()){
		marshaller.marshal(pmml, os);

		string = os.toString("UTF-8");
	}

	assertTrue(string.contains("<PMML xmlns=\"http://www.dmg.org/PMML-4_4\""));
	assertTrue(string.contains(" version=\"4.4\">"));
	assertTrue(string.contains("<RegressionModel>"));
	assertTrue(string.contains("</RegressionModel>"));
	assertTrue(string.contains("</PMML>"));
}
 
Example 7
Source File: KMeansPMMLUtilsTest.java    From oryx with Apache License 2.0 4 votes vote down vote up
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 8
Source File: RDFPMMLUtilsTest.java    From oryx with Apache License 2.0 4 votes vote down vote up
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 9
Source File: RDFPMMLUtilsTest.java    From oryx with Apache License 2.0 4 votes vote down vote up
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 10
Source File: RDFUpdate.java    From oryx with Apache License 2.0 4 votes vote down vote up
private PMML rdfModelToPMML(RandomForestModel rfModel,
                            CategoricalValueEncodings categoricalValueEncodings,
                            int maxDepth,
                            int maxSplitCandidates,
                            String impurity,
                            List<? extends IntLongMap> nodeIDCounts,
                            IntLongMap predictorIndexCounts) {

  boolean classificationTask = rfModel.algo().equals(Algo.Classification());
  Preconditions.checkState(classificationTask == inputSchema.isClassification());

  DecisionTreeModel[] trees = rfModel.trees();

  Model model;
  if (trees.length == 1) {
    model = toTreeModel(trees[0], categoricalValueEncodings, nodeIDCounts.get(0));
  } else {
    MiningModel miningModel = new MiningModel();
    model = miningModel;
    Segmentation.MultipleModelMethod multipleModelMethodType = classificationTask ?
        Segmentation.MultipleModelMethod.WEIGHTED_MAJORITY_VOTE :
        Segmentation.MultipleModelMethod.WEIGHTED_AVERAGE;
    List<Segment> segments = new ArrayList<>(trees.length);
    for (int treeID = 0; treeID < trees.length; treeID++) {
      TreeModel treeModel =
          toTreeModel(trees[treeID], categoricalValueEncodings, nodeIDCounts.get(treeID));
      segments.add(new Segment()
           .setId(Integer.toString(treeID))
           .setPredicate(new True())
           .setModel(treeModel)
           .setWeight(1.0)); // No weights in MLlib impl now
    }
    miningModel.setSegmentation(new Segmentation(multipleModelMethodType, segments));
  }

  model.setMiningFunction(classificationTask ?
                          MiningFunction.CLASSIFICATION :
                          MiningFunction.REGRESSION);

  double[] importances = countsToImportances(predictorIndexCounts);
  model.setMiningSchema(AppPMMLUtils.buildMiningSchema(inputSchema, importances));
  DataDictionary dictionary =
      AppPMMLUtils.buildDataDictionary(inputSchema, categoricalValueEncodings);

  PMML pmml = PMMLUtils.buildSkeletonPMML();
  pmml.setDataDictionary(dictionary);
  pmml.addModels(model);

  AppPMMLUtils.addExtension(pmml, "maxDepth", maxDepth);
  AppPMMLUtils.addExtension(pmml, "maxSplitCandidates", maxSplitCandidates);
  AppPMMLUtils.addExtension(pmml, "impurity", impurity);

  return pmml;
}
 
Example 11
Source File: VersionInspectorTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
@Test
public void inspectFieldAnnotations(){
	PMML pmml = createPMML();

	AssociationModel model = new AssociationModel();

	pmml.addModels(model);

	assertVersionRange(pmml, Version.PMML_3_0, Version.PMML_4_4);

	Output output = new Output();

	model.setOutput(output);

	assertVersionRange(pmml, Version.PMML_4_0, Version.PMML_4_4);

	model.setScorable(Boolean.FALSE);

	assertVersionRange(pmml, Version.PMML_4_1, Version.PMML_4_4);

	model.setScorable(null);

	assertVersionRange(pmml, Version.PMML_4_0, Version.PMML_4_4);

	OutputField outputField = new OutputField()
		.setRuleFeature(OutputField.RuleFeature.AFFINITY);

	output.addOutputFields(outputField);

	assertVersionRange(pmml, Version.PMML_4_1, Version.PMML_4_2);

	outputField.setDataType(DataType.DOUBLE);

	assertVersionRange(pmml, Version.PMML_4_1, Version.PMML_4_4);

	model.setOutput(null);

	assertVersionRange(pmml, Version.PMML_3_0, Version.PMML_4_4);
}
 
Example 12
Source File: VersionInspectorTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
@Test
public void inspectValueAnnotations(){
	PMML pmml = createPMML();

	FieldName name = FieldName.create("y");

	Target target = new Target()
		.setField(name)
		.addTargetValues(createTargetValue("no event"), createTargetValue("event"));

	Targets targets = new Targets()
		.addTargets(target);

	GeneralRegressionModel model = new GeneralRegressionModel()
		.setTargets(targets);

	pmml.addModels(model);

	assertVersionRange(pmml, Version.PMML_3_0, Version.PMML_3_0);

	PPMatrix ppMatrix = new PPMatrix()
		.addPPCells(new PPCell(), new PPCell());

	model.setPPMatrix(ppMatrix);

	assertVersionRange(pmml, Version.PMML_3_0, Version.PMML_4_4);

	target.setField(null);

	assertVersionRange(pmml, Version.PMML_4_3, Version.PMML_4_4);
}
 
Example 13
Source File: PMMLUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
@Test
public void findModel(){
	PMML pmml = new PMML();

	TreeModel firstTreeModel = new TreeModel()
		.setModelName("first");

	TreeModel secondTreeModel = new TreeModel()
		.setModelName("second");

	pmml.addModels(firstTreeModel, secondTreeModel);

	assertSame(firstTreeModel, PMMLUtil.findModel(pmml, TreeModel.class));

	firstTreeModel.setScorable(false);

	assertSame(secondTreeModel, PMMLUtil.findModel(pmml, TreeModel.class));

	secondTreeModel.setScorable(false);

	try {
		PMMLUtil.findModel(pmml, TreeModel.class);

		fail();
	} catch(MissingElementException mee){
		// Ignored
	}
}