Skip to content

Commit

Permalink
Simplified to only include enet_path (mlflow#134)
Browse files Browse the repository at this point in the history
removed lasso calls and cleaned up the code
  • Loading branch information
dennyglee authored and mateiz committed Jul 6, 2018
1 parent 26a7603 commit edbf37f
Showing 1 changed file with 18 additions and 52 deletions.
70 changes: 18 additions & 52 deletions example/tutorial/train_diabetes.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#
# train_diabetes.py
#
# MLflow model using ElasticNet (sklearn) and Plots Lasso vs. ElasticNet Descent Paths
# MLflow model using ElasticNet (sklearn) and Plots ElasticNet Descent Paths
#
# Uses the sklearn Diabetes dataset to predict diabetes progression using ElasticNet
# The predicted "progression" column is a quantitative measure of disease progression one year after baseline
# http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_diabetes.html
# Combines the above with the Lasso Descent Path Plot
# Combines the above with the Lasso Coordinate Descent Path Plot
# http://scikit-learn.org/stable/auto_examples/linear_model/plot_lasso_coordinate_descent_path.html
# Original author: Alexandre Gramfort <alexandre.gramfort@inria.fr>; License: BSD 3 clause
#
# Usage:
# python train_diabetes.py 0.01 0.01
# python train_diabetes.py 0.01 0.75
# python train_diabetes.py 0.01 1.0
#

import os
import warnings
Expand Down Expand Up @@ -91,69 +96,30 @@ def eval_metrics(actual, pred):
# Compute paths
eps = 5e-3 # the smaller it is the longer is the path

print("Computing regularization path using the lasso...")
alphas_lasso, coefs_lasso, _ = lasso_path(X, y, eps, fit_intercept=False)

print("Computing regularization path using the positive lasso...")
alphas_positive_lasso, coefs_positive_lasso, _ = lasso_path(X, y, eps, positive=True, fit_intercept=False)

print("Computing regularization path using the elastic net...")
alphas_enet, coefs_enet, _ = enet_path(X, y, eps=eps, l1_ratio=0.8, fit_intercept=False)

print("Computing regularization path using the positive elastic net...")
alphas_positive_enet, coefs_positive_enet, _ = enet_path(X, y, eps=eps, l1_ratio=0.8, positive=True, fit_intercept=False)

print("Computing regularization path using the elastic net.")
alphas_enet, coefs_enet, _ = enet_path(X, y, eps=eps, l1_ratio=l1_ratio, fit_intercept=False)

# Display results
fig1 = plt.figure(1)
fig = plt.figure(1)
ax = plt.gca()

colors = cycle(['b', 'r', 'g', 'c', 'k'])
neg_log_alphas_lasso = -np.log10(alphas_lasso)
neg_log_alphas_enet = -np.log10(alphas_enet)
for coef_l, coef_e, c in zip(coefs_lasso, coefs_enet, colors):
l1 = plt.plot(neg_log_alphas_lasso, coef_l, c=c)
for coef_e, c in zip(coefs_enet, colors):
l2 = plt.plot(neg_log_alphas_enet, coef_e, linestyle='--', c=c)

plt.xlabel('-Log(alpha)')
plt.ylabel('coefficients')
plt.title('Lasso and Elastic-Net Paths')
plt.legend((l1[-1], l2[-1]), ('Lasso', 'Elastic-Net'), loc='lower left')
plt.axis('tight')

fig2 = plt.figure(2)
ax = plt.gca()
neg_log_alphas_positive_lasso = -np.log10(alphas_positive_lasso)
for coef_l, coef_pl, c in zip(coefs_lasso, coefs_positive_lasso, colors):
l1 = plt.plot(neg_log_alphas_lasso, coef_l, c=c)
l2 = plt.plot(neg_log_alphas_positive_lasso, coef_pl, linestyle='--', c=c)

plt.xlabel('-Log(alpha)')
plt.ylabel('coefficients')
plt.title('Lasso and positive Lasso')
plt.legend((l1[-1], l2[-1]), ('Lasso', 'positive Lasso'), loc='lower left')
title = 'ElasticNet Path by alpha for l1_ratio = ' + str(l1_ratio)
plt.title(title)
plt.axis('tight')


fig3 = plt.figure(3)
ax = plt.gca()
neg_log_alphas_positive_enet = -np.log10(alphas_positive_enet)
for (coef_e, coef_pe, c) in zip(coefs_enet, coefs_positive_enet, colors):
l1 = plt.plot(neg_log_alphas_enet, coef_e, c=c)
l2 = plt.plot(neg_log_alphas_positive_enet, coef_pe, linestyle='--', c=c)

plt.xlabel('-Log(alpha)')
plt.ylabel('coefficients')
plt.title('Elastic-Net and positive Elastic-Net')
plt.legend((l1[-1], l2[-1]), ('Elastic-Net', 'positive Elastic-Net'), loc='lower left')
plt.axis('tight')

# Save figures
fig1.savefig("plot1.png")
fig2.savefig("plot2.png")
fig3.savefig("plot3.png")
fig.savefig("ElasticNet-paths.png")

# Close plot
plt.close(fig)

# Log artifacts (output files)
mlflow.log_artifact("plot1.png")
mlflow.log_artifact("plot2.png")
mlflow.log_artifact("plot3.png")
mlflow.log_artifact("ElasticNet-paths.png")

0 comments on commit edbf37f

Please sign in to comment.