sulpha commited on
Commit
a63b3d6
1 Parent(s): 1be6c40

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+ 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,
42
+ X,
43
+ cmap=plt.cm.Paired,
44
+ ax=ax,
45
+ response_method="predict",
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()