|
28 | 28 | # Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>
|
29 | 29 | # License: BSD 3 clause
|
30 | 30 |
|
31 |
| -from time import time |
32 |
| -import numpy as np |
33 |
| -import matplotlib.pyplot as plt |
| 31 | +# %% |
| 32 | +# Load data |
| 33 | +# --------- |
34 | 34 |
|
35 |
| -from sklearn import linear_model |
36 | 35 | from sklearn import datasets
|
37 |
| -from sklearn.svm import l1_min_c |
38 | 36 |
|
39 | 37 | iris = datasets.load_iris()
|
40 | 38 | X = iris.data
|
|
45 | 43 |
|
46 | 44 | X /= X.max() # Normalize X to speed-up convergence
|
47 | 45 |
|
48 |
| -# ############################################################################# |
49 |
| -# Demo path functions |
| 46 | +# %% |
| 47 | +# Compute regularization path |
| 48 | +# --------------------------- |
50 | 49 |
|
51 |
| -cs = l1_min_c(X, y, loss="log") * np.logspace(0, 7, 16) |
| 50 | +import numpy as np |
| 51 | + |
| 52 | +from sklearn import linear_model |
| 53 | +from sklearn.svm import l1_min_c |
52 | 54 |
|
| 55 | +cs = l1_min_c(X, y, loss="log") * np.logspace(0, 7, 16) |
53 | 56 |
|
54 |
| -print("Computing regularization path ...") |
55 |
| -start = time() |
56 | 57 | clf = linear_model.LogisticRegression(
|
57 | 58 | penalty="l1",
|
58 | 59 | solver="liblinear",
|
|
66 | 67 | clf.set_params(C=c)
|
67 | 68 | clf.fit(X, y)
|
68 | 69 | coefs_.append(clf.coef_.ravel().copy())
|
69 |
| -print("This took %0.3fs" % (time() - start)) |
70 | 70 |
|
71 | 71 | coefs_ = np.array(coefs_)
|
| 72 | + |
| 73 | +# %% |
| 74 | +# Plot regularization path |
| 75 | +# ------------------------ |
| 76 | + |
| 77 | +import matplotlib.pyplot as plt |
| 78 | + |
72 | 79 | plt.plot(np.log10(cs), coefs_, marker="o")
|
73 | 80 | ymin, ymax = plt.ylim()
|
74 | 81 | plt.xlabel("log(C)")
|
|
0 commit comments