Example usage for org.apache.commons.math3.ml.clustering MultiKMeansPlusPlusClusterer cluster

List of usage examples for org.apache.commons.math3.ml.clustering MultiKMeansPlusPlusClusterer cluster

Introduction

In this page you can find the example usage for org.apache.commons.math3.ml.clustering MultiKMeansPlusPlusClusterer cluster.

Prototype

@Override
public List<CentroidCluster<T>> cluster(final Collection<T> points)
        throws MathIllegalArgumentException, ConvergenceException 

Source Link

Document

Runs the K-means++ clustering algorithm.

Usage

From source file:org.apache.solr.client.solrj.io.eval.MultiKmeansEvaluator.java

@Override
public Object doWork(Object... values) throws IOException {

    if (values.length != 3) {
        throw new IOException(
                "The multiKmeans function expects three parameters; a matrix to cluster, k and number of trials.");
    }// w ww .  j a  v a2 s .  co m

    Object value1 = values[0];
    Object value2 = values[1];
    Object value3 = values[2];

    Matrix matrix = null;
    int k = 0;
    int trials = 0;

    if (value1 instanceof Matrix) {
        matrix = (Matrix) value1;
    } else {
        throw new IOException("The first parameter for multiKmeans should be the observation matrix.");
    }

    if (value2 instanceof Number) {
        k = ((Number) value2).intValue();
    } else {
        throw new IOException("The second parameter for multiKmeans should be k.");
    }

    if (value3 instanceof Number) {
        trials = ((Number) value3).intValue();
    } else {
        throw new IOException("The third parameter for multiKmeans should be trials.");
    }

    KMeansPlusPlusClusterer<KmeansEvaluator.ClusterPoint> kmeans = new KMeansPlusPlusClusterer(k,
            maxIterations);
    MultiKMeansPlusPlusClusterer multiKmeans = new MultiKMeansPlusPlusClusterer(kmeans, trials);

    List<KmeansEvaluator.ClusterPoint> points = new ArrayList();
    double[][] data = matrix.getData();

    List<String> ids = matrix.getRowLabels();

    for (int i = 0; i < data.length; i++) {
        double[] vec = data[i];
        points.add(new KmeansEvaluator.ClusterPoint(ids.get(i), vec));
    }

    Map fields = new HashMap();

    fields.put("k", k);
    fields.put("trials", trials);
    fields.put("distance", "euclidean");
    fields.put("maxIterations", maxIterations);

    return new KmeansEvaluator.ClusterTuple(fields, multiKmeans.cluster(points), matrix.getColumnLabels());
}