Exemplo n.º 1
0
def run_pla_vs_svm(nbruns=1, N=10):
    solvers.options['show_progress'] = False

    d = []
    l = 0
    f = 0
    t_set = []
    y = []
    svm_vs_pla = []
    for i in range(nbruns):
        onBothSides = False
        while (not onBothSides):
            d = data(N)
            l = randomline()
            f = target_function(l)
            t_set = build_training_set(d, f)
            y = target_vector(t_set)
            if (1 in y) and (-1 in y):
                onBothSides = True
            else:
                onBothSides = False
        w = [0, 0, 0]
        w_pla, iteration = PLA(N, w, f, t_set)
        plaEout = evaluate_diff_f_g(f, w_pla)
        X_matrix = input_data_matrix(t_set)
        dimension = len(X_matrix[0])
        #identity matrix of size dim X dim matrix x,I,J,typecode double
        P = spmatrix(1, range(dimension), range(dimension), tc='d')
        #vector of zeros of size dim, typecode double
        q = matrix([0] * (dimension), tc='d')

        mat = []
        for t in t_set:
            y = t[1]
            temp = [x * -1.0 * y for x in t[0]]
            mat.append(temp)

        G = matrix(mat, tc='d')
        G = G.trans()
        # vectors of -1 of size t_set
        h = matrix([-1] * len(t_set), tc='d')
        #http://abel.ee.ucla.edu/cvxopt/examples/tutorial/qp.html
        qp_sln = solvers.qp(P, q, G, h)
        wsvm = list(qp_sln['x'])
        # number of support vectors you can get at each run
        count_sv = 0
        for t in t_set:
            wsvm = array(wsvm)
            x = array(t[0])
            y = t[1]
            res = fabs(y * dot(wsvm, x) - 1)
            if res < 0.001:
                count_sv = count_sv + 1
        #print count_sv
        # Eout of svm
        svmEout = computeEout_svm(f, wsvm)
        #print 'svmEout: %s'%svmEout
        if (svmEout < plaEout):
            svm_vs_pla.append([True, count_sv])
        else:
            svm_vs_pla.append([False, count_sv])

    print "svm win pla %f" % (len(filter(lambda a: a[0] is True, svm_vs_pla)) *
                              1.0 / N)
    percent_svm_won = len([r[0] for r in svm_vs_pla if r[0] is True
                           ]) * 1.0 / len(svm_vs_pla)
    print "question 9: svm beat pla %f percent of the time" % (
        percent_svm_won * 100)

    avg_sv = sum([a[1] for a in svm_vs_pla]) * 1.0 / len(svm_vs_pla)
    print "avg sv:", avg_sv
Exemplo n.º 2
0
def run_pla_vs_svm(nbruns = 1, N = 10):
    solvers.options['show_progress'] = False
    
    d = []
    l = 0
    f = 0
    t_set = []
    y = []
    svm_vs_pla = []
    for i in range(nbruns):
        onBothSides = False
        while(not onBothSides):
            d = data(N)
            l = randomline()
            f = target_function(l)
            t_set = build_training_set(d,f)
            y = target_vector(t_set)
            if (1 in y) and (-1 in y):
                onBothSides = True
            else:
                onBothSides = False
        w = [0,0,0]
        w_pla,iteration = PLA(N,w,f,t_set)
        plaEout = evaluate_diff_f_g(f,w_pla)
        X_matrix = input_data_matrix(t_set)
        dimension = len(X_matrix[0])
        #identity matrix of size dim X dim matrix x,I,J,typecode double
        P = spmatrix(1, range(dimension), range(dimension), tc='d')
        #vector of zeros of size dim, typecode double
        q = matrix([0]*(dimension), tc='d')

        mat = []
        for t in t_set:
            y = t[1]
            temp = [x * -1.0*y for x in t[0]]
            mat.append(temp) 
        
        G = matrix(mat, tc='d')
        G = G.trans()
        # vectors of -1 of size t_set
        h = matrix([-1]*len(t_set), tc='d')
        #http://abel.ee.ucla.edu/cvxopt/examples/tutorial/qp.html
        qp_sln = solvers.qp(P, q, G, h)
        wsvm = list(qp_sln['x'])
        # number of support vectors you can get at each run
        count_sv = 0
        for t in t_set:
            wsvm = array(wsvm)
            x = array(t[0])
            y = t[1]
            res = fabs(y*dot(wsvm,x)-1)
            if res < 0.001:
                count_sv = count_sv + 1
        #print count_sv
        # Eout of svm
        svmEout = computeEout_svm(f,wsvm)
        #print 'svmEout: %s'%svmEout
        if(svmEout < plaEout):
            svm_vs_pla.append([True,count_sv])
        else:
            svm_vs_pla.append([False,count_sv])

    print "svm win pla %f" % (len(filter(lambda a: a[0] is True, svm_vs_pla))*1.0/N) 
    percent_svm_won = len([r[0] for r in svm_vs_pla if r[0] is True])*1.0/len(svm_vs_pla)
    print "question 9: svm beat pla %f percent of the time" % (percent_svm_won*100)

    avg_sv = sum([a[1] for a in svm_vs_pla])*1.0/len(svm_vs_pla) 
    print "avg sv:", avg_sv 
Exemplo n.º 3
0
def computeEout_svm(f, w):
    return evaluate_diff_f_g(f, w)
Exemplo n.º 4
0
def computeEout_svm(f,w):
    return evaluate_diff_f_g(f,w)