[Scipy-svn] r3168 - in trunk/Lib/sandbox/pyem: . examples profile_data

scipy-svn@scip... scipy-svn@scip...
Tue Jul 17 21:07:33 CDT 2007


Author: cdavid
Date: 2007-07-17 21:07:24 -0500 (Tue, 17 Jul 2007)
New Revision: 3168

Modified:
   trunk/Lib/sandbox/pyem/
   trunk/Lib/sandbox/pyem/examples/utils.py
   trunk/Lib/sandbox/pyem/gauss_mix.py
   trunk/Lib/sandbox/pyem/profile_data/profile_densities.py
Log:
Fix a bug in ellipses of confidence computation for mixture + adapt to new datasets layout in examples


Property changes on: trunk/Lib/sandbox/pyem
___________________________________________________________________
Name: svn:ignore
   - *.pyc
*.swp
*.pyd
*.so
*.prof


   + *.pyc
*.swp
*.pyd
*.so
*.prof
build
sandbox
mixture-0.3a
mixture-0.3a.tar.gz


Modified: trunk/Lib/sandbox/pyem/examples/utils.py
===================================================================
--- trunk/Lib/sandbox/pyem/examples/utils.py	2007-07-17 16:33:55 UTC (rev 3167)
+++ trunk/Lib/sandbox/pyem/examples/utils.py	2007-07-18 02:07:24 UTC (rev 3168)
@@ -1,5 +1,5 @@
 #! /usr/bin/env python
-# Last Change: Mon Jul 02 08:00 PM 2007 J
+# Last Change: Tue Jul 17 10:00 PM 2007 J
 
 # Various utilities for examples 
 
@@ -15,7 +15,7 @@
     data = oldfaithful.load()
     tmp1 = []
     tmp2 = []
-    for i in data:
+    for i in data['data']:
         if not (i[0] == 'L' or i[0] == 'M' or i[0] == 'S'):
             tmp1.append(i[0])
             tmp2.append(i[1])

Modified: trunk/Lib/sandbox/pyem/gauss_mix.py
===================================================================
--- trunk/Lib/sandbox/pyem/gauss_mix.py	2007-07-17 16:33:55 UTC (rev 3167)
+++ trunk/Lib/sandbox/pyem/gauss_mix.py	2007-07-18 02:07:24 UTC (rev 3168)
@@ -1,5 +1,5 @@
 # /usr/bin/python
-# Last Change: Mon Jul 02 07:00 PM 2007 J
+# Last Change: Tue Jul 17 11:00 PM 2007 J
 
 """Module implementing GM, a class which represents Gaussian mixtures.
 
@@ -243,10 +243,10 @@
                 level of confidence (between 0 and 1).
 
         :Returns:
-            Xe : sequence
+            xe : sequence
                 a list of x coordinates for the ellipses (Xe[i] is the array
                 containing x coordinates of the ith Gaussian)
-            Ye : sequence
+            ye : sequence
                 a list of y coordinates for the ellipses.
 
         Examples
@@ -276,17 +276,17 @@
         ye  = []   
         if self.mode == 'diag':
             for i in range(self.k):
-                xe, ye  = D.gauss_ell(self.mu[i, :], self.va[i, :], 
+                x, y  = D.gauss_ell(self.mu[i, :], self.va[i, :], 
                         dim, npoints, level)
-                xe.append(xe)
-                ye.append(ye)
+                xe.append(x)
+                ye.append(y)
         elif self.mode == 'full':
             for i in range(self.k):
-                xe, ye  = D.gauss_ell(self.mu[i, :], 
+                x, y  = D.gauss_ell(self.mu[i, :], 
                         self.va[i*self.d:i*self.d+self.d, :], 
                         dim, npoints, level)
-                xe.append(xe)
-                ye.append(ye)
+                xe.append(x)
+                ye.append(y)
 
         return xe, ye
     

Modified: trunk/Lib/sandbox/pyem/profile_data/profile_densities.py
===================================================================
--- trunk/Lib/sandbox/pyem/profile_data/profile_densities.py	2007-07-17 16:33:55 UTC (rev 3167)
+++ trunk/Lib/sandbox/pyem/profile_data/profile_densities.py	2007-07-18 02:07:24 UTC (rev 3168)
@@ -17,26 +17,15 @@
 # Compare computing per component likelihood for frame per row vs frame per column
 def component_likelihood(x, mu, va, log = False):
     """expect one frame to be one row (rank 2). mu and var are rank 1 array."""
-    d = mu.size
+    x -= mu
+    x **= 2
+    return N.exp(N.dot(x, N.ones((mu.size, 1), x.dtype)))
 
-    return N.exp(N.sum((x - mu) ** 2, 1))
-
-def component_likelihood2(x, mu, va, log = False):
-    """expect one frame to be one column (rank 2). mu and var are rank 1 array."""
-    d = mu.size
-
-    y = (x[0] - mu[0]) ** 2
-    for i in range(1, d):
-        y += (x[i] - mu[i]) ** 2
-
-    return N.exp(y)
-
 def component_likelihood3(x, mu, va, log = False):
     """expect one frame to be one row (rank 2). mu and var are rank 1 array."""
-    d = mu.size
-
     y = N.empty(x.shape[0], x.dtype)
-    return lib.compute(x, x.shape[0], d, mu, y)
+    lib.compute(x, x.shape[0], x.shape[1], mu, y)
+    return y
 
 def bench(func, mode = 'diag'):
     d       = 30
@@ -44,26 +33,13 @@
     niter   = 10
 
     print "Compute %d times densities, %d dimension, %d frames" % (niter, d, n)
-    mu  = randn(d)
-    va  = abs(randn(d))
+    mu  = 0.1 * randn(d)
+    va  = 0.1 * abs(randn(d))
     
-    X   = randn(n, d)
+    X   = 0.1 * randn(n, d)
     for i in range(niter):
         Y   = func(X, mu, va)
 
-def bench2(func, mode = 'diag'):
-    d       = 30
-    n       = 1e5
-    niter   = 10
-
-    print "Compute %d times densities, %d dimension, %d frames" % (niter, d, n)
-    mu  = randn(d)
-    va  = abs(randn(d))
-    
-    X   = randn(d, n)
-    for i in range(niter):
-        Y   = func(X, mu, va)
-
 def benchpy():
     bench(component_likelihood)
 
@@ -74,24 +50,22 @@
     bench2(component_likelihood2)
 
 if __name__ == "__main__":
-    import hotshot, hotshot.stats
-    profile_file    = 'gdenpy.prof'
-    prof    = hotshot.Profile(profile_file, lineevents=1)
-    prof.runcall(benchpy)
-    p = hotshot.stats.load(profile_file)
-    print p.sort_stats('cumulative').print_stats(20)
-    prof.close()
+    #import hotshot, hotshot.stats
+    #profile_file    = 'gdenpy.prof'
+    #prof    = hotshot.Profile(profile_file, lineevents=1)
+    #prof.runcall(benchpy)
+    #p = hotshot.stats.load(profile_file)
+    #print p.sort_stats('cumulative').print_stats(20)
+    #prof.close()
 
-    profile_file    = 'gdenc.prof'
-    prof    = hotshot.Profile(profile_file, lineevents=1)
-    prof.runcall(benchpy2)
-    p = hotshot.stats.load(profile_file)
-    print p.sort_stats('cumulative').print_stats(20)
-    prof.close()
+    #profile_file    = 'gdenc.prof'
+    #prof    = hotshot.Profile(profile_file, lineevents=1)
+    #prof.runcall(benchpy3)
+    #p = hotshot.stats.load(profile_file)
+    #print p.sort_stats('cumulative').print_stats(20)
+    #prof.close()
 
-    profile_file    = 'gdenc.prof'
-    prof    = hotshot.Profile(profile_file, lineevents=1)
-    prof.runcall(benchpy3)
-    p = hotshot.stats.load(profile_file)
-    print p.sort_stats('cumulative').print_stats(20)
-    prof.close()
+    #import cProfile as profile
+    #profile.run('benchpy()', 'fooprof')
+    benchpy()
+    benchpy3()



More information about the Scipy-svn mailing list