/
iEngine.py
executable file
·101 lines (77 loc) · 2.29 KB
/
iEngine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#! /usr/bin/env python
import sys, getopt, math, datetime, os
from random import gauss
from santa_fe import getData
from components import *
from numpy import *
from pylab import *
_Functions = ['run']
def run():
print "Starting"
print "Loading Dataset"
# Retrieve dataset
data = getData('B1.dat')[:100]
#data = list()
#for i in range(100):
# data.append( array( [gauss(2.0,.1), gauss(0.0,.1) ]) )
#for i in range(100):
# data.append( array( [gauss(0.0,.1), gauss(2.0,.1) ]) )
print "Constructing Module"
mod = inference_module(data,gm=2,nu=.1)
print "Parsing Results"
clusters = [ [] , ]*(len(mod.clusters))
bsv = list()
colors = ['b','r','g','c','m','y','w']
for point in data:
vector = mod.induce( data_vector(point),False)
if vector.cluster != None:
if not clusters[vector.cluster]:
clusters[vector.cluster] = [vector,]
else:
clusters[vector.cluster].append(vector)
else:
bsv.append(vector)
print "Formatting Results"
for cluster in clusters:
if cluster:
scatter( [point.data[0] for point in cluster], [point.data[1] for point in cluster],s=10, c=colors[cluster[0].cluster % 7])
for cluster in mod.clusters:
scatter( [SV.data[0] for SV in cluster], [SV.data[1] for SV in cluster], s=30, c=colors[cluster[0].cluster % 7], label="Cluster %s" % cluster[0].cluster )
if bsv:
scatter( [v.data[0] for v in bsv], [v.data[1] for v in bsv], s=10, marker="x" )
title('%s SV in %s clusters from %s points' % (len(mod.SV),len(mod.clusters),len(data)))
show()
def help():
print __doc__
return 0
def process(arg='run'):
if arg in _Functions:
globals()[arg]()
class Usage(Exception): def __init__(self, msg): self.msg = msgdef main(argv=None): if argv is None: argv = sys.argv try: try: opts, args = getopt.getopt(sys.argv[1:], "hl:d:", ["help","list=","database="]) except getopt.error, msg: raise Usage(msg)
# process options for o, a in opts: if o in ("-h", "--help"):
for f in _Functions:
if f in args:
apply(f,(opts,args))
return 0 help()
# process arguments for arg in args: process(arg) # process() is defined elsewhere
except Usage, err: print >>sys.stderr, err.msg print >>sys.stderr, "for help use --help" return 2if __name__ == "__main__": sys.exit(main())