diff --git a/scikits/learn/glm/coordinate_descent.py b/scikits/learn/glm/coordinate_descent.py index f63826c75a9874130cb5877554ade77e51ac14b7..570b97a468f5476131ce0bdbf3fb2e6615757b2f 100644 --- a/scikits/learn/glm/coordinate_descent.py +++ b/scikits/learn/glm/coordinate_descent.py @@ -156,137 +156,3 @@ def enet_path(X, y, factor=0.95, n_alphas=10, beta=1.0, **kwargs): alphas = np.asarray(alphas) weights = np.asarray(weights) return alphas, weights - -if __name__ == '__main__': - import time - import pylab as pl - - n_samples, n_features, maxit = 5, 10, 30 - np.random.seed(0) - y = np.random.randn(n_samples) - X = np.random.randn(n_samples, n_features) - - """Tests Lasso implementations (python and cython) - """ - - alpha = 1.0 - - tol = 1e-5 - - # Callbacks to store objective values and densities - callback_objective = IterationCallbackFunc(lasso_objective) - callback_density = IterationCallbackFunc(density) - - t0 = time.time() - lasso_slow = Lasso(alpha=alpha, callbacks=[callback_objective, - callback_density]) - lasso_slow.learner = lasso_coordinate_descent_slow - lasso_slow.fit(X, y, maxit=maxit, tol=tol) - print time.time() - t0 - - objective_convergence_slow = callback_objective.values - density_slow = callback_density.values - - print "Duality gap Lasso (should be small): %f" % \ - lasso_dual_gap(X, y, lasso_slow.coef_, alpha)[0] - - t0 = time.time() - lasso_fast = Lasso(alpha=alpha, callbacks=[callback_objective, - callback_density]) - lasso_fast.learner = lasso_coordinate_descent_fast - lasso_fast.fit(X, y, maxit=maxit, tol=tol) - print time.time() - t0 - - print "Duality gap Lasso (should be small): %f" % \ - lasso_dual_gap(X, y, lasso_slow.coef_, alpha)[0] - - objective_convergence_fast = callback_objective.values - density_fast = callback_density.values - - pl.close('all') - pl.plot(objective_convergence_fast,"rx-") - pl.plot(objective_convergence_slow,"bo--") - pl.xlabel('Iteration') - pl.ylabel('Cost function') - pl.legend(['Fast', 'Slow']) - pl.title('Lasso') - - pl.figure() - pl.plot(density_fast,"rx-") - pl.plot(density_slow,"bo--") - pl.xlabel('Iteration') - pl.ylabel('Density') - pl.legend(['Fast', 'Slow']) - pl.title('Lasso') - - """Tests Elastic-Net implementations (python and cython) - """ - - alpha = 1.0 - beta = 1.0 - - callback_objective = IterationCallbackFunc(enet_objective) - - import time - t0 = time.time() - enet_slow = ElasticNet(alpha=alpha, beta=beta, callbacks=[callback_objective, - callback_density]) - enet_slow.learner = enet_coordinate_descent_slow - enet_slow.fit(X, y, maxit=maxit) - print time.time() - t0 - - print "Duality gap (should be small): %f" % \ - enet_dual_gap(X, y, enet_slow.coef_, alpha)[0] - - objective_convergence_slow = callback_objective.values - density_slow = callback_density.values - - t0 = time.time() - enet_fast = ElasticNet(alpha=alpha, beta=beta, callbacks=[callback_objective, - callback_density]) - - enet_fast.learner = enet_coordinate_descent_fast - enet_fast.fit(X, y, maxit=maxit) - print time.time() - t0 - - print "Duality gap (should be small): %f" % \ - enet_dual_gap(X, y, enet_fast.coef_, alpha)[0] - - objective_convergence_fast = callback_objective.values - density_fast = callback_density.values - - pl.figure() - pl.plot(objective_convergence_fast,"rx-") - pl.plot(objective_convergence_slow,"bo--") - pl.xlabel('Iteration') - pl.ylabel('Cost function') - pl.legend(['Fast', 'Slow']) - pl.title('Elastic-Net') - - pl.figure() - pl.plot(density_fast,"rx-") - pl.plot(density_slow,"bo--") - pl.xlabel('Iteration') - pl.ylabel('Density') - pl.legend(['Fast', 'Slow']) - pl.title('Elastic-Net') - - """Test path functions - """ - - alphas_lasso, weights_lasso = lasso_path(X, y, factor=0.97, n_alphas = 100, tol=1-2) - alphas_enet, weights_enet = enet_path(X, y, factor=0.97, n_alphas = 100, beta=0.1, tol=1-2) - - from itertools import cycle - color_iter = cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k']) - - pl.figure() - 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') - pl.legend(['Lasso','Elastic-Net']) - pl.show() -