Skip to content
Snippets Groups Projects
Commit fd9ab5d3 authored by Fabian Pedregosa's avatar Fabian Pedregosa
Browse files

Fix a bug in ellipses of confidence computation for mixture + adapt to new...

Fix a bug in ellipses of confidence computation for mixture + adapt to new datasets layout in examples

From: jarrod.millman <jarrod.millman@cb17146a-f446-4be1-a4f7-bd7c5bb65646>

git-svn-id: https://scikit-learn.svn.sourceforge.net/svnroot/scikit-learn/trunk@207 22fbfee3-77ab-4535-9bad-27d1bd3bc7d8
parent a2514e3f
Branches
Tags
No related merge requests found
#! /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 @@ def get_faithful():
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])
......
# /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 @@ class GM:
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 @@ class GM:
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
......
......@@ -17,26 +17,15 @@ lib.compute.restype = c_int
# 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
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)
x -= mu
x **= 2
return N.exp(N.dot(x, N.ones((mu.size, 1), x.dtype)))
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,23 +33,10 @@ def bench(func, mode = 'diag'):
niter = 10
print "Compute %d times densities, %d dimension, %d frames" % (niter, d, n)
mu = randn(d)
va = abs(randn(d))
X = 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))
mu = 0.1 * randn(d)
va = 0.1 * abs(randn(d))
X = randn(d, n)
X = 0.1 * randn(n, d)
for i in range(niter):
Y = func(X, mu, va)
......@@ -74,24 +50,22 @@ def benchpy2():
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()
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()
#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(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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment