org.dmg.pmml.ComparisonMeasure Java Examples

The following examples show how to use org.dmg.pmml.ComparisonMeasure. 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: KMeansModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public ClusteringModel encodeModel(Schema schema){
	KMeansModel model = getTransformer();

	List<Cluster> clusters = new ArrayList<>();

	Vector[] clusterCenters = model.clusterCenters();
	for(int i = 0; i < clusterCenters.length; i++){
		Cluster cluster = new Cluster(PMMLUtil.createRealArray(VectorUtil.toList(clusterCenters[i])))
			.setId(String.valueOf(i));

		clusters.add(cluster);
	}

	ComparisonMeasure comparisonMeasure = new ComparisonMeasure(ComparisonMeasure.Kind.DISTANCE, new SquaredEuclidean())
		.setCompareFunction(CompareFunction.ABS_DIFF);

	return new ClusteringModel(MiningFunction.CLUSTERING, ClusteringModel.ModelClass.CENTER_BASED, clusters.size(), ModelUtil.createMiningSchema(schema.getLabel()), comparisonMeasure, ClusteringModelUtil.createClusteringFields(schema.getFeatures()), clusters);
}
 
Example #2
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
private <V extends Number> AffinityDistribution<V> createAffinityDistribution(List<InstanceResult<V>> instanceResults, Function<Integer, String> function, Object result){
	NearestNeighborModel nearestNeighborModel = getModel();

	ComparisonMeasure comparisonMeasure = nearestNeighborModel.getComparisonMeasure();

	ValueMap<String, V> values = new ValueMap<>(2 * instanceResults.size());

	for(InstanceResult<V> instanceResult : instanceResults){
		values.put(function.apply(instanceResult.getId()), instanceResult.getValue());
	}

	Measure measure = MeasureUtil.ensureMeasure(comparisonMeasure);

	if(measure instanceof Similarity){
		return new AffinityDistribution<>(Classification.Type.SIMILARITY, values, result);
	} else

	if(measure instanceof Distance){
		return new AffinityDistribution<>(Classification.Type.DISTANCE, values, result);
	} else

	{
		throw new UnsupportedElementException(measure);
	}
}
 
Example #3
Source File: KMeans.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public ClusteringModel encodeModel(Schema schema){
	int[] shape = getClusterCentersShape();

	int numberOfClusters = shape[0];
	int numberOfFeatures = shape[1];

	List<? extends Number> clusterCenters = getClusterCenters();
	List<Integer> labels = getLabels();

	Multiset<Integer> labelCounts = HashMultiset.create();

	if(labels != null){
		labelCounts.addAll(labels);
	}

	List<Cluster> clusters = new ArrayList<>();

	for(int i = 0; i < numberOfClusters; i++){
		Cluster cluster = new Cluster(PMMLUtil.createRealArray(CMatrixUtil.getRow(clusterCenters, numberOfClusters, numberOfFeatures, i)))
			.setId(String.valueOf(i))
			.setSize((labelCounts.size () > 0 ? labelCounts.count(i) : null));

		clusters.add(cluster);
	}

	ComparisonMeasure comparisonMeasure = new ComparisonMeasure(ComparisonMeasure.Kind.DISTANCE, new SquaredEuclidean())
		.setCompareFunction(CompareFunction.ABS_DIFF);

	ClusteringModel clusteringModel = new ClusteringModel(MiningFunction.CLUSTERING, ClusteringModel.ModelClass.CENTER_BASED, numberOfClusters, ModelUtil.createMiningSchema(schema.getLabel()), comparisonMeasure, ClusteringModelUtil.createClusteringFields(schema.getFeatures()), clusters)
		.setOutput(ClusteringModelUtil.createOutput(FieldName.create("Cluster"), DataType.DOUBLE, clusters));

	return clusteringModel;
}
 
Example #4
Source File: KNeighborsUtil.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private ComparisonMeasure encodeComparisonMeasure(String metric, int p){

	switch(metric){
		case "minkowski":
			{
				Measure measure;

				switch(p){
					case 1:
						measure = new CityBlock();
						break;
					case 2:
						measure = new Euclidean();
						break;
					default:
						measure = new Minkowski(p);
						break;
				}

				ComparisonMeasure comparisonMeasure = new ComparisonMeasure(ComparisonMeasure.Kind.DISTANCE, measure)
					.setCompareFunction(CompareFunction.ABS_DIFF);

				return comparisonMeasure;
			}
		default:
			throw new IllegalArgumentException(metric);
	}
}
 
Example #5
Source File: KMeansConverter.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	RGenericVector kmeans = getObject();

	RDoubleVector centers = kmeans.getDoubleElement("centers");
	RIntegerVector size = kmeans.getIntegerElement("size");

	RIntegerVector centersDim = centers.dim();

	int rows = centersDim.getValue(0);
	int columns = centersDim.getValue(1);

	List<Cluster> clusters = new ArrayList<>();

	RStringVector rowNames = centers.dimnames(0);
	for(int i = 0; i < rowNames.size(); i++){
		Cluster cluster = new Cluster(PMMLUtil.createRealArray(FortranMatrixUtil.getRow(centers.getValues(), rows, columns, i)))
			.setId(String.valueOf(i + 1))
			.setName(rowNames.getValue(i))
			.setSize(size.getValue(i));

		clusters.add(cluster);
	}

	ComparisonMeasure comparisonMeasure = new ComparisonMeasure(ComparisonMeasure.Kind.DISTANCE, new SquaredEuclidean())
		.setCompareFunction(CompareFunction.ABS_DIFF);

	ClusteringModel clusteringModel = new ClusteringModel(MiningFunction.CLUSTERING, ClusteringModel.ModelClass.CENTER_BASED, rows, ModelUtil.createMiningSchema(schema.getLabel()), comparisonMeasure, ClusteringModelUtil.createClusteringFields(schema.getFeatures()), clusters)
		.setOutput(ClusteringModelUtil.createOutput(FieldName.create("cluster"), DataType.DOUBLE, clusters));

	return clusteringModel;
}
 
Example #6
Source File: KMeansUpdate.java    From oryx with Apache License 2.0 5 votes vote down vote up
private ClusteringModel pmmlClusteringModel(KMeansModel model,
                                            Map<Integer,Long> clusterSizesMap) {
  Vector[] clusterCenters = model.clusterCenters();

  List<ClusteringField> clusteringFields = new ArrayList<>();
  for (int i = 0; i < inputSchema.getNumFeatures(); i++) {
    if (inputSchema.isActive(i)) {
      FieldName fieldName = FieldName.create(inputSchema.getFeatureNames().get(i));
      ClusteringField clusteringField =
          new ClusteringField(fieldName).setCenterField(ClusteringField.CenterField.TRUE);
      clusteringFields.add(clusteringField);
    }
  }

  List<Cluster> clusters = new ArrayList<>(clusterCenters.length);
  for (int i = 0; i < clusterCenters.length; i++) {
    clusters.add(new Cluster().setId(Integer.toString(i))
                     .setSize(clusterSizesMap.get(i).intValue())
                     .setArray(AppPMMLUtils.toArray(clusterCenters[i].toArray())));
  }

  return new ClusteringModel(
      MiningFunction.CLUSTERING,
      ClusteringModel.ModelClass.CENTER_BASED,
      clusters.size(),
      AppPMMLUtils.buildMiningSchema(inputSchema),
      new ComparisonMeasure(ComparisonMeasure.Kind.DISTANCE, new SquaredEuclidean()),
      clusteringFields,
      clusters);
}
 
Example #7
Source File: ClusteringModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
public ClusteringModelEvaluator(PMML pmml, ClusteringModel clusteringModel){
	super(pmml, clusteringModel);

	ComparisonMeasure comparisonMeasure = clusteringModel.getComparisonMeasure();
	if(comparisonMeasure == null){
		throw new MissingElementException(clusteringModel, PMMLElements.CLUSTERINGMODEL_COMPARISONMEASURE);
	}

	ClusteringModel.ModelClass modelClass = clusteringModel.getModelClass();
	switch(modelClass){
		case CENTER_BASED:
			break;
		default:
			throw new UnsupportedAttributeException(clusteringModel, modelClass);
	}

	CenterFields centerFields = clusteringModel.getCenterFields();
	if(centerFields != null){
		throw new UnsupportedElementException(centerFields);
	}

	if(!clusteringModel.hasClusteringFields()){
		throw new MissingElementException(clusteringModel, PMMLElements.CLUSTERINGMODEL_CLUSTERINGFIELDS);
	} // End if

	if(!clusteringModel.hasClusters()){
		throw new MissingElementException(clusteringModel, PMMLElements.CLUSTERINGMODEL_CLUSTERS);
	}

	Targets targets = clusteringModel.getTargets();
	if(targets != null){
		throw new MisplacedElementException(targets);
	}
}
 
Example #8
Source File: ClusteringModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private <V extends Number> ClusterAffinityDistribution<V> evaluateDistance(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<ClusteringField> clusteringFields, List<FieldValue> values){
	ClusteringModel clusteringModel = getModel();

	List<Cluster> clusters = clusteringModel.getClusters();

	Value<V> adjustment;

	MissingValueWeights missingValueWeights = clusteringModel.getMissingValueWeights();
	if(missingValueWeights != null){
		Array array = missingValueWeights.getArray();

		List<? extends Number> adjustmentValues = ArrayUtil.asNumberList(array);
		if(values.size() != adjustmentValues.size()){
			throw new InvalidElementException(missingValueWeights);
		}

		adjustment = MeasureUtil.calculateAdjustment(valueFactory, values, adjustmentValues);
	} else

	{
		adjustment = MeasureUtil.calculateAdjustment(valueFactory, values);
	}

	ClusterAffinityDistribution<V> result = createClusterAffinityDistribution(Classification.Type.DISTANCE, clusters);

	for(Cluster cluster : clusters){
		List<FieldValue> clusterValues = CacheUtil.getValue(cluster, ClusteringModelEvaluator.clusterValueCache);

		if(values.size() != clusterValues.size()){
			throw new InvalidElementException(cluster);
		}

		Value<V> distance = MeasureUtil.evaluateDistance(valueFactory, comparisonMeasure, clusteringFields, values, clusterValues, adjustment);

		result.put(cluster, distance);
	}

	return result;
}
 
Example #9
Source File: MeasureUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public Measure ensureMeasure(ComparisonMeasure comparisonMeasure){
	Measure measure = comparisonMeasure.getMeasure();
	if(measure == null){
		throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(comparisonMeasure.getClass()) + "/<Measure>"), comparisonMeasure);
	}

	return measure;
}
 
Example #10
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
public NearestNeighborModelEvaluator(PMML pmml, NearestNeighborModel nearestNeighborModel){
	super(pmml, nearestNeighborModel);

	ComparisonMeasure comparisoonMeasure = nearestNeighborModel.getComparisonMeasure();
	if(comparisoonMeasure == null){
		throw new MissingElementException(nearestNeighborModel, PMMLElements.NEARESTNEIGHBORMODEL_COMPARISONMEASURE);
	}

	TrainingInstances trainingInstances = nearestNeighborModel.getTrainingInstances();
	if(trainingInstances == null){
		throw new MissingElementException(nearestNeighborModel, PMMLElements.NEARESTNEIGHBORMODEL_TRAININGINSTANCES);
	}

	InstanceFields instanceFields = trainingInstances.getInstanceFields();
	if(instanceFields == null){
		throw new MissingElementException(trainingInstances, PMMLElements.TRAININGINSTANCES_INSTANCEFIELDS);
	} // End if

	if(!instanceFields.hasInstanceFields()){
		throw new MissingElementException(instanceFields, PMMLElements.INSTANCEFIELDS_INSTANCEFIELDS);
	}

	KNNInputs knnInputs = nearestNeighborModel.getKNNInputs();
	if(knnInputs == null){
		throw new MissingElementException(nearestNeighborModel, PMMLElements.NEARESTNEIGHBORMODEL_KNNINPUTS);
	} // End if

	if(!knnInputs.hasKNNInputs()){
		throw new MissingElementException(knnInputs, PMMLElements.KNNINPUTS_KNNINPUTS);
	}
}
 
Example #11
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private <V extends Number> List<InstanceResult<V>> evaluateInstanceRows(ValueFactory<V> valueFactory, EvaluationContext context){
	NearestNeighborModel nearestNeighborModel = getModel();

	ComparisonMeasure comparisonMeasure = nearestNeighborModel.getComparisonMeasure();

	List<FieldValue> values = new ArrayList<>();

	KNNInputs knnInputs = nearestNeighborModel.getKNNInputs();
	for(KNNInput knnInput : knnInputs){
		FieldName name = knnInput.getField();
		if(name == null){
			throw new MissingAttributeException(knnInput, PMMLAttributes.KNNINPUT_FIELD);
		}

		FieldValue value = context.evaluate(name);

		values.add(value);
	}

	Measure measure = MeasureUtil.ensureMeasure(comparisonMeasure);

	if(measure instanceof Similarity){
		return evaluateSimilarity(valueFactory, comparisonMeasure, knnInputs.getKNNInputs(), values);
	} else

	if(measure instanceof Distance){
		return evaluateDistance(valueFactory, comparisonMeasure, knnInputs.getKNNInputs(), values);
	} else

	{
		throw new UnsupportedElementException(measure);
	}
}
 
Example #12
Source File: MeasureUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void evaluateSimilarity(){
	BitSet flags = createFlags(Arrays.asList(0, 0, 1, 1));
	BitSet referenceFlags = createFlags(Arrays.asList(0, 1, 0, 1));

	ValueFactory<?> valueFactory = MeasureUtilTest.valueFactoryFactory.newValueFactory(MathContext.DOUBLE);

	ComparisonMeasure comparisonMeasure = new ComparisonMeasure(ComparisonMeasure.Kind.SIMILARITY, new SimpleMatching());

	List<ClusteringField> clusteringFields = createClusteringFields("one", "two", "three", "four");

	assertEquals(valueFactory.newValue(2d / 4d), MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, referenceFlags));

	comparisonMeasure.setMeasure(new Jaccard());

	assertEquals(valueFactory.newValue(1d / 3d), MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, referenceFlags));

	comparisonMeasure.setMeasure(new Tanimoto());

	assertEquals(valueFactory.newValue(2d / (1d + 2 * 2d + 1d)), MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, referenceFlags));

	comparisonMeasure.setMeasure(new BinarySimilarity(0.5d, 0.5d, 0.5d, 0.5d, 1d, 1d, 1d, 1d));

	assertEquals(valueFactory.newValue(2d / 4d), MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, referenceFlags));
}
 
Example #13
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
private <V extends Number> List<InstanceResult<V>> evaluateDistance(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<KNNInput> knnInputs, List<FieldValue> values){
	Map<Integer, List<FieldValue>> valueMap = getInstanceValues();

	List<InstanceResult<V>> result = new ArrayList<>(valueMap.size());

	Value<V> adjustment = MeasureUtil.calculateAdjustment(valueFactory, values);

	Set<Integer> rowKeys = valueMap.keySet();
	for(Integer rowKey : rowKeys){
		List<FieldValue> instanceValues = valueMap.get(rowKey);

		Value<V> distance = MeasureUtil.evaluateDistance(valueFactory, comparisonMeasure, knnInputs, values, instanceValues, adjustment);

		result.add(new InstanceResult.Distance<>(rowKey, distance));
	}

	return result;
}
 
Example #14
Source File: NearestNeighborModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
private <V extends Number> List<InstanceResult<V>> evaluateSimilarity(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<KNNInput> knnInputs, List<FieldValue> values){
	BitSet flags = MeasureUtil.toBitSet(values);

	Map<Integer, BitSet> flagMap = getInstanceFlags();

	List<InstanceResult<V>> result = new ArrayList<>(flagMap.size());

	Set<Integer> rowKeys = flagMap.keySet();
	for(Integer rowKey : rowKeys){
		BitSet instanceFlags = flagMap.get(rowKey);

		Value<V> similarity = MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, knnInputs, flags, instanceFlags);

		result.add(new InstanceResult.Similarity<>(rowKey, similarity));
	}

	return result;
}
 
Example #15
Source File: ClusteringModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
private <V extends Number> ClusterAffinityDistribution<V> evaluateSimilarity(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<ClusteringField> clusteringFields, List<FieldValue> values){
	ClusteringModel clusteringModel = getModel();

	List<Cluster> clusters = clusteringModel.getClusters();

	ClusterAffinityDistribution<V> result = createClusterAffinityDistribution(Classification.Type.SIMILARITY, clusters);

	BitSet flags = MeasureUtil.toBitSet(values);

	for(Cluster cluster : clusters){
		BitSet clusterFlags = CacheUtil.getValue(cluster, ClusteringModelEvaluator.clusterFlagCache);

		if(flags.size() != clusterFlags.size()){
			throw new InvalidElementException(cluster);
		}

		Value<V> similarity = MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, clusterFlags);

		result.put(cluster, similarity);
	}

	return result;
}
 
Example #16
Source File: ClusteringModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected <V extends Number> Map<FieldName, ClusterAffinityDistribution<V>> evaluateClustering(ValueFactory<V> valueFactory, EvaluationContext context){
	ClusteringModel clusteringModel = getModel();

	ComparisonMeasure comparisonMeasure = clusteringModel.getComparisonMeasure();

	List<ClusteringField> clusteringFields = getCenterClusteringFields();

	List<FieldValue> values = new ArrayList<>(clusteringFields.size());

	for(int i = 0, max = clusteringFields.size(); i < max; i++){
		ClusteringField clusteringField = clusteringFields.get(i);

		FieldName name = clusteringField.getField();
		if(name == null){
			throw new MissingAttributeException(clusteringField, PMMLAttributes.CLUSTERINGFIELD_FIELD);
		}

		FieldValue value = context.evaluate(name);

		values.add(value);
	}

	ClusterAffinityDistribution<V> result;

	Measure measure = MeasureUtil.ensureMeasure(comparisonMeasure);

	if(measure instanceof Similarity){
		result = evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, values);
	} else

	if(measure instanceof Distance){
		result = evaluateDistance(valueFactory, comparisonMeasure, clusteringFields, values);
	} else

	{
		throw new UnsupportedElementException(measure);
	}

	// "For clustering models, the identifier of the winning cluster is returned as the predictedValue"
	result.computeResult(DataType.STRING);

	return Collections.singletonMap(getTargetName(), result);
}
 
Example #17
Source File: KMeansUpdateIT.java    From oryx with Apache License 2.0 4 votes vote down vote up
@Test
public void testKMeans() throws Exception {
  Path tempDir = getTempDir();
  Path dataDir = tempDir.resolve("data");
  Path modelDir = tempDir.resolve("model");

  Map<String,Object> overlayConfig = new HashMap<>();
  overlayConfig.put("oryx.batch.update-class", KMeansUpdate.class.getName());
  ConfigUtils.set(overlayConfig, "oryx.batch.storage.data-dir", dataDir);
  ConfigUtils.set(overlayConfig, "oryx.batch.storage.model-dir", modelDir);
  overlayConfig.put("oryx.batch.streaming.generation-interval-sec", GEN_INTERVAL_SEC);
  overlayConfig.put("oryx.kmeans.hyperparams.k", NUM_CLUSTERS);
  overlayConfig.put("oryx.kmeans.iterations", 5);
  overlayConfig.put("oryx.input-schema.num-features", NUM_FEATURES);
  overlayConfig.put("oryx.input-schema.categorical-features", "[]");
  overlayConfig.put("oryx.kmeans.evaluation-strategy", EVALUATION_STRATEGY);

  Config config = ConfigUtils.overlayOn(overlayConfig, getConfig());

  startMessaging();

  List<KeyMessage<String, String>> updates = startServerProduceConsumeTopics(
      config,
      new RandomKMeansDataGenerator(NUM_FEATURES),
      DATA_TO_WRITE,
      WRITE_INTERVAL_MSEC);

  List<Path> modelInstanceDirs = IOUtils.listFiles(modelDir, "*");

  int generations = modelInstanceDirs.size();
  checkIntervals(generations, DATA_TO_WRITE, WRITE_INTERVAL_MSEC, GEN_INTERVAL_SEC);

  for (Path modelInstanceDir : modelInstanceDirs) {
    Path modelFile = modelInstanceDir.resolve(MLUpdate.MODEL_FILE_NAME);
    assertNonEmpty(modelFile);
    PMMLUtils.read(modelFile); // Shouldn't throw exception
  }

  InputSchema schema = new InputSchema(config);

  for (KeyMessage<String,String> km : updates) {

    String type = km.getKey();
    String value = km.getMessage();

    assertContains(Arrays.asList("MODEL", "MODEL-REF"), type);
    PMML pmml = AppPMMLUtils.readPMMLFromUpdateKeyMessage(type, value, null);
    assertNotNull(pmml);
    checkHeader(pmml.getHeader());

    checkDataDictionary(schema, pmml.getDataDictionary());

    Model rootModel = pmml.getModels().get(0);

    ClusteringModel clusteringModel = (ClusteringModel) rootModel;

    // Check if Basic hyperparameters match
    assertEquals(NUM_CLUSTERS, clusteringModel.getNumberOfClusters().intValue());
    assertEquals(NUM_CLUSTERS, clusteringModel.getClusters().size());
    assertEquals(NUM_FEATURES, clusteringModel.getClusteringFields().size());
    assertEquals(ComparisonMeasure.Kind.DISTANCE,
                 clusteringModel.getComparisonMeasure().getKind());
    assertEquals(NUM_FEATURES, clusteringModel.getClusters().get(0).getArray().getN().intValue());
    for (Cluster cluster : clusteringModel.getClusters()) {
      assertGreater(cluster.getSize(), 0);
    }
  }
}
 
Example #18
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;
}