From f2c7b437094c1e24e26a5eba2581ea45ecbf89a8 Mon Sep 17 00:00:00 2001 From: alexanmv <56640707+alexanmv@users.noreply.github.com> Date: Fri, 18 Feb 2022 16:39:34 +0200 Subject: [PATCH 1/2] Update plot_logistic_path.py making the '# ####' comment line to '# %%' type and making a header for demo path functions. Also made a header with '# %%' type for author and license. Thought also about making author and license into note style but header seemed better. --- examples/linear_model/plot_logistic_path.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/examples/linear_model/plot_logistic_path.py b/examples/linear_model/plot_logistic_path.py index b74d6a67021d0..014bf8ad5c164 100644 --- a/examples/linear_model/plot_logistic_path.py +++ b/examples/linear_model/plot_logistic_path.py @@ -25,8 +25,15 @@ """ -# Author: Alexandre Gramfort -# License: BSD 3 clause +# %% +# Author +# --------------------------------------------------- +# Alexandre Gramfort + +# %% +# License +# --------------------------------------------------- +# BSD 3 clause from time import time import numpy as np @@ -45,8 +52,9 @@ X /= X.max() # Normalize X to speed-up convergence -# ############################################################################# +# %% # Demo path functions +# --------------------------------------------------- cs = l1_min_c(X, y, loss="log") * np.logspace(0, 7, 16) From 1bcc7ab4ca2e1fc430fa8c665fc436cfdf3c2d7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Tue, 22 Feb 2022 11:22:13 +0100 Subject: [PATCH 2/2] Tweak example --- examples/linear_model/plot_logistic_path.py | 37 ++++++++++----------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/examples/linear_model/plot_logistic_path.py b/examples/linear_model/plot_logistic_path.py index 014bf8ad5c164..a52b082eaed32 100644 --- a/examples/linear_model/plot_logistic_path.py +++ b/examples/linear_model/plot_logistic_path.py @@ -25,23 +25,14 @@ """ -# %% -# Author -# --------------------------------------------------- -# Alexandre Gramfort +# Author: Alexandre Gramfort +# License: BSD 3 clause # %% -# License -# --------------------------------------------------- -# BSD 3 clause +# Load data +# --------- -from time import time -import numpy as np -import matplotlib.pyplot as plt - -from sklearn import linear_model from sklearn import datasets -from sklearn.svm import l1_min_c iris = datasets.load_iris() X = iris.data @@ -53,14 +44,16 @@ X /= X.max() # Normalize X to speed-up convergence # %% -# Demo path functions -# --------------------------------------------------- +# Compute regularization path +# --------------------------- -cs = l1_min_c(X, y, loss="log") * np.logspace(0, 7, 16) +import numpy as np +from sklearn import linear_model +from sklearn.svm import l1_min_c + +cs = l1_min_c(X, y, loss="log") * np.logspace(0, 7, 16) -print("Computing regularization path ...") -start = time() clf = linear_model.LogisticRegression( penalty="l1", solver="liblinear", @@ -74,9 +67,15 @@ clf.set_params(C=c) clf.fit(X, y) coefs_.append(clf.coef_.ravel().copy()) -print("This took %0.3fs" % (time() - start)) coefs_ = np.array(coefs_) + +# %% +# Plot regularization path +# ------------------------ + +import matplotlib.pyplot as plt + plt.plot(np.log10(cs), coefs_, marker="o") ymin, ymax = plt.ylim() plt.xlabel("log(C)")