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

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

Introduction

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

Prototype

public RealMatrix getMembershipMatrix() 

Source Link

Document

Returns the nxk membership matrix, where n is the number of data points and k the number of clusters.

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  ww  .  j av  a2s.c o m*/
    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);
}