Spaces:
Sleeping
Sleeping
Commit
·
262bb23
1
Parent(s):
3be65ae
App itself
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import gradio as gr
|
3 |
+
from sklearn.svm import LinearSVC
|
4 |
+
from sklearn.datasets import load_iris
|
5 |
+
from sklearn.pipeline import make_pipeline
|
6 |
+
from sklearn.multiclass import OneVsRestClassifier
|
7 |
+
from sklearn.model_selection import train_test_split
|
8 |
+
from sklearn.preprocessing import label_binarize, StandardScaler
|
9 |
+
|
10 |
+
import utils
|
11 |
+
|
12 |
+
|
13 |
+
def app_fn(n_random_features: int, test_size: float, random_state_val: int):
|
14 |
+
X, y = load_iris(return_X_y=True)
|
15 |
+
|
16 |
+
# Add noisy features
|
17 |
+
random_state = np.random.RandomState(random_state_val)
|
18 |
+
n_samples, n_features = X.shape
|
19 |
+
X = np.concatenate([X, random_state.randn(n_samples, n_random_features)], axis=1)
|
20 |
+
|
21 |
+
# Solving Binary Problem
|
22 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
23 |
+
X[y < 2], y[y < 2], test_size=test_size, random_state=random_state
|
24 |
+
)
|
25 |
+
|
26 |
+
clf_bin = make_pipeline(StandardScaler(), LinearSVC(random_state=random_state))
|
27 |
+
clf_bin.fit(X_train, y_train)
|
28 |
+
|
29 |
+
fig_bin = utils.plot_binary_pr_curve(clf_bin, X_test, y_test)
|
30 |
+
|
31 |
+
# Solving Multi-Label Problem
|
32 |
+
Y = label_binarize(y, classes=[0, 1, 2])
|
33 |
+
X_train_multi, X_test_multi, Y_train, Y_test = train_test_split(
|
34 |
+
X, Y, test_size=test_size, random_state=random_state
|
35 |
+
)
|
36 |
+
|
37 |
+
clf = OneVsRestClassifier(
|
38 |
+
make_pipeline(StandardScaler(), LinearSVC(random_state=random_state))
|
39 |
+
)
|
40 |
+
clf.fit(X_train_multi, Y_train)
|
41 |
+
|
42 |
+
fig_multi = utils.plot_multi_label_pr_curve(clf, X_test_multi, Y_test)
|
43 |
+
|
44 |
+
return fig_bin, fig_multi
|
45 |
+
|
46 |
+
def app_fn_multi():
|
47 |
+
...
|
48 |
+
|
49 |
+
|
50 |
+
title = "Precision-Recall Curves"
|
51 |
+
with gr.Blocks(title=title) as demo:
|
52 |
+
gr.Markdown(f"# {title}")
|
53 |
+
gr.Markdown(
|
54 |
+
"""
|
55 |
+
### This demo shows the precision-recall curves on the Iris dataset \
|
56 |
+
using a Linear SVM classifier + StandardScaler. \
|
57 |
+
Noise is added to the dataset to make the problem more challenging. \
|
58 |
+
The dataset is split into train and test sets. \
|
59 |
+
The model is trained on the train set and evaluated on the test set. \
|
60 |
+
Two separate problems are solved:
|
61 |
+
|
62 |
+
### Binary classification: class 0 vs class 1
|
63 |
+
### Multi-label classification: class 0 vs class 1 vs class 2
|
64 |
+
|
65 |
+
[Original Example](https://scikit-learn.org/stable/auto_examples/model_selection/plot_precision_recall.html#sphx-glr-auto-examples-model-selection-plot-precision-recall-py)
|
66 |
+
"""
|
67 |
+
)
|
68 |
+
|
69 |
+
with gr.Row():
|
70 |
+
n_random_features = gr.inputs.Slider(0, 1000, 50, 800,label="Number of Random Features")
|
71 |
+
test_size = gr.inputs.Slider(0.1, 0.9, 0.3, 0.5, label="Test Size")
|
72 |
+
random_state_val = gr.inputs.Slider(0, 100, 5, 0,label="Random State")
|
73 |
+
|
74 |
+
btn = gr.Button("Run")
|
75 |
+
|
76 |
+
with gr.Column():
|
77 |
+
fig_bin = gr.Plot(label="Binary PR Curve")
|
78 |
+
fig_multi = gr.Plot(label="Multi-Label PR Curve")
|
79 |
+
|
80 |
+
btn.click(fn=app_fn, inputs=[n_random_features, test_size, random_state_val], outputs=[fig_bin, fig_multi])
|
81 |
+
demo.load(fn=app_fn, inputs=[n_random_features, test_size, random_state_val], outputs=[fig_bin, fig_multi])
|
82 |
+
|
83 |
+
demo.launch()
|
84 |
+
|
85 |
+
|