Example #1
0
def CART(trainData):

    if len(getClassesByData(trainData)) == 1:
        leaf = {"type": "leaf", "class": trainData[0]["y"]}
        return leaf

    minE = len(trainData)
    minT = 0.
    minD = 0
    dimN = len(trainData[0]["x"])
    for i in xrange(dimN):
        dimX = getDimN(trainData, i)
        hyps = buildHyp(dimX)
        for th in hyps:
            da, db = splitData(trainData, i, th)
            ip = imp(da) + imp(db)
            #print i,th,ip

            if ip < minE:
                minE = ip
                minT = th
                minD = i

    da, db = splitData(trainData, minD, minT)
    #print minD,minT,minE,len(da),imp(da),len(db),imp(db)

    node = {"type": "node", "dim": minD, "th": minT}

    rt = CART(da)
    rl = CART(db)

    node["rtree"] = rt
    node["ltree"] = rl

    return node
Example #2
0
def Ads(trainData,U):
    dimN = len(trainData[0]["x"])
    sign = [1,-1]

    err = len(trainData) 
    en  = len(trainData)
    dim = 0
    th  = 0
    sig = 1
    meu = 0. 
    mcu = 0. 
    for i in range(dimN):
        hyps = buildHyp(getDimN(trainData,i))
        for s in sign:
            for h in hyps:
                eu,cu,n = getEviaDSu(trainData,U,s,i,h)
                #print "\t",i,s,h
                if eu < err:
                    err = eu
                    meu = eu
                    mcu = cu
                    en  = n
                    dim = i
                    th  = h
                    sig = s

    #return Algo
    err = meu/(meu+mcu)
    #print en,sum(U),err,meu,mcu
    #return {}
    de = math.sqrt((1.0-err)/err)
    algo = {"err":err,"sign":sig,"dim":dim,"th":th,"de":de,"alpha":math.log(de),"en":en}
    return algo
Example #3
0
def CART(trainData):
    
    if len(getClassesByData(trainData)) == 1:
        leaf = {"type":"leaf","class":trainData[0]["y"]}
        return leaf

    minE = len(trainData)
    minT = 0.
    minD = 0
    dimN = len(trainData[0]["x"])
    for i in xrange(dimN):
        dimX = getDimN(trainData,i)
        hyps = buildHyp(dimX)
        for th in hyps:
            da,db = splitData(trainData,i,th)
            ip = imp(da) + imp(db)
            #print i,th,ip

            if ip < minE:
                minE = ip
                minT = th
                minD = i
    
    da,db = splitData(trainData,minD,minT)
    #print minD,minT,minE,len(da),imp(da),len(db),imp(db)

    node = {"type":"node","dim":minD,"th":minT}

    rt = CART(da)
    rl = CART(db)

    node["rtree"] = rt
    node["ltree"] = rl

    return node
Example #4
0
def Ads(trainData, U):
    dimN = len(trainData[0]["x"])
    sign = [1, -1]

    err = len(trainData)
    en = len(trainData)
    dim = 0
    th = 0
    sig = 1
    meu = 0.
    mcu = 0.
    for i in range(dimN):
        hyps = buildHyp(getDimN(trainData, i))
        for s in sign:
            for h in hyps:
                eu, cu, n = getEviaDSu(trainData, U, s, i, h)
                #print "\t",i,s,h
                if eu < err:
                    err = eu
                    meu = eu
                    mcu = cu
                    en = n
                    dim = i
                    th = h
                    sig = s

    #return Algo
    err = meu / (meu + mcu)
    #print en,sum(U),err,meu,mcu
    #return {}
    de = math.sqrt((1.0 - err) / err)
    algo = {
        "err": err,
        "sign": sig,
        "dim": dim,
        "th": th,
        "de": de,
        "alpha": math.log(de),
        "en": en
    }
    return algo