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

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

Introduction

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

Prototype

public FuzzyKMeansClusterer(final int k, final double fuzziness, final int maxIterations,
        final DistanceMeasure measure) throws NumberIsTooSmallException 

Source Link

Document

Creates a new instance of a FuzzyKMeansClusterer.

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;//from   w  ww .j a  v a2  s  .c om
    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);
}