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

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

Introduction

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

Prototype

@Override
public List<CentroidCluster<T>> cluster(final Collection<T> dataPoints) throws MathIllegalArgumentException 

Source Link

Document

Performs Fuzzy K-Means cluster analysis.

Usage

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

@Override
public Object doWork(Object value1, Object value2) throws IOException {

    Matrix matrix = null;//w  w  w .j a  v a 2s  .  com
    int k = 0;

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

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

    FuzzyKMeansClusterer<KmeansEvaluator.ClusterPoint> kmeans = new FuzzyKMeansClusterer(k, fuzziness,
            maxIterations, new EuclideanDistance());
    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("fuzziness", fuzziness);
    fields.put("distance", "euclidean");
    fields.put("maxIterations", maxIterations);

    List<CentroidCluster<KmeansEvaluator.ClusterPoint>> clusters = kmeans.cluster(points);
    RealMatrix realMatrix = kmeans.getMembershipMatrix();
    double[][] mmData = realMatrix.getData();
    Matrix mmMatrix = new Matrix(mmData);
    mmMatrix.setRowLabels(matrix.getRowLabels());
    return new KmeansEvaluator.ClusterTuple(fields, clusters, matrix.getColumnLabels(), mmMatrix);
}