Beispiel #1
0
    for lambda_val in lasso_coeffs.keys():
        sse = sse_fn(lasso_coeffs[lambda_val])
        all_values[lambda_val] = sse

        if sse < min_sse:
            min_sse = sse
            min_lambda_val = lambda_val

    return min_lambda_val, all_values


if __name__ == '__main__':
    x_validate, y_validate = lassoData.lassoValData()
    x_training, y_training = lassoData.lassoTrainData()
    x_testing, y_testing = lassoData.lassoTestData()
    w_true = lassoData.lassoW()

    lambdas = [0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1]
    M = len(w_true)
    sin_basis = [
        lambda x: x, lambda x: math.sin(0.4 * math.pi * x * 1),
        lambda x: math.sin(0.4 * math.pi * x * 2),
        lambda x: math.sin(0.4 * math.pi * x * 3),
        lambda x: math.sin(0.4 * math.pi * x * 4),
        lambda x: math.sin(0.4 * math.pi * x * 5),
        lambda x: math.sin(0.4 * math.pi * x * 6),
        lambda x: math.sin(0.4 * math.pi * x * 7),
        lambda x: math.sin(0.4 * math.pi * x * 8),
        lambda x: math.sin(0.4 * math.pi * x * 9),
        lambda x: math.sin(0.4 * math.pi * x * 10),
Beispiel #2
0
            opt_lasso_theta = lasso_theta
        if mse_ridge < opt_ridge_mse:
            opt_ridge_lambda = l
            opt_ridge_mse = mse_ridge
            opt_ridge_theta = ridge_theta
    # return [lasso,ridge]
    # lasso = optimal (lambda,mse,theta)
    # ridge = optimatl (lambda,mse,theta)
    return [(opt_lasso_lambda, opt_lasso_mse, opt_lasso_theta),
            (opt_ridge_lambda, opt_ridge_mse, opt_ridge_theta)]


if __name__ == '__main__':
    x_valid, y_valid = lassoData.lassoValData()
    x_train, y_train = lassoData.lassoTrainData()
    x_test, y_test = lassoData.lassoTestData()
    w_true = lassoData.lassoW()

    M = len(w_true)

    sin_basis = [
        lambda x: x, lambda x: math.sin(0.4 * math.pi * x * 1),
        lambda x: math.sin(0.4 * math.pi * x * 2),
        lambda x: math.sin(0.4 * math.pi * x * 3),
        lambda x: math.sin(0.4 * math.pi * x * 4),
        lambda x: math.sin(0.4 * math.pi * x * 5),
        lambda x: math.sin(0.4 * math.pi * x * 6),
        lambda x: math.sin(0.4 * math.pi * x * 7),
        lambda x: math.sin(0.4 * math.pi * x * 8),
        lambda x: math.sin(0.4 * math.pi * x * 9),
        lambda x: math.sin(0.4 * math.pi * x * 10),
Beispiel #3
0
import math
import matplotlib.pyplot as plt


def compute_sinX(X, M):
    r = np.zeros((X.size, M))
    for i in range(X.size):
        r[i, 0] = X[i]
        for j in range(1, M):
            r[i, j] = math.sin(0.4 * j * math.pi * X[i])
    return r


x, y = ld.lassoTrainData()
xv, yv = ld.lassoValData()
xt, yt = ld.lassoTestData()
m = 13
l = 0.001
X = compute_sinX(x, m)
clf = linear_model.Lasso(alpha=l)
clf.fit(X, y)
clf1 = linear_model.Lasso(alpha=0.1)
clf1.fit(X, y)
clf2 = linear_model.Lasso(alpha=1)
clf2.fit(X, y)

plt.plot(x, y, 'bo', label='training')
plt.plot(xv, yv, 'ro', label='validation')
plt.plot(xt, yt, 'go', label='test')
xnew = np.linspace(min(x.min(), xt.min(), xv.min()),
                   max(x.max(), xt.max(), xv.max()), 100)
Beispiel #4
0
"""
Created on Wed Sep 28 00:40:15 2016

@author: loredp
"""

import sklearn.linear_model as skls
import lassoData
import pylab as pl
import functions as fun
import matplotlib.pyplot as plt
import numpy as np

train = lassoData.lassoTrainData()
val = lassoData.lassoValData()
test = lassoData.lassoTestData()

true_w = pl.loadtxt('lasso_true_w.txt')  #True value for the data

#Step 1: transform data

X = train[0]
Y = train[1]

(Xc, Yc) = fun.center_data(X, Y)

alpha = 0.2
fig = plt.figure()
fig.add_subplot(121)
w1 = fun.compare(X, Y, M=12, alpha=alpha, basis=fun.basis_sin)
plt.bar(range(13), w1[0], color='teal')
Beispiel #5
0
def test():
    Xtrain, Ytrain = lassoData.lassoTrainData()
    phi_train = design_matrix(Xtrain)
    print '{} data points for training'.format(len(Xtrain))
    Xval, Yval = lassoData.lassoValData()
    phi_val = design_matrix(Xval)
    print '{} data points for validation'.format(len(Xval))
    Xtest, Ytest = lassoData.lassoTestData()
    phi_test = design_matrix(Xtest)
    print '{} data points for testing'.format(len(Xtest))

    xx = np.linspace(-1, 1, 100)
    phi_plot = design_matrix(xx)

    plt.figure(3)
    plt.plot(Xtrain, Ytrain, 'o', label='Training')
    plt.plot(Xval, Yval, 'o', label='Validation')
    plt.plot(Xtest, Ytest, 'o', label='Testing')

    wtrue = np.loadtxt('lasso_true_w.txt').reshape((13, 1))
    plt.plot(xx, np.dot(phi_plot, wtrue), label='True model')

    lam = 0.01
    res_train = lasso(phi_train, Ytrain, lam)
    #res_val = lasso(phi_val,Yval,lam)
    #res_test = lasso(phi_test,Ytest,lam)

    plt.plot(xx, res_train.predict(phi_plot), label='LASSO')
    #plt.plot(xx,res_val.predict(phi_plot),label='LASSO validation')
    #plt.plot(xx,res_test.predict(phi_plot),label='LASSO testing')

    lam = 0.01
    res_ridge = ridge(phi_train, Ytrain, lam)
    plt.plot(xx, res_ridge.predict(phi_plot), label='Ridge')

    lam = 0
    res_noreg = lasso(phi_train, Ytrain, lam)
    plt.plot(xx, res_noreg.predict(phi_plot), label='$\lambda = 0$')

    plt.legend(loc='best')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.savefig('all_fitting.png')

    print 'Training error with wtrue  : {:.3f}'.format(
        error(wtrue, phi_train, Ytrain))
    print 'Training error with w lasso: {:.3f}'.format(
        error(res_train.coef_, phi_train, Ytrain))
    print 'Training error with w ridge: {:.3f}'.format(
        error(res_ridge.coef_, phi_train, Ytrain))
    print 'Training error with w noreg: {:.3f}'.format(
        error(res_noreg.coef_, phi_train, Ytrain))
    print 'Validation error with wtrue  : {:.3f}'.format(
        error(wtrue, phi_val, Yval))
    print 'Validation error with w lasso: {:.3f}'.format(
        error(res_train.coef_, phi_val, Yval))
    print 'Validation error with w ridge: {:.3f}'.format(
        error(res_ridge.coef_, phi_val, Yval))
    print 'Validation error with w noreg: {:.3f}'.format(
        error(res_noreg.coef_, phi_val, Yval))
    print 'Testing error with wtrue  : {:.3f}'.format(
        error(wtrue, phi_test, Ytest))
    print 'Testing error with w lasso: {:.3f}'.format(
        error(res_train.coef_, phi_test, Ytest))
    print 'Testing error with w ridge: {:.3f}'.format(
        error(res_ridge.coef_, phi_test, Ytest))
    print 'Testing error with w noreg: {:.3f}'.format(
        error(res_noreg.coef_, phi_test, Ytest))
    plot_w(wtrue, 'w true', 'w_true.png')
    plot_w(res_train.coef_, 'LASSO w', 'w_lasso.png')
    plot_w(res_ridge.coef_, 'ridge w', 'w_ridge.png')
    plot_w(res_noreg.coef_, 'unregularized w', 'w_noreg.png')