Search in sources :

Example 1 with KMeansModel

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);
}
Also used : KMeansModel(org.apache.spark.ml.clustering.KMeansModel) SquaredEuclidean(org.dmg.pmml.SquaredEuclidean) ArrayList(java.util.ArrayList) Cluster(org.dmg.pmml.clustering.Cluster) Vector(org.apache.spark.ml.linalg.Vector) ComparisonMeasure(org.dmg.pmml.ComparisonMeasure) ClusteringModel(org.dmg.pmml.clustering.ClusteringModel)

Aggregations

ArrayList (java.util.ArrayList)1 KMeansModel (org.apache.spark.ml.clustering.KMeansModel)1 Vector (org.apache.spark.ml.linalg.Vector)1 ComparisonMeasure (org.dmg.pmml.ComparisonMeasure)1 SquaredEuclidean (org.dmg.pmml.SquaredEuclidean)1 Cluster (org.dmg.pmml.clustering.Cluster)1 ClusteringModel (org.dmg.pmml.clustering.ClusteringModel)1