Spaces:
Sleeping
Sleeping
EduardoPach
commited on
Commit
·
6f11e8c
1
Parent(s):
7d35b20
Everything for space
Browse files- app.py +134 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 8 |
+
def plot_decision(
|
| 9 |
+
clf: SVC,
|
| 10 |
+
X: np.ndarray,
|
| 11 |
+
x_range: np.array,
|
| 12 |
+
y_range: np.array,
|
| 13 |
+
weights: np.array,
|
| 14 |
+
colors: list[str],
|
| 15 |
+
title: str
|
| 16 |
+
):
|
| 17 |
+
# plot the decision function
|
| 18 |
+
xx, yy = np.meshgrid(x_range, y_range)
|
| 19 |
+
|
| 20 |
+
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
| 21 |
+
Z = Z.reshape(xx.shape)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
fig = go.Figure()
|
| 25 |
+
|
| 26 |
+
fig.add_trace(
|
| 27 |
+
go.Contour(
|
| 28 |
+
x=x_range,
|
| 29 |
+
y=y_range,
|
| 30 |
+
z=Z,
|
| 31 |
+
colorscale="gray",
|
| 32 |
+
opacity=0.75,
|
| 33 |
+
showscale=False,
|
| 34 |
+
)
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
fig.add_trace(
|
| 38 |
+
go.Scatter(
|
| 39 |
+
x=X[:, 0],
|
| 40 |
+
y=X[:, 1],
|
| 41 |
+
mode="markers",
|
| 42 |
+
marker=dict(
|
| 43 |
+
color=colors,
|
| 44 |
+
size=(weights + 5) * 2
|
| 45 |
+
),
|
| 46 |
+
)
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Remove x and y ticks
|
| 50 |
+
fig.update_xaxes(showticklabels=False)
|
| 51 |
+
fig.update_yaxes(showticklabels=False)
|
| 52 |
+
# Add title
|
| 53 |
+
fig.update_layout(title=title)
|
| 54 |
+
|
| 55 |
+
return fig
|
| 56 |
+
|
| 57 |
+
def app_fn(seed: int, weight_1: int, weight_2: int):
|
| 58 |
+
# we create 20 points
|
| 59 |
+
np.random.seed(seed)
|
| 60 |
+
X = np.r_[np.random.randn(10, 2) + [1, 1], np.random.randn(10, 2)]
|
| 61 |
+
y = [1] * 10 + [-1] * 10
|
| 62 |
+
|
| 63 |
+
sample_weight_last_ten = abs(np.random.randn(len(X)))
|
| 64 |
+
sample_weight_constant = np.ones(len(X))
|
| 65 |
+
|
| 66 |
+
sample_weight_last_ten[15:] *= weight_1
|
| 67 |
+
sample_weight_last_ten[9] *= weight_2
|
| 68 |
+
|
| 69 |
+
# This model does not take into account sample weights.
|
| 70 |
+
clf_no_weights = SVC(gamma=1)
|
| 71 |
+
clf_no_weights.fit(X, y)
|
| 72 |
+
|
| 73 |
+
# This other model takes into account some dedicated sample weights.
|
| 74 |
+
clf_weights = SVC(gamma=1)
|
| 75 |
+
clf_weights.fit(X, y, sample_weight=sample_weight_last_ten)
|
| 76 |
+
|
| 77 |
+
# Plotting
|
| 78 |
+
x_range = np.arange(-4, 5, 0.1)
|
| 79 |
+
colors = np.where(np.array(y)==1, "white", "black")
|
| 80 |
+
|
| 81 |
+
fig_no_weights = plot_decision(
|
| 82 |
+
clf_no_weights,
|
| 83 |
+
X,
|
| 84 |
+
x_range,
|
| 85 |
+
x_range,
|
| 86 |
+
sample_weight_constant,
|
| 87 |
+
colors,
|
| 88 |
+
"SVM without Weights"
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
fig_weights = plot_decision(
|
| 92 |
+
clf_weights,
|
| 93 |
+
X,
|
| 94 |
+
x_range,
|
| 95 |
+
x_range,
|
| 96 |
+
sample_weight_last_ten,
|
| 97 |
+
colors,
|
| 98 |
+
"SVM with Weights"
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
return fig_no_weights, fig_weights
|
| 102 |
+
|
| 103 |
+
title = "SVM with Weighted Samples"
|
| 104 |
+
|
| 105 |
+
with gr.Blocks(title=title) as demo:
|
| 106 |
+
gr.Markdown(f"# {title}")
|
| 107 |
+
gr.Markdown(
|
| 108 |
+
"""
|
| 109 |
+
### This is a demo of how SVMs can be trained with weighted samples \
|
| 110 |
+
and the impact on the decision boundary. To represent that a synthetic \
|
| 111 |
+
dataset is generated with 20 points, 10 of which are assigned to the \
|
| 112 |
+
positive class and 10 to the negative class. A weight is assigned to \
|
| 113 |
+
each sample, which is the importance of that sample in the dataset. \
|
| 114 |
+
A model with and without weights is trained and the decision boundary \
|
| 115 |
+
is plotted. The size of the points is proportional to the weight of \
|
| 116 |
+
the sample.
|
| 117 |
+
|
| 118 |
+
Created by [@eduardopacheco](https://huggingface.co/EduardoPacheco) based on [scikit-learn-docs](https://scikit-learn.org/stable/auto_examples/svm/plot_weighted_samples.html#sphx-glr-auto-examples-svm-plot-weighted-samples-py)
|
| 119 |
+
"""
|
| 120 |
+
)
|
| 121 |
+
with gr.Row():
|
| 122 |
+
seed = gr.inputs.Slider(0, 100, 1, default=0, label="Seed")
|
| 123 |
+
weight_1 = gr.inputs.Slider(0, 20, 1, default=5, label="Weight for last 5 Samples")
|
| 124 |
+
weight_2 = gr.inputs.Slider(0, 20, 1, default=15, label="Weight for Sample 10")
|
| 125 |
+
btn = gr.Button("Run")
|
| 126 |
+
with gr.Row():
|
| 127 |
+
fig_no_weights = gr.Plot(label="SVM without Weights")
|
| 128 |
+
fig_weights = gr.Plot(label="SVM with Weights")
|
| 129 |
+
|
| 130 |
+
btn.click(fn=app_fn, outputs=[fig_no_weights, fig_weights], inputs=[seed, weight_1, weight_2])
|
| 131 |
+
demo.load(fn=app_fn, outputs=[fig_no_weights, fig_weights], inputs=[seed, weight_1, weight_2])
|
| 132 |
+
|
| 133 |
+
demo.launch()
|
| 134 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scikit-learn==1.2.2
|
| 2 |
+
plotly==5.14.1
|