use of org.apache.spark.ml.clustering.KMeansModel in project jpmml-sparkml by jpmml.
the class KMeansModelConverter method encodeModel.
@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);
}
Aggregations