Ejemplo n.º 1
0
def linear_regression_lambda(N_points,t_set,lda = 1):
    '''Linear regresion algorithm with regularization
    from Y and X compute the dagger or pseudo matrix
    return the Xdagger.Y as the w vector
    default lambda is 1.0
    '''
    y_vector = target_vector(t_set)
    X_matrix = input_data_matrix(t_set)

    # weight decay term
    foo = eye(size(X_matrix,1))
    bar = pinv( dot(X_matrix.T, X_matrix) + lda * foo) 

    return dot(dot(bar, X_matrix.T), y_vector), X_matrix, y_vector
Ejemplo n.º 2
0
def compute_Eout(w, test_set):
    'number of out-of-sample points misclassifed/total number of out-of-sample points from data'
    
    X_matrix = input_data_matrix(test_set)
    y_vector = target_vector(test_set)
    g_vector = dot(X_matrix,w)
    for i in range(len(g_vector)):
        g_vector[i] = sign(g_vector[i])
    
    vEout = g_vector - y_vector
    nEout = 0
    for i in range(len(vEout)):
        if vEout[i]!=0:
            nEout = nEout + 1
    Eout = nEout/(len(vEout)*1.0)
    return Eout
Ejemplo n.º 3
0
def compute_Eout(wlin,N_points):
    'number of out-of-sample points misclassifed / total number of out-of-sample points'
    
    t_set = read_data_set("data/out.dta")
    
    X_matrix = input_data_matrix(t_set)
    y_vector = target_vector(t_set)

    g_vector = dot(X_matrix,wlin)
    
    for i in range(len(g_vector)):
        g_vector[i] = sign(g_vector[i])
    
    vEout = g_vector - y_vector
    nEout = 0
    for i in range(len(vEout)):
        if vEout[i]!=0:
            nEout = nEout + 1
    Eout = nEout/(len(vEout)*1.0)

    return Eout
Ejemplo n.º 4
0
def compute_Eout_nonlineartrans(w, N_points):
    'number of out-of-sample points misclassifed / total number of out-of-sample points'
    
    # generate N fresh points (f will not change) with noise
    t_set = read_data_set("data/out.dta")
    t_set_trans = transform_t_set(t_set)
    
    X_matrix = input_data_matrix(t_set_trans)
    y_vector = target_vector(t_set_trans)

    g_vector = dot(X_matrix,w)
    for i in range(len(g_vector)):
        g_vector[i] = sign(g_vector[i])
    
    vEout = g_vector - y_vector
    nEout = 0
    for i in range(len(vEout)):
        if vEout[i]!=0:
            nEout = nEout + 1
    Eout = nEout/(len(vEout)*1.0)
    return Eout
Ejemplo n.º 5
0
def run_LR_validation(t_set, v_set, k):
    '''
    Run linear regression on given feature set,
    and compute Ein and Eout
    '''

    N_points = len(t_set)

    'run LR on the training set'
    w, Xtrans, y = linear_regression(N_points, t_set)

    ' compute Ein using the validation set'
    valid_set = input_data_matrix(v_set)
    y_vector = target_vector(v_set)
    Eval = compute_Eval(w, valid_set, y_vector)

    'using the weights, compute Eval'
    Eout = compute_Eout(w, k)
    
    print 'k:' + str(k)
    print 'Eval: ' + str(Eval)
    print 'Eout: ' + str(Eout)