jiehou commited on
Commit
b5ad301
·
1 Parent(s): 113eaa5

Create new file

Browse files
Files changed (1) hide show
  1. app.py +184 -0
app.py ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## CSCI4750/5750-SLU
2
+ # Jie Hou, Oct 5, 2022
3
+
4
+ import gradio as gr
5
+ import numpy as np
6
+ import pandas as pd
7
+ from matplotlib import pyplot as plt
8
+ import matplotlib.colors as colors
9
+ import itertools
10
+ from scipy.stats import norm
11
+ from scipy import stats
12
+ from sklearn.naive_bayes import GaussianNB
13
+
14
+
15
+
16
+ def gaussian(x, n, u, s):
17
+ #u = x.mean()
18
+ #s = x.std()
19
+
20
+ # divide [x.min(), x.max()] by n
21
+ x = np.linspace(x.min(), x.max(), n)
22
+
23
+ a = ((x - u) ** 2) / (2 * (s ** 2))
24
+ y = 1 / (s * np.sqrt(2 * np.pi)) * np.exp(-a)
25
+
26
+ return x, y, u, s
27
+
28
+ def plot_figure(N, u1, std1, u2, std2, show_dist, classifier=None):
29
+ #N = 100
30
+ import numpy as np
31
+ import matplotlib.pyplot as pp
32
+ pp.style.use('default')
33
+ val = 0. # this is the value where you want the data to appear on the y-axis.
34
+
35
+ points_class1 = [stats.norm.rvs(loc=u1, scale = std1) for x in range(N)]
36
+ points_class2 = [stats.norm.rvs(loc=u2, scale = std2) for x in range(N)]
37
+
38
+ pd_class1 = pd.DataFrame({'Feature 1 (X)': points_class1, 'Label (Y)': np.repeat(0,len(points_class1))})
39
+ pd_class2 = pd.DataFrame({'Feature 1 (X)': points_class2, 'Label (Y)': np.repeat(1,len(points_class2))})
40
+
41
+
42
+ pd_all = pd.concat([pd_class1, pd_class2]).reset_index(drop=True)
43
+
44
+ import numpy as np
45
+ X_data= pd_all['Feature 1 (X)'].to_numpy().reshape((len(pd_all),1))
46
+ y_labels= pd_all['Label (Y)']
47
+
48
+
49
+ # define x, y limits
50
+ x_min, x_max = X_data[:, 0].min() - 1, X_data[:, 0].max() + 1
51
+ y_min, y_max = 0-1, 1 + 1
52
+
53
+ fig = pp.figure(figsize=(8, 6)) # figure size in inches
54
+ fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.3, wspace=0.05)
55
+
56
+
57
+ pp.tick_params(left = False, right = False , labelleft = False ,
58
+ labelbottom = True, bottom = False)
59
+
60
+ #reference = [stats.uniform.rvs(loc=1, scale = 1) for x in range(N)]
61
+ pp.plot(points_class1, np.zeros_like(points_class1) + val, 'x', label = 'Class 1', markersize = 10)
62
+ pp.plot(points_class2, np.zeros_like(points_class2) + val, 'o', label = 'Class 2', markersize = 10)
63
+
64
+
65
+ if show_dist:
66
+ x = np.arange(x_min, x_max, 0.01, dtype=np.float) # define range of x
67
+ x, y, u, s = gaussian(x, 10000, np.mean(points_class1), np.std(points_class1) )
68
+ pp.plot(x, y)
69
+ #pp.plot(x, y, label=r'$Gaussian (\mu=%.2f,\ \sigma=%.2f)$' % (u, s))
70
+
71
+
72
+ x = np.arange(x_min, x_max, 0.01, dtype=np.float) # define range of x
73
+ x, y, u, s = gaussian(x, 10000, np.mean(points_class2), np.std(points_class2) )
74
+ pp.plot(x, y)
75
+ #pp.plot(x, y, label=r'$Gaussian (\mu=%.2f,\ \sigma=%.2f)$' % (u, s))
76
+
77
+
78
+
79
+ ### draw decision boundary on knn
80
+
81
+ import numpy as np
82
+ from matplotlib import pyplot as plt
83
+ from sklearn import neighbors, datasets
84
+ from matplotlib.colors import ListedColormap
85
+
86
+ # Create color maps for 3-class classification problem, as with iris
87
+ cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA'])
88
+ cmap_bold = ListedColormap(['#FF0000', '#00FF00'])
89
+
90
+
91
+ xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100),
92
+ np.linspace(y_min, y_max, 100))
93
+
94
+
95
+ if classifier == 'LDA':
96
+ from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
97
+ model_sk = LinearDiscriminantAnalysis()
98
+ model_sk.fit(X_data,y_labels)
99
+ zz = model_sk.predict(np.c_[xx.ravel()])
100
+
101
+ #Predictions for each point on meshgrid
102
+ #zz = np.array( [model_sk.predict( [[xx,yy]])[0] for xx, yy in zip(np.ravel(X), np.ravel(Y)) ] )
103
+
104
+ #Reshaping the predicted class into the meshgrid shape
105
+ Z = zz.reshape(xx.shape)
106
+
107
+ pp.pcolormesh(xx, yy, Z, cmap=cmap_light, alpha=0.2)
108
+ elif classifier == 'QDA':
109
+ from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
110
+ model_sk = QuadraticDiscriminantAnalysis()
111
+ model_sk.fit(X_data,y_labels)
112
+
113
+ model_sk.fit(X_data,y_labels)
114
+ zz = model_sk.predict(np.c_[xx.ravel()])
115
+
116
+ #Predictions for each point on meshgrid
117
+ #zz = np.array( [model_sk.predict( [[xx,yy]])[0] for xx, yy in zip(np.ravel(X), np.ravel(Y)) ] )
118
+
119
+ #Reshaping the predicted class into the meshgrid shape
120
+ Z = zz.reshape(xx.shape)
121
+
122
+ print("Z: ",Z)
123
+ pp.pcolormesh(xx, yy, Z, cmap=cmap_light, alpha=0.2)
124
+ elif classifier == 'NaiveBayes':
125
+ from sklearn.naive_bayes import GaussianNB
126
+ model_sk = GaussianNB(priors = None)
127
+ model_sk.fit(X_data,y_labels)
128
+ Z = model_sk.predict(np.c_[xx.ravel()])
129
+ Z = Z.reshape(xx.shape)
130
+
131
+ pp.pcolormesh(xx, yy, Z, cmap=cmap_light, alpha=0.2)
132
+
133
+
134
+
135
+ pp.xlim([x_min, x_max])
136
+ pp.ylim([y_min, y_max])
137
+ pp.xlabel("Feature 1 (X1)", size=20)
138
+ pp.xticks(fontsize=20)
139
+ pp.ylabel("Feature 2 (X2)")
140
+ pp.legend(loc='upper right', borderpad=0, handletextpad=0, fontsize = 20)
141
+ pp.savefig('plot.png')
142
+
143
+ return 'plot.png', pd_all
144
+
145
+
146
+
147
+ set_mean_class1 = gr.inputs.Slider(-20, 20, step=0.5, default=1, label = 'Mean (Class 1)')
148
+ set_std_class1 = gr.inputs.Slider(0, 10, step=0.5, default=1.5, label = 'Standard Deviation (Class 1)')
149
+
150
+ # 2. define mean and standard deviation for class 2
151
+
152
+ set_mean_class2 = gr.inputs.Slider(-20, 20, step=0.5, default=10, label = 'Mean (Class 2)')
153
+ set_std_class2 = gr.inputs.Slider(0, 10, step=0.5, default=1.5, label = 'Standard Deviation (Class 2)')
154
+
155
+ # 3. Define the number of data points
156
+ set_number_points = gr.inputs.Slider(10, 100, step=5, default=20, label = 'Number of samples in each class')
157
+
158
+ # 4. show distribution or not
159
+ set_show_dist = gr.inputs.Checkbox(label="Show data distribution")
160
+
161
+ # 5. set classifier type
162
+ set_classifier = gr.inputs.Dropdown(["None", "LDA", "QDA", "NaiveBayes"])
163
+
164
+ # 6. define output imagem model
165
+ set_out_plot_images = gr.outputs.Image(label="Data visualization")
166
+
167
+ set_out_plot_table = gr.outputs.Dataframe(type='pandas', label ='Simulated Dataset')
168
+
169
+
170
+
171
+
172
+
173
+ ### configure gradio, detailed can be found at https://www.gradio.app/docs/#i_slider
174
+ interface = gr.Interface(fn=plot_figure,
175
+ inputs=[set_number_points,set_mean_class1,set_std_class1,set_mean_class2,set_std_class2, set_show_dist, set_classifier],
176
+ outputs=[set_out_plot_images,set_out_plot_table],
177
+ examples_per_page = 2,
178
+ #examples = get_sample_data(10),
179
+ title="CSCI4750/5750 Demo: Web Application for Probabilistic Classifier (Single feature)",
180
+ description= "Click examples below for a quick demo",
181
+ theme = 'huggingface',
182
+ layout = 'vertical', live=True
183
+ )
184
+ interface.launch(debug=True)