[Scipy-svn] r3077 - trunk/Lib/cluster

scipy-svn at scipy.org scipy-svn at scipy.org
Fri Jun 8 00:42:30 EDT 2007


Author: cdavid
Date: 2007-06-07 23:42:23 -0500 (Thu, 07 Jun 2007)
New Revision: 3077

Modified:
   trunk/Lib/cluster/vq.py
Log:
Additional init method for kmeans2, 'matrix', to avoid possible confusion between k number of components and k unique initial cluster. Close #443

Modified: trunk/Lib/cluster/vq.py
===================================================================
--- trunk/Lib/cluster/vq.py	2007-06-08 04:14:14 UTC (rev 3076)
+++ trunk/Lib/cluster/vq.py	2007-06-08 04:42:23 UTC (rev 3077)
@@ -479,8 +479,8 @@
             dimensional data, rank 2 multidimensional data, in which case one
             row is one observation.
         k : int or ndarray
-            Number of clusters. If a ndarray is given instead, it is
-            interpreted as initial cluster to use instead.
+            Number of clusters. If minit arg is 'matrix', or if a ndarray is
+            given instead, it is interpreted as initial cluster to use instead.
         niter : int
             Number of iterations to run.
         thresh : float
@@ -495,8 +495,11 @@
             points choses k points at random from the points in data.
 
             uniform choses k points from the data such are they form a uniform
-            grid od the dataset.
+            grid od the dataset (not supported yet).
 
+            matrix means that k has to be interpreted as initial clusters
+            (format is the same than data).
+
     :Returns:
         clusters : ndarray
             the found clusters (one cluster per row).
@@ -517,7 +520,7 @@
 
     # If k is not a single value, then it should be compatible with data's
     # shape
-    if N.size(k) > 1:
+    if N.size(k) > 1 or minit == 'matrix':
         if not nd == N.ndim(k):
             raise ValueError("k is not an int and has not same rank than data")
         if d == 1:
@@ -529,7 +532,9 @@
                         data")
         clusters = k.copy()
     else:
-        nc = k
+        nc = int(k)
+        if not nc == k:
+            warnings.warn("k was not an integer, was converted.")
         try:
             init = _valid_init_meth[minit]
         except KeyError:




More information about the Scipy-svn mailing list