EduardoPacheco commited on
Commit
f4448ff
1 Parent(s): 6f11e8c

Proposed changes

Browse files
Files changed (1) hide show
  1. app.py +11 -9
app.py CHANGED
@@ -8,10 +8,10 @@ import plotly.graph_objects as go
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
@@ -28,7 +28,7 @@ def plot_decision(
28
  x=x_range,
29
  y=y_range,
30
  z=Z,
31
- colorscale="gray",
32
  opacity=0.75,
33
  showscale=False,
34
  )
@@ -40,7 +40,8 @@ def plot_decision(
40
  y=X[:, 1],
41
  mode="markers",
42
  marker=dict(
43
- color=colors,
 
44
  size=(weights + 5) * 2
45
  ),
46
  )
@@ -76,25 +77,24 @@ def app_fn(seed: int, weight_1: int, weight_2: int):
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
 
@@ -122,12 +122,14 @@ with gr.Blocks(title=title) as demo:
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()
 
8
  def plot_decision(
9
  clf: SVC,
10
  X: np.ndarray,
11
+ y: np.array,
12
  x_range: np.array,
13
  y_range: np.array,
14
  weights: np.array,
 
15
  title: str
16
  ):
17
  # plot the decision function
 
28
  x=x_range,
29
  y=y_range,
30
  z=Z,
31
+ colorscale="Viridis",
32
  opacity=0.75,
33
  showscale=False,
34
  )
 
40
  y=X[:, 1],
41
  mode="markers",
42
  marker=dict(
43
+ color=y,
44
+ colorscale="viridis",
45
  size=(weights + 5) * 2
46
  ),
47
  )
 
77
 
78
  # Plotting
79
  x_range = np.arange(-4, 5, 0.1)
 
80
 
81
  fig_no_weights = plot_decision(
82
  clf_no_weights,
83
  X,
84
+ y,
85
  x_range,
86
  x_range,
87
+ sample_weight_constant,
 
88
  "SVM without Weights"
89
  )
90
 
91
  fig_weights = plot_decision(
92
  clf_weights,
93
  X,
94
+ y,
95
  x_range,
96
  x_range,
97
  sample_weight_last_ten,
 
98
  "SVM with Weights"
99
  )
100
 
 
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
+ seed.change(fn=app_fn, outputs=[fig_no_weights, fig_weights], inputs=[seed, weight_1, weight_2])
131
+ weight_1.change(fn=app_fn, outputs=[fig_no_weights, fig_weights], inputs=[seed, weight_1, weight_2])
132
+ weight_2.change(fn=app_fn, outputs=[fig_no_weights, fig_weights], inputs=[seed, weight_1, weight_2])
133
  demo.load(fn=app_fn, outputs=[fig_no_weights, fig_weights], inputs=[seed, weight_1, weight_2])
134
 
135
  demo.launch()