summaryrefslogtreecommitdiff
path: root/cross.py
blob: 09674f25e32e66a8b60eb0f6f59ddc35f526e733 (plain)
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
from misc import *

def onefold(inr,inm,tot,clist,folds,resm):

#clist=[0.0001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 10, 100, 1000]

#inr="a.rid"

#folds=4

#tot=316

#inm="%d.m"

#o=ite+1

    step = tot // folds

    ourb=inr

    for i in range(folds-1):
        inra=ourb
        oura="a%d.rid"%i
        ourb="b%d.rid"%i
        params = "-c %d -i %s -a %s -b %s" %(step,inra,oura,ourb)
        split(params)

    inra=ourb
    oura="a%d.rid"% (folds-1)
    params = "-s -i %s -a %s"%(inra,oura)
    split(params)

    for a in range(folds):
        entries=['0','0']
        for b in range(folds):
            if b!=a:
                tmp=take("a%d.rid"%b)
                entries = merge(tmp,entries)
        rid="b%d.rid" %a
        put(rid,entries)

    optc=1
    bcmc=folds

    for c in clist:
        print(c)
        oum="t%g.m" % c
        
        acmc = 0;
        for a in range(folds):
            rid = "b%d.rid"%a
            params = "-T -d -m %s -i %s -o %s -c %g" % (inm,rid,oum,c)
            train(params)
            rid="a%d.rid"%a
            params = "-V -C -m %s -i %s -s" %(oum,rid)
            acmc+=cmc(params)

        print(acmc/folds)

        if bcmc>acmc:
            optc=c
            bcmc=acmc
        else:
            if bcmc< acmc:
                break

    print("train with: %g" % optc)    
    params = "-T -d -m %s -i %s -o %s -c %g" % (inm,inr,resm,optc)
    train(params)