sulpha commited on
Commit
95d8a7d
·
1 Parent(s): ee04321

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -69
app.py CHANGED
@@ -1,3 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
  import matplotlib.pyplot as plt
@@ -5,37 +16,30 @@ from sklearn import datasets
5
  from sklearn.linear_model import SGDClassifier
6
  from sklearn.inspection import DecisionBoundaryDisplay
7
 
8
- def predict_class(x, y):
9
- iris = datasets.load_iris()
10
- X = iris.data[:, :2]
11
- y = iris.target
12
- colors = "bry"
13
- idx = np.arange(X.shape[0])
14
- np.random.seed(13)
15
- np.random.shuffle(idx)
16
- X = X[idx]
17
- y = y[idx]
18
- mean = X.mean(axis=0)
19
- std = X.std(axis=0)
20
- X = (X - mean) / std
21
- clf = SGDClassifier(alpha=0.001, max_iter=100).fit(X, y)
22
- predicted_class = clf.predict(np.array([[x, y]]))[0]
23
- return iris.target_names[predicted_class]
24
 
25
- def decision_boundary(x_min, x_max, y_min, y_max):
 
26
  iris = datasets.load_iris()
 
 
 
27
  X = iris.data[:, :2]
28
  y = iris.target
29
  colors = "bry"
 
 
30
  idx = np.arange(X.shape[0])
31
  np.random.seed(13)
32
  np.random.shuffle(idx)
33
  X = X[idx]
34
  y = y[idx]
 
 
35
  mean = X.mean(axis=0)
36
  std = X.std(axis=0)
37
  X = (X - mean) / std
38
- clf = SGDClassifier(alpha=0.001, max_iter=100).fit(X, y)
 
39
  ax = plt.gca()
40
  DecisionBoundaryDisplay.from_estimator(
41
  clf,
@@ -46,54 +50,49 @@ def decision_boundary(x_min, x_max, y_min, y_max):
46
  xlabel=iris.feature_names[0],
47
  ylabel=iris.feature_names[1],
48
  )
49
- plt.axis([x_min, x_max, y_min, y_max])
50
- plt.xticks(fontsize=8)
51
- plt.yticks(fontsize=8)
52
- plt.gcf().set_size_inches(5, 4)
53
- return plt.gcf()
54
-
55
- iris = datasets.load_iris()
56
-
57
- inputs = [
58
- gr.inputs.Slider(0, 8, label=iris.feature_names[0], default=5.8, decimal=1),
59
- gr.inputs.Slider(0, 8, label=iris.feature_names[1], default=3.5, decimal=1),
60
- ]
61
-
62
- output = gr.outputs.Label(num_top_classes=1)
63
-
64
- title = "Iris Dataset - Decision Boundary"
65
- description = "Predict the class of the given data point and show the decision boundary of the SGD classifier."
66
- article = "<p><a href='https://scikit-learn.org/stable/auto_examples/linear_model/plot_sgd_iris.html'>More about the dataset and the example</a></p>"
67
- examples = [
68
- [
69
- 5.8,
70
- 3.5,
71
- ],
72
- [
73
- 7.2,
74
- 3.2,
75
- ],
76
- [
77
- 5.1,
78
- 2.5,
79
- ],
80
- [
81
- 4.9,
82
- 3.1,
83
- ],
84
- ]
85
-
86
- gr.Interface(
87
- predict_class,
88
- inputs,
89
- output,
90
- title=title,
91
- description=description,
92
- examples=examples,
93
- theme=theme,
94
- article=article,
95
- layout="vertical",
96
- allow_flagging=False,
97
- live=True,
98
- outputs=[None, decision_boundary],
99
- ).launch()
 
1
+
2
+ """
3
+ ========================================
4
+ Plot multi-class SGD on the iris dataset
5
+ ========================================
6
+
7
+ Plot decision surface of multi-class SGD on iris dataset.
8
+ The hyperplanes corresponding to the three one-versus-all (OVA) classifiers
9
+ are represented by the dashed lines.
10
+
11
+ """
12
  import gradio as gr
13
  import numpy as np
14
  import matplotlib.pyplot as plt
 
16
  from sklearn.linear_model import SGDClassifier
17
  from sklearn.inspection import DecisionBoundaryDisplay
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ def plot(alpha):
21
+ # import some data to play with
22
  iris = datasets.load_iris()
23
+
24
+ # we only take the first two features. We could
25
+ # avoid this ugly slicing by using a two-dim dataset
26
  X = iris.data[:, :2]
27
  y = iris.target
28
  colors = "bry"
29
+
30
+ # shuffle
31
  idx = np.arange(X.shape[0])
32
  np.random.seed(13)
33
  np.random.shuffle(idx)
34
  X = X[idx]
35
  y = y[idx]
36
+
37
+ # standardize
38
  mean = X.mean(axis=0)
39
  std = X.std(axis=0)
40
  X = (X - mean) / std
41
+
42
+ clf = SGDClassifier(alpha=alpha, max_iter=100).fit(X, y)
43
  ax = plt.gca()
44
  DecisionBoundaryDisplay.from_estimator(
45
  clf,
 
50
  xlabel=iris.feature_names[0],
51
  ylabel=iris.feature_names[1],
52
  )
53
+ plt.axis("tight")
54
+
55
+ # Plot also the training points
56
+ for i, color in zip(clf.classes_, colors):
57
+ idx = np.where(y == i)
58
+ plt.scatter(
59
+ X[idx, 0],
60
+ X[idx, 1],
61
+ c=color,
62
+ label=iris.target_names[i],
63
+ cmap=plt.cm.Paired,
64
+ edgecolor="black",
65
+ s=20,
66
+ )
67
+ plt.title("Decision surface of multi-class SGD")
68
+ plt.axis("tight")
69
+
70
+ # Plot the three one-against-all classifiers
71
+ xmin, xmax = plt.xlim()
72
+ ymin, ymax = plt.ylim()
73
+ coef = clf.coef_
74
+ intercept = clf.intercept_
75
+
76
+
77
+ def plot_hyperplane(c, color):
78
+ def line(x0):
79
+ return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1]
80
+
81
+ plt.plot([xmin, xmax], [line(xmin), line(xmax)], ls="--", color=color)
82
+
83
+ for i, color in zip(clf.classes_, colors):
84
+ plot_hyperplane(i, color)
85
+ plt.legend()
86
+ #plt.show()
87
+ return plt
88
+
89
+ #pl = plot(0)
90
+ #pl.show()
91
+ with gr.Blocks() as demo:
92
+ alpha = gr.Slider(minimum=0.0001, maximum=10, step=0.01, value=0.0001, label="Alpha Value")
93
+ with gr.Row():
94
+ plt = gr.Plot()
95
+
96
+ alpha.change(fn=plot, inputs=[alpha],outputs=[plt])
97
+
98
+ demo.launch()