huabdul commited on
Commit
52b7de4
·
1 Parent(s): f02d488

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -28
app.py CHANGED
@@ -18,7 +18,6 @@ import gradio as gr
18
  C1, C2, C3 = '#ff0000', '#ffff00', '#0000ff'
19
  CMAP = ListedColormap([C1, C2, C3])
20
  GRANULARITY = 0.05
21
- SEED = 1
22
 
23
  FEATURE_NAMES = ["Sepal Length", "Sepal Width", "Petal Length", "Petal Width"]
24
  TARGET_NAMES = ["Setosa", "Versicolour", "Virginica"]
@@ -39,9 +38,7 @@ def get_decision_surface(X, y, model):
39
  return xx, yy, Z
40
 
41
  def create_plot(feature_string, max_depth, n_neighbors, gamma, weight1, weight2, weight3):
42
-
43
- np.random.seed(SEED)
44
-
45
  feature_list = feature_string.split(',')
46
  feature_list = [s.strip() for s in feature_list]
47
  idx_x = FEATURE_NAMES.index(feature_list[0])
@@ -68,12 +65,13 @@ def create_plot(feature_string, max_depth, n_neighbors, gamma, weight1, weight2,
68
  clf3.fit(X, y)
69
  eclf.fit(X, y)
70
 
71
- fig = plt.figure(figsize=(12, 12))
72
 
73
  for i, clf in enumerate([clf1, clf2, clf3, eclf]):
74
  xx, yy, Z = get_decision_surface(X, y, clf)
75
 
76
  ax = fig.add_subplot(2, 2, i+1)
 
77
  ax.contourf(xx, yy, Z, cmap=CMAP, alpha=0.65)
78
 
79
  for j, label in enumerate(TARGET_NAMES):
@@ -81,47 +79,46 @@ def create_plot(feature_string, max_depth, n_neighbors, gamma, weight1, weight2,
81
  y_label = y[y==j]
82
  ax.scatter(X_label[:, 0], X_label[:, 1], c=[[C1], [C2], [C3]][j]*len(y_label), edgecolor='k', s=40, label=label)
83
 
84
- ax.set_xlabel(feature_list[0]); ax.set_ylabel(feature_list[1])
85
  ax.legend()
86
  ax.set_title(f'{MODEL_NAMES[i]}')
87
-
 
 
 
88
  return fig
89
 
90
  info = '''
91
  # Voting Classifier Decision Surface
92
 
93
- This app plots the decision surface of four classifiers on two selected features of the Iris dataset:
94
- - DecisionTreeClassifier.
95
- - KNeighborsClassifier.
96
- - SupportVectorClassifier.
97
- - A VotingClassifier from all of the above.
98
 
99
  Use the controls below to tune the parameters of the classifiers and the weights of each of them in the soft voting classifier and click submit. The more weight you assign to a classifier, the more importance will be assigned to its predictions compared to the other classifiers in the vote.
 
 
100
  '''
101
 
102
  with gr.Blocks(analytics_enabled=False) as demo:
103
- gr.Markdown(info)
104
-
105
  selections = combinations(FEATURE_NAMES, 2)
106
  selections = [f'{s[0]}, {s[1]}' for s in selections]
107
 
108
- dd = gr.Dropdown(selections, value=selections[0], interactive=True, label="Input features")
109
-
110
  with gr.Row():
111
  with gr.Column():
112
- slider_max_depth = gr.Slider(1, 50, value=4, step=1, label='max_depth (for DecisionTreeClassifier)')
113
- slider_n_neighbors = gr.Slider(1, 20, value=7, step=1, label='n_neighbors (for KNeighborsClassifier)')
114
- slider_gamma = gr.Slider(0, 10, value=0.1, step=0.1, label='gamma (for SVC)')
115
-
116
- with gr.Column():
117
- slider_w1 = gr.Slider(0, 10, value=2, step=0.1, label='DecisionTreeClassifier weight')
118
- slider_w2 = gr.Slider(0, 10, value=1, step=0.1, label='KNeighborsClassifier weight')
119
- slider_w3 = gr.Slider(0, 10, value=2, step=0.1, label='SVC weight')
120
-
 
 
121
 
122
- btn = gr.Button(value='Submit')
123
-
124
- plot = gr.Plot(label='Decision Surfaces')
 
125
 
126
  btn.click(create_plot, inputs=[dd, slider_max_depth, slider_n_neighbors, slider_gamma, slider_w1, slider_w2, slider_w3], outputs=[plot])
127
 
 
18
  C1, C2, C3 = '#ff0000', '#ffff00', '#0000ff'
19
  CMAP = ListedColormap([C1, C2, C3])
20
  GRANULARITY = 0.05
 
21
 
22
  FEATURE_NAMES = ["Sepal Length", "Sepal Width", "Petal Length", "Petal Width"]
23
  TARGET_NAMES = ["Setosa", "Versicolour", "Virginica"]
 
38
  return xx, yy, Z
39
 
40
  def create_plot(feature_string, max_depth, n_neighbors, gamma, weight1, weight2, weight3):
41
+
 
 
42
  feature_list = feature_string.split(',')
43
  feature_list = [s.strip() for s in feature_list]
44
  idx_x = FEATURE_NAMES.index(feature_list[0])
 
65
  clf3.fit(X, y)
66
  eclf.fit(X, y)
67
 
68
+ fig, _ = plt.subplots(2, 2, figsize=(7, 7), sharex=True, sharey=True)
69
 
70
  for i, clf in enumerate([clf1, clf2, clf3, eclf]):
71
  xx, yy, Z = get_decision_surface(X, y, clf)
72
 
73
  ax = fig.add_subplot(2, 2, i+1)
74
+ ax.set_axis_off()
75
  ax.contourf(xx, yy, Z, cmap=CMAP, alpha=0.65)
76
 
77
  for j, label in enumerate(TARGET_NAMES):
 
79
  y_label = y[y==j]
80
  ax.scatter(X_label[:, 0], X_label[:, 1], c=[[C1], [C2], [C3]][j]*len(y_label), edgecolor='k', s=40, label=label)
81
 
 
82
  ax.legend()
83
  ax.set_title(f'{MODEL_NAMES[i]}')
84
+
85
+ fig.supxlabel(feature_list[0]); fig.supylabel(feature_list[1])
86
+ fig.set_tight_layout(True)
87
+ fig.set_constrained_layout(True)
88
  return fig
89
 
90
  info = '''
91
  # Voting Classifier Decision Surface
92
 
93
+ This app plots the decision surface of four classifiers on two selected features of the Iris dataset: DecisionTreeClassifier, KNeighborsClassifier, SupportVectorClassifier, and a VotingClassifier from all of them.
 
 
 
 
94
 
95
  Use the controls below to tune the parameters of the classifiers and the weights of each of them in the soft voting classifier and click submit. The more weight you assign to a classifier, the more importance will be assigned to its predictions compared to the other classifiers in the vote.
96
+
97
+ Created by [@huabdul]() based on [scikit-learn docs]().
98
  '''
99
 
100
  with gr.Blocks(analytics_enabled=False) as demo:
 
 
101
  selections = combinations(FEATURE_NAMES, 2)
102
  selections = [f'{s[0]}, {s[1]}' for s in selections]
103
 
 
 
104
  with gr.Row():
105
  with gr.Column():
106
+ gr.Markdown(info)
107
+ dd = gr.Dropdown(selections, value=selections[0], interactive=True, label="Input features")
108
+ with gr.Row():
109
+ with gr.Column():
110
+ slider_max_depth = gr.Slider(1, 50, value=4, step=1, label='max_depth (for DecisionTreeClassifier)')
111
+ slider_n_neighbors = gr.Slider(1, 20, value=7, step=1, label='n_neighbors (for KNeighborsClassifier)')
112
+ slider_gamma = gr.Slider(0, 10, value=0.1, step=0.1, label='gamma (for SVC)')
113
+ with gr.Column():
114
+ slider_w1 = gr.Slider(0, 10, value=2, step=0.1, label='DecisionTreeClassifier weight')
115
+ slider_w2 = gr.Slider(0, 10, value=1, step=0.1, label='KNeighborsClassifier weight')
116
+ slider_w3 = gr.Slider(0, 10, value=2, step=0.1, label='SVC weight')
117
 
118
+ btn = gr.Button(value='Submit')
119
+
120
+ with gr.Column():
121
+ plot = gr.Plot(show_label=False)
122
 
123
  btn.click(create_plot, inputs=[dd, slider_max_depth, slider_n_neighbors, slider_gamma, slider_w1, slider_w2, slider_w3], outputs=[plot])
124