Ejemplo n.º 1
0
# # pl.plot(enet_objective,  label='Elastic Net')
# pl.xlabel('Iteration')
# pl.ylabel('Cost function')
# pl.legend()
# pl.title('Convergence')

################################################################################
# Demo path functions
################################################################################

print "Computing regularization path using the lasso..."
from datetime import datetime

start = datetime.now()

alphas_lasso, weights_lasso = lasso_path(X, y, factor=0.97, n_alphas=100)

delta = datetime.now() - start

print 'time taken: ', delta

# print "Computing regularization path using the elastic net..."
# alphas_enet, weights_enet = enet_path(X, y, factor=0.93, n_alphas = 50,
#                                                 beta=0.5)

# Display results
# pl.subplot(1, 2, 2)
color_iter = cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k'])
for color, weight_lasso in zip(color_iter, weights_lasso.T):
    pl.plot(-np.log(alphas_lasso), weight_lasso, color)
    # pl.plot(-np.log(alphas_enet), weight_enet, color+'x')
Ejemplo n.º 2
0
pl.clf()
pl.subplots_adjust(wspace=.4, right=.95)
pl.subplot(1, 2, 1)
pl.plot(lasso_objective, label='Lasso')
pl.plot(enet_objective, label='Elastic Net')
pl.xlabel('Iteration')
pl.ylabel('Cost function')
pl.legend()
pl.title('Convergence')

################################################################################
# Demo path functions
################################################################################

print "Computing regularization path using the lasso..."
alphas_lasso, weights_lasso = lasso_path(X, y, factor=0.93, n_alphas=50)

print "Computing regularization path using the elastic net..."
alphas_enet, weights_enet = enet_path(X, y, factor=0.93, n_alphas=50, beta=0.5)

# Display results
pl.subplot(1, 2, 2)
color_iter = cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k'])
for color, weight_lasso, weight_enet in zip(color_iter, weights_lasso.T,
                                            weights_enet.T):
    pl.plot(-np.log(alphas_lasso), weight_lasso, color)
    pl.plot(-np.log(alphas_enet), weight_enet, color + 'x')

pl.xlabel('-log(lambda)')
pl.ylabel('weights')
pl.title('Lasso and Elastic-Net Paths')
Ejemplo n.º 3
0
eps = 1e-2 # the smaller it is the longer is the path
=======
# print "Fitting lasso model to random data..."
>>>>>>> LOCAL
<<<<<<< REMOTE

=======
# lasso.fit(X, y, maxit=maxit)
>>>>>>> LOCAL
<<<<<<< REMOTE
start = datetime.now()
=======
# print "Duality gap Lasso (should be small): %f" % \
>>>>>>> LOCAL
<<<<<<< REMOTE
alphas_lasso, weights_lasso = lasso_path(X, y, eps=eps)
=======
#         lasso_dual_gap(X, y, lasso.coef_, alpha)[0]
>>>>>>> LOCAL
<<<<<<< REMOTE
print "This took ", datetime.now() - start
=======
# lasso_objective = lasso_objective_callback.values
>>>>>>> LOCAL
<<<<<<< REMOTE
alphas_enet, weights_enet = enet_path(X, y, rho=0.6, eps=eps)
=======
# alpha, beta = 1, 1
>>>>>>> LOCAL
<<<<<<< REMOTE
print "This took ", datetime.now() - start