update project
Browse files- .vscode/settings.json +15 -0
- .vscode/tasks.json +29 -0
- app.py +139 -144
- pyproject.toml +1 -1
.vscode/settings.json
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"files.trimFinalNewlines": true,
|
3 |
+
"files.trimTrailingWhitespace": true,
|
4 |
+
"editor.formatOnPaste": true,
|
5 |
+
"editor.formatOnSave": true,
|
6 |
+
"python.linting.pylintEnabled": true,
|
7 |
+
"python.testing.pytestEnabled": true,
|
8 |
+
"python.testing.pytestArgs": [
|
9 |
+
"tests"
|
10 |
+
],
|
11 |
+
"python.formatting.provider": "black",
|
12 |
+
"conventionalCommits.scopes": [
|
13 |
+
"core"
|
14 |
+
]
|
15 |
+
}
|
.vscode/tasks.json
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
// See https://go.microsoft.com/fwlink/?LinkId=733558
|
3 |
+
// for the documentation about the tasks.json format
|
4 |
+
"version": "2.0.0",
|
5 |
+
"tasks": [
|
6 |
+
{
|
7 |
+
"label": "echo",
|
8 |
+
// "type": "shell",
|
9 |
+
"command": "echo ${file}"
|
10 |
+
},
|
11 |
+
{
|
12 |
+
"label": "Export Notebook",
|
13 |
+
// "type": "shell",
|
14 |
+
// "type": "process",
|
15 |
+
"cwd": "${workspaceFolder}",
|
16 |
+
"command": "${command:jupyter.exportAsPythonScript}"
|
17 |
+
},
|
18 |
+
{
|
19 |
+
"label": "Show File",
|
20 |
+
"cwd": "${workspaceFolder}",
|
21 |
+
"command": "echo ${file}"
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"label": "pre-commit",
|
25 |
+
"type": "shell",
|
26 |
+
"command": "poetry run pre-commit run -a"
|
27 |
+
}
|
28 |
+
]
|
29 |
+
}
|
app.py
CHANGED
@@ -1,24 +1,30 @@
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import matplotlib.pyplot as plt
|
4 |
import matplotlib
|
5 |
-
|
6 |
-
|
7 |
from sklearn.kernel_approximation import Nystroem
|
|
|
8 |
from sklearn.pipeline import make_pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
font = {"weight": "normal", "size": 15}
|
11 |
|
12 |
matplotlib.rc("font", **font)
|
13 |
|
14 |
random_state = 42
|
15 |
-
rng = np.random.
|
|
|
16 |
|
17 |
# Generate train data
|
18 |
-
X = 0.3 * rng.
|
19 |
X_train = np.r_[X + 2, X - 2]
|
20 |
# Generate some regular novel observations
|
21 |
-
X = 0.3 * rng.
|
22 |
X_test = np.r_[X + 2, X - 2]
|
23 |
# Generate some abnormal novel observations
|
24 |
X_outliers = rng.uniform(low=-4, high=4, size=(20, 2))
|
@@ -29,14 +35,6 @@ xx, yy = np.meshgrid(np.linspace(-4.5, 4.5, 50), np.linspace(-4.5, 4.5, 50))
|
|
29 |
# nu = 0.05
|
30 |
# gamma = 2.0
|
31 |
|
32 |
-
md_description = """
|
33 |
-
# A 1D regression with decision tree.
|
34 |
-
|
35 |
-
The [decision trees](https://scikit-learn.org/stable/modules/tree.html#tree) is used to fit a sine curve with addition noisy observation. As a result, it learns local linear regressions approximating the sine curve.
|
36 |
-
|
37 |
-
We can see that if the maximum depth of the tree (controlled by the max_depth parameter) is set too high, the decision trees learn too fine details of the training data and learn from the noise, i.e. they overfit.
|
38 |
-
"""
|
39 |
-
|
40 |
|
41 |
def make_regression(nu, gamma):
|
42 |
clf = OneClassSVM(gamma=gamma, kernel="rbf", nu=nu)
|
@@ -51,7 +49,6 @@ def make_regression(nu, gamma):
|
|
51 |
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
52 |
Z = Z.reshape(xx.shape)
|
53 |
|
54 |
-
|
55 |
# Fit the One-Class SVM using a kernel approximation and SGD
|
56 |
transform = Nystroem(gamma=gamma, random_state=random_state)
|
57 |
clf_sgd = SGDOneClassSVM(
|
@@ -68,17 +65,15 @@ def make_regression(nu, gamma):
|
|
68 |
|
69 |
Z_sgd = pipe_sgd.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
70 |
Z_sgd = Z_sgd.reshape(xx.shape)
|
71 |
-
|
72 |
-
def
|
73 |
-
# plot the level sets of the decision function
|
74 |
fig = plt.figure(figsize=(9, 6))
|
75 |
-
# fig, ax = plt.subplots(1, 1, figsize=(9,6))
|
76 |
ax = fig.add_subplot(111)
|
77 |
-
|
78 |
-
ax.set_title(
|
79 |
-
ax.contourf(xx, yy,
|
80 |
-
a = ax.contour(xx, yy,
|
81 |
-
ax.contourf(xx, yy,
|
82 |
|
83 |
s = 20
|
84 |
b1 = ax.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k")
|
@@ -100,35 +95,106 @@ def make_regression(nu, gamma):
|
|
100 |
ax.set_xlabel(
|
101 |
"error train: %d/%d; errors novel regular: %d/%d; errors novel abnormal: %d/%d"
|
102 |
% (
|
103 |
-
|
104 |
X_train.shape[0],
|
105 |
-
|
106 |
X_test.shape[0],
|
107 |
-
|
108 |
X_outliers.shape[0],
|
109 |
)
|
110 |
)
|
111 |
-
|
112 |
return fig
|
113 |
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
s = 20
|
125 |
-
b1 =
|
126 |
-
b2 =
|
127 |
-
c =
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
[a.collections[0], b1, b2, c],
|
133 |
[
|
134 |
"learned frontier",
|
@@ -138,123 +204,52 @@ def make_regression(nu, gamma):
|
|
138 |
],
|
139 |
loc="upper left",
|
140 |
)
|
141 |
-
|
142 |
"error train: %d/%d; errors novel regular: %d/%d; errors novel abnormal: %d/%d"
|
143 |
% (
|
144 |
-
|
145 |
X_train.shape[0],
|
146 |
-
|
147 |
X_test.shape[0],
|
148 |
-
|
149 |
X_outliers.shape[0],
|
150 |
)
|
151 |
)
|
152 |
-
|
153 |
-
return fig
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
return make_fig_2(), make_fig_2()
|
159 |
-
|
160 |
-
# def make_figure():
|
161 |
-
# fig = plt.figure(figsize=(9, 6))
|
162 |
-
|
163 |
-
# plt.title("One Class SVM")
|
164 |
-
# plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), 0, 7), cmap=plt.cm.PuBu)
|
165 |
-
# a = plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors="darkred")
|
166 |
-
# plt.contourf(xx, yy, Z, levels=[0, Z.max()], colors="palevioletred")
|
167 |
-
|
168 |
-
# s = 20
|
169 |
-
# b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k")
|
170 |
-
# b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c="blueviolet", s=s, edgecolors="k")
|
171 |
-
# c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c="gold", s=s, edgecolors="k")
|
172 |
-
# plt.axis("tight")
|
173 |
-
# plt.xlim((-4.5, 4.5))
|
174 |
-
# plt.ylim((-4.5, 4.5))
|
175 |
-
# plt.legend(
|
176 |
-
# [a.collections[0], b1, b2, c],
|
177 |
-
# [
|
178 |
-
# "learned frontier",
|
179 |
-
# "training observations",
|
180 |
-
# "new regular observations",
|
181 |
-
# "new abnormal observations",
|
182 |
-
# ],
|
183 |
-
# loc="upper left",
|
184 |
-
# )
|
185 |
-
# plt.xlabel(
|
186 |
-
# "error train: %d/%d; errors novel regular: %d/%d; errors novel abnormal: %d/%d"
|
187 |
-
# % (
|
188 |
-
# n_error_train,
|
189 |
-
# X_train.shape[0],
|
190 |
-
# n_error_test,
|
191 |
-
# X_test.shape[0],
|
192 |
-
# n_error_outliers,
|
193 |
-
# X_outliers.shape[0],
|
194 |
-
# )
|
195 |
-
# )
|
196 |
-
# plt.show()
|
197 |
-
|
198 |
-
|
199 |
-
def make_example(model_1_depth, model_2_depth):
|
200 |
-
return f"""
|
201 |
-
With the following code you can reproduce this example with the current values of the sliders and the same data in a notebook:
|
202 |
-
|
203 |
-
```python
|
204 |
-
import numpy as np
|
205 |
-
import plotly.graph_objects as go
|
206 |
-
from sklearn.tree import DecisionTreeRegressor
|
207 |
-
|
208 |
-
rng = np.random.default_rng(0)
|
209 |
-
|
210 |
-
X = np.sort(5 * rng.random((80, 1)), axis=0)
|
211 |
-
y = np.sin(X).ravel()
|
212 |
-
y[::5] += 3 * (0.5 - rng.random(16))
|
213 |
|
214 |
-
regr_1 = DecisionTreeRegressor(max_depth={model_1_depth}, random_state=0)
|
215 |
-
regr_2 = DecisionTreeRegressor(max_depth={model_2_depth}, random_state=0)
|
216 |
-
regr_1.fit(X, y)
|
217 |
-
regr_2.fit(X, y)
|
218 |
|
219 |
-
|
220 |
-
|
221 |
-
y_1 = regr_1.predict(X_test)
|
222 |
-
y_2 = regr_2.predict(X_test)
|
223 |
|
224 |
-
|
225 |
-
fig = go.Figure()
|
226 |
-
fig.add_trace(go.Scatter(x=X[:,0], y=y, mode='markers', name='data'))
|
227 |
-
fig.add_trace(go.Scatter(x=X_test[:,0], y=y_1, mode='lines', name=f"max_depth={model_1_depth}"))
|
228 |
-
fig.add_trace(go.Scatter(x=X_test[:,0], y=y_2, mode='lines', name=f"max_depth={model_2_depth}"))
|
229 |
-
|
230 |
-
fig.update_layout(title='Decision Tree Regression')
|
231 |
-
fig.update_xaxes(title_text='data')
|
232 |
-
fig.update_yaxes(title_text='target')
|
233 |
-
fig.show()
|
234 |
```
|
235 |
"""
|
236 |
|
|
|
237 |
with gr.Blocks() as demo:
|
238 |
with gr.Row():
|
239 |
-
gr.Markdown(
|
240 |
with gr.Row():
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
|
|
|
|
245 |
with gr.Row():
|
246 |
-
|
|
|
|
|
|
|
247 |
with gr.Row():
|
248 |
-
|
249 |
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
button.click(make_regression, inputs=[slider_nu, slider_gamma], outputs=[plot1, plot2])
|
259 |
|
260 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import matplotlib
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import numpy as np
|
5 |
from sklearn.kernel_approximation import Nystroem
|
6 |
+
from sklearn.linear_model import SGDOneClassSVM
|
7 |
from sklearn.pipeline import make_pipeline
|
8 |
+
from sklearn.svm import OneClassSVM
|
9 |
+
|
10 |
+
md_description = """
|
11 |
+
This example shows how to approximate the solution of [sklearn.svm.OneClassSVM](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM) in the case of an RBF kernel with [sklearn.linear_model.SGDOneClassSVM](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM), a Stochastic Gradient Descent (SGD) version of the One-Class SVM. A kernel approximation is first used in order to apply [sklearn.linear_model.SGDOneClassSVM](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM) which implements a linear One-Class SVM using SGD.
|
12 |
+
Note that [sklearn.linear_model.SGDOneClassSVM](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM) scales linearly with the number of samples whereas the complexity of a kernelized [sklearn.svm.OneClassSVM](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM) is at best quadratic with respect to the number of samples. It is not the purpose of this example to illustrate the benefits of such an approximation in terms of computation time but rather to show that we obtain similar results on a toy dataset.
|
13 |
+
"""
|
14 |
|
15 |
font = {"weight": "normal", "size": 15}
|
16 |
|
17 |
matplotlib.rc("font", **font)
|
18 |
|
19 |
random_state = 42
|
20 |
+
rng = np.random.RandomState(random_state)
|
21 |
+
# rng = np.random.default_rng(random_state)
|
22 |
|
23 |
# Generate train data
|
24 |
+
X = 0.3 * rng.randn(500, 2)
|
25 |
X_train = np.r_[X + 2, X - 2]
|
26 |
# Generate some regular novel observations
|
27 |
+
X = 0.3 * rng.randn(20, 2)
|
28 |
X_test = np.r_[X + 2, X - 2]
|
29 |
# Generate some abnormal novel observations
|
30 |
X_outliers = rng.uniform(low=-4, high=4, size=(20, 2))
|
|
|
35 |
# nu = 0.05
|
36 |
# gamma = 2.0
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
def make_regression(nu, gamma):
|
40 |
clf = OneClassSVM(gamma=gamma, kernel="rbf", nu=nu)
|
|
|
49 |
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
50 |
Z = Z.reshape(xx.shape)
|
51 |
|
|
|
52 |
# Fit the One-Class SVM using a kernel approximation and SGD
|
53 |
transform = Nystroem(gamma=gamma, random_state=random_state)
|
54 |
clf_sgd = SGDOneClassSVM(
|
|
|
65 |
|
66 |
Z_sgd = pipe_sgd.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
67 |
Z_sgd = Z_sgd.reshape(xx.shape)
|
68 |
+
|
69 |
+
def make_plot(title, curr_z):
|
|
|
70 |
fig = plt.figure(figsize=(9, 6))
|
|
|
71 |
ax = fig.add_subplot(111)
|
72 |
+
|
73 |
+
ax.set_title(title)
|
74 |
+
ax.contourf(xx, yy, curr_z, levels=np.linspace(curr_z.min(), 0, 7), cmap=plt.cm.PuBu)
|
75 |
+
a = ax.contour(xx, yy, curr_z, levels=[0], linewidths=2, colors="darkred")
|
76 |
+
ax.contourf(xx, yy, curr_z, levels=[0, curr_z.max()], colors="palevioletred")
|
77 |
|
78 |
s = 20
|
79 |
b1 = ax.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k")
|
|
|
95 |
ax.set_xlabel(
|
96 |
"error train: %d/%d; errors novel regular: %d/%d; errors novel abnormal: %d/%d"
|
97 |
% (
|
98 |
+
n_error_train_sgd,
|
99 |
X_train.shape[0],
|
100 |
+
n_error_test_sgd,
|
101 |
X_test.shape[0],
|
102 |
+
n_error_outliers_sgd,
|
103 |
X_outliers.shape[0],
|
104 |
)
|
105 |
)
|
106 |
+
|
107 |
return fig
|
108 |
|
109 |
+
return (
|
110 |
+
make_plot("One Class SVM", Z),
|
111 |
+
make_plot("Online One-Class SVM", Z_sgd),
|
112 |
+
make_example(nu, gamma),
|
113 |
+
)
|
114 |
+
|
115 |
+
|
116 |
+
def make_example(nu, gamma):
|
117 |
+
return f"""
|
118 |
+
With the following code you can reproduce this example with the current values of the sliders and the same data in a notebook:
|
119 |
+
|
120 |
+
```python
|
121 |
+
import numpy as np
|
122 |
+
import matplotlib.pyplot as plt
|
123 |
+
import matplotlib
|
124 |
+
from sklearn.svm import OneClassSVM
|
125 |
+
from sklearn.linear_model import SGDOneClassSVM
|
126 |
+
from sklearn.kernel_approximation import Nystroem
|
127 |
+
from sklearn.pipeline import make_pipeline
|
128 |
+
|
129 |
+
font = {{"weight": "normal", "size": 15}}
|
130 |
+
|
131 |
+
matplotlib.rc("font", **font)
|
132 |
+
|
133 |
+
rng = np.random.RandomState(random_state)
|
134 |
+
|
135 |
+
# Generate train data
|
136 |
+
X = 0.3 * rng.randn(500, 2)
|
137 |
+
X_train = np.r_[X + 2, X - 2]
|
138 |
+
# Generate some regular novel observations
|
139 |
+
X = 0.3 * rng.randn(20, 2)
|
140 |
+
X_test = np.r_[X + 2, X - 2]
|
141 |
+
# Generate some abnormal novel observations
|
142 |
+
X_outliers = rng.uniform(low=-4, high=4, size=(20, 2))
|
143 |
+
|
144 |
+
xx, yy = np.meshgrid(np.linspace(-4.5, 4.5, 50), np.linspace(-4.5, 4.5, 50))
|
145 |
+
|
146 |
+
# OCSVM hyperparameters
|
147 |
+
nu = {nu}
|
148 |
+
gamma = {gamma}
|
149 |
+
|
150 |
+
# Fit the One-Class SVM
|
151 |
+
clf = OneClassSVM(gamma=gamma, kernel="rbf", nu=nu)
|
152 |
+
clf.fit(X_train)
|
153 |
+
y_pred_train = clf.predict(X_train)
|
154 |
+
y_pred_test = clf.predict(X_test)
|
155 |
+
y_pred_outliers = clf.predict(X_outliers)
|
156 |
+
n_error_train = y_pred_train[y_pred_train == -1].size
|
157 |
+
n_error_test = y_pred_test[y_pred_test == -1].size
|
158 |
+
n_error_outliers = y_pred_outliers[y_pred_outliers == 1].size
|
159 |
+
|
160 |
+
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
161 |
+
Z = Z.reshape(xx.shape)
|
162 |
+
|
163 |
+
|
164 |
+
# Fit the One-Class SVM using a kernel approximation and SGD
|
165 |
+
transform = Nystroem(gamma=gamma, random_state=random_state)
|
166 |
+
clf_sgd = SGDOneClassSVM(
|
167 |
+
nu=nu, shuffle=True, fit_intercept=True, random_state=random_state, tol=1e-4
|
168 |
+
)
|
169 |
+
pipe_sgd = make_pipeline(transform, clf_sgd)
|
170 |
+
pipe_sgd.fit(X_train)
|
171 |
+
y_pred_train_sgd = pipe_sgd.predict(X_train)
|
172 |
+
y_pred_test_sgd = pipe_sgd.predict(X_test)
|
173 |
+
y_pred_outliers_sgd = pipe_sgd.predict(X_outliers)
|
174 |
+
n_error_train_sgd = y_pred_train_sgd[y_pred_train_sgd == -1].size
|
175 |
+
n_error_test_sgd = y_pred_test_sgd[y_pred_test_sgd == -1].size
|
176 |
+
n_error_outliers_sgd = y_pred_outliers_sgd[y_pred_outliers_sgd == 1].size
|
177 |
+
|
178 |
+
Z_sgd = pipe_sgd.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
179 |
+
Z_sgd = Z_sgd.reshape(xx.shape)
|
180 |
+
|
181 |
+
|
182 |
+
# plot the level sets of the decision function
|
183 |
+
def make_plot(Z_curr, title):
|
184 |
+
plt.figure(figsize=(9, 6))
|
185 |
+
plt.title(title)
|
186 |
+
plt.contourf(xx, yy, Z_curr, levels=np.linspace(Z_curr.min(), 0, 7), cmap=plt.cm.PuBu)
|
187 |
+
a = plt.contour(xx, yy, Z_curr, levels=[0], linewidths=2, colors="darkred")
|
188 |
+
plt.contourf(xx, yy, Z_curr, levels=[0, Z_curr.max()], colors="palevioletred")
|
189 |
|
190 |
s = 20
|
191 |
+
b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k")
|
192 |
+
b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c="blueviolet", s=s, edgecolors="k")
|
193 |
+
c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c="gold", s=s, edgecolors="k")
|
194 |
+
plt.axis("tight")
|
195 |
+
plt.xlim((-4.5, 4.5))
|
196 |
+
plt.ylim((-4.5, 4.5))
|
197 |
+
plt.legend(
|
198 |
[a.collections[0], b1, b2, c],
|
199 |
[
|
200 |
"learned frontier",
|
|
|
204 |
],
|
205 |
loc="upper left",
|
206 |
)
|
207 |
+
plt.xlabel(
|
208 |
"error train: %d/%d; errors novel regular: %d/%d; errors novel abnormal: %d/%d"
|
209 |
% (
|
210 |
+
n_error_train,
|
211 |
X_train.shape[0],
|
212 |
+
n_error_test,
|
213 |
X_test.shape[0],
|
214 |
+
n_error_outliers,
|
215 |
X_outliers.shape[0],
|
216 |
)
|
217 |
)
|
218 |
+
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
|
|
|
|
|
|
|
|
|
220 |
|
221 |
+
make_plot(Z, "One-Class SVM")
|
222 |
+
make_plot(Z_sgd, "Online One-Class SVM")
|
|
|
|
|
223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
```
|
225 |
"""
|
226 |
|
227 |
+
|
228 |
with gr.Blocks() as demo:
|
229 |
with gr.Row():
|
230 |
+
gr.Markdown("# One-Class SVM versus One-Class SVM using Stochastic Gradient Descent")
|
231 |
with gr.Row():
|
232 |
+
with gr.Column():
|
233 |
+
gr.Markdown(md_description)
|
234 |
+
with gr.Column():
|
235 |
+
slider_nu = gr.Slider(minimum=0.01, maximum=1, label="Nu", step=0.025, value=0.05)
|
236 |
+
slider_gamma = gr.Slider(minimum=0.1, maximum=3, label="Gamma", step=0.1, value=2.0)
|
237 |
+
button = gr.Button("Generate")
|
238 |
with gr.Row():
|
239 |
+
with gr.Column():
|
240 |
+
plot1 = gr.Plot(label="Output")
|
241 |
+
with gr.Column():
|
242 |
+
plot2 = gr.Plot(label="Output")
|
243 |
with gr.Row():
|
244 |
+
example = gr.Markdown("")
|
245 |
|
246 |
+
slider_nu.change(
|
247 |
+
fn=make_regression, inputs=[slider_nu, slider_gamma], outputs=[plot1, plot2, example]
|
248 |
+
)
|
249 |
+
slider_gamma.change(
|
250 |
+
fn=make_regression, inputs=[slider_nu, slider_gamma], outputs=[plot1, plot2, example]
|
251 |
+
)
|
252 |
+
button.click(make_regression, inputs=[slider_nu, slider_gamma], outputs=[plot1, plot2, example])
|
253 |
+
demo.load(make_regression, inputs=[slider_nu, slider_gamma], outputs=[plot1, plot2, example])
|
|
|
254 |
|
255 |
demo.launch()
|
pyproject.toml
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
[tool.poetry]
|
2 |
-
name = "sklearn-
|
3 |
version = "0.1.0"
|
4 |
description = "Hugging Face Scikit Learn Demos"
|
5 |
authors = ["Niels van Galen Last <[email protected]>"]
|
|
|
1 |
[tool.poetry]
|
2 |
+
name = "sklearn-ocsvm-vs-sgdocsvm"
|
3 |
version = "0.1.0"
|
4 |
description = "Hugging Face Scikit Learn Demos"
|
5 |
authors = ["Niels van Galen Last <[email protected]>"]
|