jiehou commited on
Commit
74ac803
·
1 Parent(s): 01560a0

Create new file

Browse files
Files changed (1) hide show
  1. app.py +216 -0
app.py ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### CSCI 4750/5750- SLU
2
+ ### Jie Hou, Oct 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
+ import gradio as gr
29
+ # 1. define mean and standard deviation for class 1
30
+
31
+ set_fea1_mean_class1 = gr.inputs.Slider(0, 20, step=0.5, default=1, label = 'Feature_1 Mean (Class 1)')
32
+ set_fea1_var_class1 = gr.inputs.Slider(0, 10, step=0.5, default=1.5, label = 'Feature_1 Variance (Class 1)')
33
+
34
+ set_fea2_mean_class1 = gr.inputs.Slider(0, 20, step=0.5, default=2, label = 'Feature_2 Mean (Class 1)')
35
+ set_fea2_var_class1 = gr.inputs.Slider(0, 10, step=0.5, default=1.5, label = 'Feature_2 Variance (Class 1)')
36
+
37
+ set_fea_covariance_class1 = gr.inputs.Slider(0, 10, step=0.5, default=1.5, label = 'Feature_1_2 Co-Variance (Class 1)')
38
+
39
+ # 2. define mean and standard deviation for class 2
40
+
41
+ set_fea1_mean_class2 = gr.inputs.Slider(0, 20, step=0.5, default=5, label = 'Feature_1 Mean (Class 2)')
42
+ set_fea1_var_class2 = gr.inputs.Slider(0, 10, step=0.5, default=1.5, label = 'Feature_1 Variance (Class 2)')
43
+
44
+ set_fea2_mean_class2 = gr.inputs.Slider(0, 20, step=0.5, default=6, label = 'Feature_2 Mean (Class 2)')
45
+ set_fea2_var_class2 = gr.inputs.Slider(0, 10, step=0.5, default=1.5, label = 'Feature_2 Variance (Class 2)')
46
+
47
+ set_fea_covariance_class2 = gr.inputs.Slider(0, 10, step=0.5, default=1.5, label = 'Feature_1_2 Co-Variance (Class 2)')
48
+
49
+ # 3. Define the number of data points
50
+ set_number_points = gr.inputs.Slider(10, 100, step=5, default=20, label = 'Number of samples in each class')
51
+
52
+ # 5. set classifier type
53
+ set_classifier = gr.inputs.Dropdown(["None", "LDA", "QDA", "NaiveBayes"])
54
+
55
+ # 6. define output imagem model
56
+ set_out_plot_images = gr.outputs.Image(label="Data visualization")
57
+
58
+ set_out_plot_table = gr.outputs.Dataframe(type='pandas', label ='Simulated Dataset')
59
+
60
+
61
+
62
+ def plot_figure_twofeature(N, fea1_u1, fea1_var1, fea2_u1, fea2_var1, covariance1, fea1_u2, fea1_var2, fea2_u2, fea2_var2, covariance2, classifier=None):
63
+
64
+
65
+ #N = 100
66
+ import numpy as np
67
+ import matplotlib.pyplot as pp
68
+ pp.style.use('default')
69
+ val = 0. # this is the value where you want the data to appear on the y-axis.
70
+
71
+ np.random.seed(seed = 3)
72
+
73
+ mu1 = [fea1_u1, fea2_u1]
74
+ sigma1 = [[np.sqrt(fea1_var1), np.sqrt(covariance1)], [np.sqrt(covariance1), np.sqrt(fea2_var1)]]
75
+ points_class1_fea1, points_class1_fea2 = np.random.multivariate_normal(mu1, sigma1, N).T
76
+
77
+ mu2 = [fea1_u2, fea2_u2]
78
+ sigma2 = [[np.sqrt(fea1_var2), np.sqrt(covariance2)], [np.sqrt(covariance2), np.sqrt(fea2_var2)]]
79
+ points_class2_fea1, points_class2_fea2 = np.random.multivariate_normal(mu2, sigma2, N).T
80
+
81
+ mu_list = [mu1,mu2]
82
+ sigma_list = [sigma1,sigma2]
83
+ color_list = ['darkblue','darkgreen']
84
+
85
+
86
+ pd_class1 = pd.DataFrame({'Feature 1 (X)': points_class1_fea1,'Feature 2 (X)': points_class1_fea2, 'Label (Y)': np.repeat(0,len(points_class1_fea1))})
87
+ pd_class2 = pd.DataFrame({'Feature 1 (X)': points_class2_fea1,'Feature 2 (X)': points_class2_fea2, 'Label (Y)': np.repeat(1,len(points_class2_fea1))})
88
+
89
+
90
+ pd_all = pd.concat([pd_class1, pd_class2]).reset_index(drop=True)
91
+
92
+ import numpy as np
93
+ #X_data= pd_all['Feature 1 (X)','Feature 2 (X)'].to_numpy().reshape((len(pd_all),2))
94
+ #y_labels= pd_all['Label (Y)']
95
+
96
+
97
+ #Setup X and y data
98
+ X_data = np.asarray(np.vstack((np.hstack((points_class1_fea1, points_class1_fea2)),np.hstack((points_class2_fea1, points_class2_fea2)))).T)
99
+ y_labels = np.hstack((np.zeros(N),np.ones(N)))
100
+
101
+ print("X_data: ",X_data.shape)
102
+
103
+ fig = pp.figure(figsize=(8, 6)) # figure size in inches
104
+ fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.3, wspace=0.05)
105
+
106
+
107
+ #pp.tick_params(left = False, right = False , labelleft = True ,
108
+ # labelbottom = True, bottom = False)
109
+
110
+ #reference = [stats.uniform.rvs(loc=1, scale = 1) for x in range(N)]
111
+ pp.plot(points_class1_fea1, points_class1_fea2 + val, 'x', label = 'Class 1', markersize = 10)
112
+ pp.plot(points_class2_fea1, points_class2_fea2 + val, 'o', label = 'Class 2', markersize = 10)
113
+
114
+
115
+
116
+
117
+ # define x, y limits
118
+ #x_min, x_max = X_data[:, 0].min() - 1, X_data[:, 0].max() + 1
119
+ #y_min, y_max = X_data[:, 1].min() - 1, X_data[:, 1].max() + 1
120
+
121
+ x_min, x_max = -5, 15
122
+ y_min, y_max = -5, 15
123
+ X_grid, Y_grid = np.meshgrid(np.linspace(x_min, x_max, 100),
124
+ np.linspace(y_min, y_max, 100))
125
+
126
+
127
+
128
+ ### draw decision boundary
129
+
130
+ import numpy as np
131
+ from matplotlib import pyplot as plt
132
+ from sklearn import neighbors, datasets
133
+ from matplotlib.colors import ListedColormap
134
+
135
+ # Create color maps for 3-class classification problem, as with iris
136
+ cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA'])
137
+ cmap_bold = ListedColormap(['#FF0000', '#00FF00'])
138
+
139
+
140
+ if classifier == 'LDA':
141
+ from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
142
+ model_sk = LinearDiscriminantAnalysis()
143
+ model_sk.fit(X_data,y_labels)
144
+
145
+ X_grid, Y_grid = np.meshgrid(np.linspace(x_min, x_max, N),
146
+ np.linspace(y_min, y_max, N))
147
+ #Predictions for each point on meshgrid
148
+ zz = np.array( [model_sk.predict( [[xx,yy]])[0] for xx, yy in zip(np.ravel(X_grid), np.ravel(Y_grid)) ] )
149
+
150
+ Z = zz.reshape(X_grid.shape)
151
+
152
+ pp.pcolormesh(X_grid, Y_grid, Z, cmap=cmap_light, alpha=0.2)
153
+ #pp.contour( X_grid, Y_grid, Z, 1, alpha = .3, colors = ('red'))
154
+
155
+ elif classifier == 'QDA':
156
+ from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
157
+ model_sk = QuadraticDiscriminantAnalysis()
158
+ model_sk.fit(X_data,y_labels)
159
+
160
+ X_grid, Y_grid = np.meshgrid(np.linspace(x_min, x_max, N),
161
+ np.linspace(y_min, y_max, N))
162
+ #Predictions for each point on meshgrid
163
+ zz = np.array( [model_sk.predict( [[xx,yy]])[0] for xx, yy in zip(np.ravel(X_grid), np.ravel(Y_grid)) ] )
164
+
165
+ Z = zz.reshape(X_grid.shape)
166
+
167
+ pp.pcolormesh(X_grid, Y_grid, Z, cmap=cmap_light, alpha=0.2)
168
+
169
+ elif classifier == 'NaiveBayes':
170
+ from sklearn.naive_bayes import GaussianNB
171
+ model_sk = GaussianNB(priors = None)
172
+ model_sk.fit(X_data,y_labels)
173
+
174
+ X_grid, Y_grid = np.meshgrid(np.linspace(x_min, x_max, N),
175
+ np.linspace(y_min, y_max, N))
176
+ #Predictions for each point on meshgrid
177
+ zz = np.array( [model_sk.predict( [[xx,yy]])[0] for xx, yy in zip(np.ravel(X_grid), np.ravel(Y_grid)) ] )
178
+
179
+ Z = zz.reshape(X_grid.shape)
180
+
181
+ pp.pcolormesh(X_grid, Y_grid, Z, cmap=cmap_light, alpha=0.2)
182
+
183
+
184
+ #Reshaping the predicted class into the meshgrid shape
185
+
186
+ #Plot the contours
187
+
188
+
189
+
190
+ print(x_min, x_max)
191
+ print(y_min, y_max)
192
+ #pp.xlim([x_min-0.8*x_max, x_max+0.8*x_max])
193
+ #pp.ylim([y_min-0.8*y_max, y_max+0.8*y_max])
194
+ pp.xlim([x_min, x_max])
195
+ pp.ylim([y_min, y_max])
196
+ pp.xlabel("Feature 1 (X)", size=20)
197
+ pp.xticks(fontsize=20)
198
+ pp.yticks(fontsize=20)
199
+ pp.ylabel("Feature 2 (X)", size=20)
200
+ pp.legend(loc='upper right', borderpad=0, handletextpad=0, fontsize = 20)
201
+ pp.savefig('plot.png')
202
+
203
+ return 'plot.png', pd_all
204
+
205
+
206
+ ### configure gradio, detailed can be found at https://www.gradio.app/docs/#i_slider
207
+ interface = gr.Interface(fn=plot_figure_twofeature, inputs=[set_number_points,set_fea1_mean_class1,set_fea1_var_class1,set_fea2_mean_class1,set_fea2_var_class1,set_fea_covariance_class1,set_fea1_mean_class2,set_fea1_var_class2,set_fea2_mean_class2,set_fea2_var_class2,set_fea_covariance_class2, set_classifier],
208
+ outputs=[set_out_plot_images,set_out_plot_table],
209
+ examples_per_page = 2,
210
+ #examples = get_sample_data(10),
211
+ title="CSCI4750/5750 Demo: Web Application for Probabilistic Classifier (Two feature)",
212
+ description= "Click examples below for a quick demo",
213
+ theme = 'huggingface',
214
+ layout = 'vertical', live=True
215
+ )
216
+ interface.launch(debug=True)