You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2013/04/10 22:13:43 UTC

svn commit: r1466655 - /commons/proper/math/trunk/src/test/java/org/apache/commons/math3/ml/clustering/FuzzyKMeansClustererTest.java

Author: tn
Date: Wed Apr 10 20:12:54 2013
New Revision: 1466655

URL: http://svn.apache.org/r1466655
Log:
[MATH-898] Add initial tests for fuzzy clusterer.

Added:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/ml/clustering/FuzzyKMeansClustererTest.java   (with props)

Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/ml/clustering/FuzzyKMeansClustererTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/ml/clustering/FuzzyKMeansClustererTest.java?rev=1466655&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/ml/clustering/FuzzyKMeansClustererTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/ml/clustering/FuzzyKMeansClustererTest.java Wed Apr 10 20:12:54 2013
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.ml.clustering;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.commons.math3.exception.MathIllegalArgumentException;
+import org.apache.commons.math3.exception.NullArgumentException;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class FuzzyKMeansClustererTest {
+
+    @Test
+    public void testCluster() {
+        List<DoublePoint> points = new ArrayList<DoublePoint>();
+        
+        // create 10 data points: [1], ... [10]
+        for (int i = 1; i <= 10; i++) {
+            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);
+
+        // we expect 3 clusters:
+        //   [1], [2], [3]
+        //   [4], [5], [6], [7]
+        //   [8], [9], [10]
+        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 = MathIllegalArgumentException.class)
+    public void testTooSmallFuzzynessFactor() {
+        new FuzzyKMeansClusterer<DoublePoint>(3, 1.0);
+    }
+
+    @Test(expected = NullArgumentException.class)
+    public void testNullDataset() {
+        FuzzyKMeansClusterer<DoublePoint> clusterer = new FuzzyKMeansClusterer<DoublePoint>(3, 2.0);
+        clusterer.cluster(null);
+    }
+
+}

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/ml/clustering/FuzzyKMeansClustererTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/ml/clustering/FuzzyKMeansClustererTest.java
------------------------------------------------------------------------------
    svn:keywords = Id Revision HeadURL

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/ml/clustering/FuzzyKMeansClustererTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain