FuzzyKMeansClusterer extends Clusterer<T> { @Override public List<CentroidCluster<T>> cluster(final Collection<T> dataPoints) throws MathIllegalArgumentException { MathUtils.checkNotNull(dataPoints); final int size = dataPoints.size(); if (size < k) { throw new NumberIsTooSmallException(size, k, false); } points = Collections.unmodifiableList(new ArrayList<T>(dataPoints)); clusters = new ArrayList<CentroidCluster<T>>(); membershipMatrix = new double[size][k]; final double[][] oldMatrix = new double[size][k]; if (size == 0) { return clusters; } initializeMembershipMatrix(); final int pointDimension = points.get(0).getPoint().length; for (int i = 0; i < k; i++) { clusters.add(new CentroidCluster<T>(new DoublePoint(new double[pointDimension]))); } int iteration = 0; final int max = (maxIterations < 0) ? Integer.MAX_VALUE : maxIterations; double difference = 0.0; do { saveMembershipMatrix(oldMatrix); updateClusterCenters(); updateMembershipMatrix(); difference = calculateMaxMembershipChange(oldMatrix); } while (difference > epsilon && ++iteration < max); return clusters; } FuzzyKMeansClusterer(final int k, final double fuzziness);  FuzzyKMeansClusterer(final int k, final double fuzziness,
                                final int maxIterations, final DistanceMeasure measure);  FuzzyKMeansClusterer(final int k, final double fuzziness,
                                final int maxIterations, final DistanceMeasure measure,
                                final double epsilon, final RandomGenerator random); int getK(); double getFuzziness(); int getMaxIterations(); double getEpsilon(); RandomGenerator getRandomGenerator(); RealMatrix getMembershipMatrix(); List<T> getDataPoints(); List<CentroidCluster<T>> getClusters(); double getObjectiveFunctionValue(); @Override List<CentroidCluster<T>> cluster(final Collection<T> dataPoints);  }
@Test public void testCluster() { final List<DoublePoint> points = new ArrayList<DoublePoint>(); for (int i = 1; i <= 10; i++) { final DoublePoint p = new DoublePoint(new double[] { i } ); points.add(p); } final FuzzyKMeansClusterer<DoublePoint> transformer = new FuzzyKMeansClusterer<DoublePoint>(3, 2.0); final List<CentroidCluster<DoublePoint>> clusters = transformer.cluster(points); final List<DoublePoint> clusterOne = Arrays.asList(points.get(0), points.get(1), points.get(2)); final List<DoublePoint> clusterTwo = Arrays.asList(points.get(3), points.get(4), points.get(5), points.get(6)); final List<DoublePoint> clusterThree = Arrays.asList(points.get(7), points.get(8), points.get(9)); boolean cluster1Found = false; boolean cluster2Found = false; boolean cluster3Found = false; Assert.assertEquals(3, clusters.size()); for (final Cluster<DoublePoint> cluster : clusters) { if (cluster.getPoints().containsAll(clusterOne)) { cluster1Found = true; } if (cluster.getPoints().containsAll(clusterTwo)) { cluster2Found = true; } if (cluster.getPoints().containsAll(clusterThree)) { cluster3Found = true; } } Assert.assertTrue(cluster1Found); Assert.assertTrue(cluster2Found); Assert.assertTrue(cluster3Found); }
@Test(expected = NullArgumentException.class) public void testNullDataset() { final FuzzyKMeansClusterer<DoublePoint> clusterer = new FuzzyKMeansClusterer<DoublePoint>(3, 2.0); clusterer.cluster(null); }
@Test public void testSingleCluster() { final List<DoublePoint> points = new ArrayList<DoublePoint>(); points.add(new DoublePoint(new double[] { 1, 1 })); final FuzzyKMeansClusterer<DoublePoint> transformer = new FuzzyKMeansClusterer<DoublePoint>(1, 2.0); final List<CentroidCluster<DoublePoint>> clusters = transformer.cluster(points); Assert.assertEquals(1, clusters.size()); }
@Test public void testClusterCenterEqualsPoints() { final List<DoublePoint> points = new ArrayList<DoublePoint>(); points.add(new DoublePoint(new double[] { 1, 1 })); points.add(new DoublePoint(new double[] { 1.00001, 1.00001 })); points.add(new DoublePoint(new double[] { 2, 2 })); points.add(new DoublePoint(new double[] { 3, 3 })); final FuzzyKMeansClusterer<DoublePoint> transformer = new FuzzyKMeansClusterer<DoublePoint>(3, 2.0); final List<CentroidCluster<DoublePoint>> clusters = transformer.cluster(points); Assert.assertEquals(3, clusters.size()); }