Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import matplotlib
|
3 |
+
|
4 |
+
matplotlib.use("Agg")
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import gradio as gr
|
7 |
+
from sklearn import datasets
|
8 |
+
from sklearn import linear_model
|
9 |
+
from sklearn.svm import l1_min_c
|
10 |
+
|
11 |
+
|
12 |
+
def train_it(solver, intersect_scaling, tol, max_iter):
|
13 |
+
iris = datasets.load_iris()
|
14 |
+
X = iris.data
|
15 |
+
y = iris.target
|
16 |
+
|
17 |
+
X = X[y != 2]
|
18 |
+
y = y[y != 2]
|
19 |
+
|
20 |
+
X /= X.max()
|
21 |
+
|
22 |
+
cs = l1_min_c(X, y, loss="log") * np.logspace(0, 7, 16)
|
23 |
+
|
24 |
+
clf = linear_model.LogisticRegression(
|
25 |
+
penalty="l1",
|
26 |
+
solver=solver,
|
27 |
+
tol=tol,
|
28 |
+
max_iter=int(max_iter),
|
29 |
+
warm_start=True,
|
30 |
+
intercept_scaling=intersect_scaling,
|
31 |
+
)
|
32 |
+
|
33 |
+
coefs_ = []
|
34 |
+
for c in cs:
|
35 |
+
clf.set_params(C=c)
|
36 |
+
clf.fit(X, y)
|
37 |
+
coefs_.append(clf.coef_.ravel().copy())
|
38 |
+
|
39 |
+
coefs_ = np.array(coefs_)
|
40 |
+
|
41 |
+
plt.plot(np.log10(cs), coefs_, marker="o")
|
42 |
+
ymin, ymax = plt.ylim()
|
43 |
+
plt.xlabel("log(C)")
|
44 |
+
plt.ylabel("Coefficients")
|
45 |
+
plt.title("Logistic Regression Path")
|
46 |
+
plt.axis("tight")
|
47 |
+
plt.show()
|
48 |
+
|
49 |
+
return plt
|
50 |
+
|
51 |
+
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
gr.Markdown("# Regularization path of L1- Logistic Regression")
|
54 |
+
gr.Markdown(
|
55 |
+
"""
|
56 |
+
This interactive demo is based on the [Regularization path of L1- Logistic Regression] (https://scikit-learn.org/stable/auto_examples/linear_model/plot_logistic_path.html).This demonstrates how to perform l1-penalized logistic regression on a binary classification problem derived from the Iris dataset. The regularization path plots the progression of the coefficients from exactly 0 to non-zero values as the regularization becomes progressively looser.
|
57 |
+
"""
|
58 |
+
)
|
59 |
+
|
60 |
+
with gr.Row():
|
61 |
+
with gr.Column():
|
62 |
+
solver = gr.Dropdown(
|
63 |
+
["liblinear", "saga"], label="Solver", value="liblinear"
|
64 |
+
)
|
65 |
+
warm_start = gr.Dropdown(
|
66 |
+
["True", "False"], label="Warm Start", value="True"
|
67 |
+
)
|
68 |
+
with gr.Column(align="center"):
|
69 |
+
intersect_scaling = gr.Slider(
|
70 |
+
value=10000.0,
|
71 |
+
minimum=0,
|
72 |
+
maximum=100000,
|
73 |
+
step=0.1,
|
74 |
+
label="Intersect Scaling",
|
75 |
+
)
|
76 |
+
tol = gr.Slider(
|
77 |
+
value=1e-6, minimum=0, maximum=1, step=0.1, label="Tolerance"
|
78 |
+
)
|
79 |
+
max_iter = gr.Slider(
|
80 |
+
value=1e6,
|
81 |
+
minimum=0,
|
82 |
+
maximum=1000000,
|
83 |
+
step=0.1,
|
84 |
+
label="Maximum Iterations",
|
85 |
+
)
|
86 |
+
train_buttion = gr.Button(label="Train")
|
87 |
+
|
88 |
+
train_buttion.click(
|
89 |
+
train_it, inputs=[solver, intersect_scaling, tol, max_iter], outputs=gr.Plot()
|
90 |
+
)
|
91 |
+
|
92 |
+
|
93 |
+
demo.launch()
|