8000 DOC: use notebook-style for plot_logistic_path.py by alexanmv · Pull Request #22536 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

DOC: use notebook-style for plot_logistic_path.py #22536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions examples/linear_model/plot_logistic_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>
# License: BSD 3 clause

from time import time
import numpy as np
import matplotlib.pyplot as plt
# %%
# Load data
# ---------

from sklearn import linear_model
from sklearn import datasets
from sklearn.svm import l1_min_c

iris = datasets.load_iris()
X = iris.data
Expand All @@ -45,14 +43,17 @@

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",
Expand All @@ -66,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)")
Expand Down
0