[Scipy-svn] r4078 - in trunk/scipy/cluster: . tests
scipy-svn@scip...
scipy-svn@scip...
Thu Apr 3 13:28:49 CDT 2008
Author: cdavid
Date: 2008-04-03 13:28:44 -0500 (Thu, 03 Apr 2008)
New Revision: 4078
Modified:
trunk/scipy/cluster/tests/test_vq.py
trunk/scipy/cluster/vq.py
Log:
Handle data of rank 1 for random init in kmean2
Modified: trunk/scipy/cluster/tests/test_vq.py
===================================================================
--- trunk/scipy/cluster/tests/test_vq.py 2008-04-03 15:13:35 UTC (rev 4077)
+++ trunk/scipy/cluster/tests/test_vq.py 2008-04-03 18:28:44 UTC (rev 4078)
@@ -131,6 +131,14 @@
code1 = kmeans2(data1, code, iter = 1)[0]
code2 = kmeans2(data1, code, iter = 2)[0]
+ def test_kmeans2_rank1_2(self):
+ """Testing simple call to kmeans2 with rank 1 data."""
+ data = N.fromfile(open(DATAFILE1), sep = ", ")
+ data = data.reshape((200, 2))
+ data1 = data[:, 0]
+
+ code1 = kmeans2(data1, 2, iter = 1)
+
def test_kmeans2_init(self):
"""Testing that kmeans2 init methods work."""
data = N.fromfile(open(DATAFILE1), sep = ", ")
Modified: trunk/scipy/cluster/vq.py
===================================================================
--- trunk/scipy/cluster/vq.py 2008-04-03 15:13:35 UTC (rev 4077)
+++ trunk/scipy/cluster/vq.py 2008-04-03 18:28:44 UTC (rev 4078)
@@ -460,15 +460,28 @@
Number of samples to generate.
"""
- mu = N.mean(data, 0)
- cov = N.atleast_2d(N.cov(data, rowvar = 0))
+ def init_rank1(data):
+ mu = N.mean(data)
+ cov = N.cov(data)
+ x = N.random.randn(k)
+ x *= N.sqrt(cov)
+ x += mu
+ return x
+ def init_rankn(data):
+ mu = N.mean(data, 0)
+ cov = N.atleast_2d(N.cov(data, rowvar = 0))
- # k rows, d cols (one row = one obs)
- # Generate k sample of a random variable ~ Gaussian(mu, cov)
- x = N.random.randn(k, mu.size)
- x = N.dot(x, N.linalg.cholesky(cov).T) + mu
+ # k rows, d cols (one row = one obs)
+ # Generate k sample of a random variable ~ Gaussian(mu, cov)
+ x = N.random.randn(k, mu.size)
+ x = N.dot(x, N.linalg.cholesky(cov).T) + mu
+ return x
- return x
+ nd = N.ndim(data)
+ if nd == 1:
+ return init_rank1(data)
+ else:
+ return init_rankn(data)
_valid_init_meth = {'random': _krandinit, 'points': _kpoints}
More information about the Scipy-svn
mailing list