Пример #1
0
def run_linear_regression(N_samples, N_points):
    '''runs on N_samples and with N_points a linear regression
    computes Ein by average of the samples as well as Eout
    '''
    print 'running Linear Regression on %s samples' % str(N_samples)
    print 'Each sample has %s data points' % str(N_points)

    Ein_avg = []
    Eout_avg = []

    for i in range(N_samples):

        d = data(N_points)
        l = randomline()
        f = target_function(l)
        t_set = build_training_set(d, f)

        wlin, X, y = linear_regression(N_points, t_set)

        Ein = compute_Ein(wlin, X, y)
        Ein_avg.append(Ein)

        Eout = compute_Eout(wlin, f, N_points)
        Eout_avg.append(Eout)

    print_avg('Ein', Ein_avg)
    print_avg('Eout', Eout_avg)
Пример #2
0
def run_linear_regression(N_samples,N_points):
    '''runs on N_samples and with N_points a linear regression
    computes Ein by average of the samples as well as Eout
    '''
    print 'running Linear Regression on %s samples' %str(N_samples)
    print 'Each sample has %s data points' %str(N_points)

    Ein_avg = []
    Eout_avg = []

    for i in range(N_samples):

        d = data(N_points)
        l = randomline()
        f = target_function(l)
        t_set = build_training_set(d,f)

        wlin,X,y = linear_regression(N_points,t_set)

        Ein = compute_Ein(wlin,X,y)
        Ein_avg.append(Ein)

        Eout = compute_Eout(wlin,f,N_points)
        Eout_avg.append(Eout)
        
    print_avg('Ein',Ein_avg)
    print_avg('Eout',Eout_avg)
Пример #3
0
def run_lr_and_pla(N_samples, N_points):
    '''runs on N_samples and with N_points a linear regresion
    then from the weight vector runs PLA algorithm
    compute the average number of iterations of PLA with this w vector
    '''
    print 'running Linear Regression on %s samples' % N_samples
    print 'Each samples has %s data points' % N_points

    iteration_avg = []
    for i in range(N_samples):

        d = data(N_points)
        l = randomline()
        f = target_function(l)
        t_set = build_training_set(d, f)

        wlin, X, y = linear_regression(N_points, t_set)

        w_pla, iteration = PLA(N_points, wlin, f, t_set)
        iteration_avg.append(iteration)

    print_avg('Number of iterations', iteration_avg)
Пример #4
0
def run_lr_and_pla(N_samples, N_points):
    '''runs on N_samples and with N_points a linear regresion
    then from the weight vector runs PLA algorithm
    compute the average number of iterations of PLA with this w vector
    '''
    print 'running Linear Regression on %s samples' %N_samples
    print 'Each samples has %s data points' %N_points
    
    iteration_avg = []
    for i in range(N_samples):

        d = data(N_points)
        l = randomline()
        f = target_function(l)
        t_set = build_training_set(d,f)
        
        wlin,X,y = linear_regression(N_points,t_set)
        
        w_pla,iteration = PLA(N_points,wlin,f,t_set)
        iteration_avg.append(iteration)
    
    print_avg('Number of iterations',iteration_avg)
Пример #5
0
def run_nonlinear_transformation(N_samples, N_points):
    '''use N_samples to have a consistent result
    create a trainng set (1; x1; x2) from a constalation on N_points
    runs linear regration from training set
    computes Ein and averages it through all the samples
    transform the training set following (1; x1; x2; x1x2; x1^2; x2^2)
    run linear transformation on this transformed training set
    compute Ein of transformed t_set and average through all the samples
    create a hypothesis vector from the weight vector and the X matrix of the t_set transformed
    Average for each function g the difference between the hypothesis vector and the function
    finaly compute Eout from the f (target function) and the weight vector from training set that was not transformed
    '''
    Ein_avg = []
    Eout_avg = []
    Eintrans_avg = []
    EdiffA = []
    EdiffB = []
    EdiffC = []
    EdiffD = []
    EdiffE = []

    for i in range(N_samples):

        t_set, f = generate_t_set(N_points)
        wlin, X, y = linear_regression(N_points, t_set)
        Ein = compute_Ein(wlin, X, y)
        Ein_avg.append(Ein)

        #transform the training data into the following nonlinear feature vector:
        #(1; x1; x2; x1x2; x1^2; x2^2)
        t_set_trans = transform_t_set(t_set)
        wtrans, Xtrans, ytrans = linear_regression(N_points, t_set_trans)
        Eintrans = compute_Ein(wtrans, Xtrans, ytrans)
        Eintrans_avg.append(Eintrans)

        h_vector = sign(dot(Xtrans, wtrans))
        gA_vector = compute_g_vector(t_set_trans, 'a')
        Ediff_a = compute_avg_difference(h_vector, gA_vector)
        EdiffA.append(1 - Ediff_a)

        gB_vector = compute_g_vector(t_set_trans, 'b')
        Ediff_b = compute_avg_difference(h_vector, gB_vector)
        EdiffB.append(1 - Ediff_b)

        gC_vector = compute_g_vector(t_set_trans, 'c')
        Ediff_c = compute_avg_difference(h_vector, gC_vector)
        EdiffC.append(1 - Ediff_c)

        gD_vector = compute_g_vector(t_set_trans, 'd')
        Ediff_d = compute_avg_difference(h_vector, gD_vector)
        EdiffD.append(1 - Ediff_d)

        gE_vector = compute_g_vector(t_set_trans, 'e')
        Ediff_e = compute_avg_difference(h_vector, gE_vector)
        EdiffE.append(1 - Ediff_e)

        Eout = compute_Eout_nonlineartrans(wtrans, f, N_points)
        Eout_avg.append(Eout)

    print_avg('Ein', Ein_avg)
    print_avg('Ein Transformed', Eintrans_avg)
    print_avg('P of agreeing A', EdiffA)
    print_avg('P of agreeing B', EdiffB)
    print_avg('P of agreeing C', EdiffC)
    print_avg('P of agreeing D', EdiffD)
    print_avg('P of agreeing E', EdiffE)
    print_avg('Eout', Eout_avg)
Пример #6
0
def run_nonlinear_transformation(N_samples, N_points):
    '''use N_samples to have a consistent result
    create a trainng set (1; x1; x2) from a constalation on N_points
    runs linear regration from training set
    computes Ein and averages it through all the samples
    transform the training set following (1; x1; x2; x1x2; x1^2; x2^2)
    run linear transformation on this transformed training set
    compute Ein of transformed t_set and average through all the samples
    create a hypothesis vector from the weight vector and the X matrix of the t_set transformed
    Average for each function g the difference between the hypothesis vector and the function
    finaly compute Eout from the f (target function) and the weight vector from training set that was not transformed
    '''
    Ein_avg = []
    Eout_avg = []
    Eintrans_avg = []
    EdiffA = []
    EdiffB = []
    EdiffC = []
    EdiffD = []
    EdiffE = []

    for i in range(N_samples):

        t_set,f = generate_t_set(N_points)
        wlin,X,y = linear_regression(N_points,t_set)
        Ein = compute_Ein(wlin, X, y)
        Ein_avg.append(Ein)

        #transform the training data into the following nonlinear feature vector:
        #(1; x1; x2; x1x2; x1^2; x2^2)
        t_set_trans = transform_t_set(t_set)
        wtrans,Xtrans,ytrans = linear_regression(N_points,t_set_trans)
        Eintrans = compute_Ein(wtrans,Xtrans,ytrans)
        Eintrans_avg.append(Eintrans)
    
        h_vector =sign(dot(Xtrans,wtrans))
        gA_vector = compute_g_vector(t_set_trans,'a')
        Ediff_a = compute_avg_difference(h_vector,gA_vector)
        EdiffA.append(1-Ediff_a)
        
        gB_vector = compute_g_vector(t_set_trans,'b')
        Ediff_b = compute_avg_difference(h_vector,gB_vector)
        EdiffB.append(1-Ediff_b)

        gC_vector = compute_g_vector(t_set_trans,'c')
        Ediff_c = compute_avg_difference(h_vector,gC_vector)
        EdiffC.append(1-Ediff_c)
        
        gD_vector = compute_g_vector(t_set_trans,'d')
        Ediff_d = compute_avg_difference(h_vector,gD_vector)
        EdiffD.append(1-Ediff_d)
        
        gE_vector = compute_g_vector(t_set_trans,'e')
        Ediff_e = compute_avg_difference(h_vector,gE_vector)
        EdiffE.append(1-Ediff_e)

        Eout = compute_Eout_nonlineartrans(wtrans,f,N_points)
        Eout_avg.append(Eout)

    print_avg('Ein',Ein_avg)
    print_avg('Ein Transformed',Eintrans_avg)
    print_avg('P of agreeing A',EdiffA)
    print_avg('P of agreeing B',EdiffB)
    print_avg('P of agreeing C',EdiffC)
    print_avg('P of agreeing D',EdiffD)
    print_avg('P of agreeing E',EdiffE)
    print_avg('Eout',Eout_avg)