Commit
·
9c6a64c
1
Parent(s):
64edc16
Everything for space
Browse files- app.py +101 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
+
from sklearn.svm import SVC
|
6 |
+
import plotly.graph_objects as go
|
7 |
+
from sklearn.datasets import load_digits
|
8 |
+
from sklearn.model_selection import validation_curve
|
9 |
+
|
10 |
+
def plot_validation_curve(x: np.array, ys: list[np.array], yerros: list[np.array], names: list[str], colors: list[str], log_x: bool=True, title: str=""):
|
11 |
+
fig = go.Figure()
|
12 |
+
|
13 |
+
for y, yerror, name, color in zip(ys, yerros, names, colors):
|
14 |
+
y_upper = y + yerror
|
15 |
+
y_lower = y - yerror
|
16 |
+
|
17 |
+
fig.add_trace(
|
18 |
+
go.Scatter(
|
19 |
+
x=x,
|
20 |
+
y=y,
|
21 |
+
name=name,
|
22 |
+
line_color=color
|
23 |
+
)
|
24 |
+
)
|
25 |
+
|
26 |
+
fig.add_trace(
|
27 |
+
go.Scatter(
|
28 |
+
x=x.tolist()+x[::-1].tolist(), # x, then x reversed
|
29 |
+
y=y_upper.tolist()+y_lower[::-1].tolist(), # upper, then lower reversed
|
30 |
+
fill='toself',
|
31 |
+
fillcolor=color,
|
32 |
+
line=dict(color=color),
|
33 |
+
hoverinfo="skip",
|
34 |
+
showlegend=False,
|
35 |
+
opacity=0.2
|
36 |
+
)
|
37 |
+
)
|
38 |
+
|
39 |
+
if log_x:
|
40 |
+
fig.update_xaxes(type="log")
|
41 |
+
|
42 |
+
fig.update_layout(title=title, xaxis_title="gamma", yaxis_title="Accuracy")
|
43 |
+
|
44 |
+
return fig
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
def app_fn(n_points: int):
|
49 |
+
X, y = load_digits(return_X_y=True)
|
50 |
+
subset_mask = np.isin(y, [1, 2]) # binary classification: 1 vs 2
|
51 |
+
X, y = X[subset_mask], y[subset_mask]
|
52 |
+
|
53 |
+
param_range = np.logspace(-6, -1, n_points)
|
54 |
+
train_scores, test_scores = validation_curve(
|
55 |
+
SVC(),
|
56 |
+
X,
|
57 |
+
y,
|
58 |
+
param_name="gamma",
|
59 |
+
param_range=param_range,
|
60 |
+
scoring="accuracy",
|
61 |
+
n_jobs=-1,
|
62 |
+
)
|
63 |
+
|
64 |
+
train_scores_mean = np.mean(train_scores, axis=1)
|
65 |
+
train_scores_std = np.std(train_scores, axis=1)
|
66 |
+
test_scores_mean = np.mean(test_scores, axis=1)
|
67 |
+
test_scores_std = np.std(test_scores, axis=1)
|
68 |
+
|
69 |
+
fig = plot_validation_curve(
|
70 |
+
param_range,
|
71 |
+
[train_scores_mean, test_scores_mean],
|
72 |
+
[train_scores_std, test_scores_std],
|
73 |
+
["Training score", "Cross-validation score"],
|
74 |
+
["orange", "navy"],
|
75 |
+
title="Validation Curve with SVM for Gamma Hyperparameter"
|
76 |
+
)
|
77 |
+
|
78 |
+
return fig
|
79 |
+
|
80 |
+
title = "Plotting Validation Curve"
|
81 |
+
with gr.Blocks(title=title) as demo:
|
82 |
+
gr.Markdown(f"# {title}")
|
83 |
+
gr.Markdown(
|
84 |
+
"""
|
85 |
+
#### This example shows the usage of a validation curve to understand \
|
86 |
+
how the performance of a model, SVM in this case, changes with varying hyperparameters. \
|
87 |
+
The dataset used was the [digits dataset](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html#sklearn.datasets.load_digits) \
|
88 |
+
from scikit-learn. The hyperparameter varied was gamma. \
|
89 |
+
|
90 |
+
[Original Example](https://scikit-learn.org/stable/auto_examples/model_selection/plot_validation_curve.html#sphx-glr-auto-examples-model-selection-plot-validation-curve-py)
|
91 |
+
"""
|
92 |
+
)
|
93 |
+
|
94 |
+
n_points = gr.inputs.Slider(5, 100, 5, 5,label="Number of points")
|
95 |
+
btn = gr.Button("Run")
|
96 |
+
fig = gr.Plot(label="Validation Curve")
|
97 |
+
|
98 |
+
btn.click(fn=app_fn, inputs=[n_points], outputs=[fig])
|
99 |
+
demo.load(fn=app_fn, inputs=[n_points], outputs=[fig])
|
100 |
+
|
101 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
scikit-learn==1.2.2
|
2 |
+
plotly==5.14.1
|