| |
| |
| import os |
| os.system("pip uninstall -y gradio") |
| os.system("pip install gradio==2.6.4") |
|
|
| import gradio as gr |
| import numpy as np |
| import pandas as pd |
| from matplotlib import pyplot as plt |
| import matplotlib.colors as colors |
| import itertools |
| from scipy.stats import norm |
| from scipy import stats |
| from sklearn.naive_bayes import GaussianNB |
|
|
|
|
|
|
| def gaussian(x, n, u, s): |
| |
| |
|
|
| |
| x = np.linspace(x.min(), x.max(), n) |
|
|
| a = ((x - u) ** 2) / (2 * (s ** 2)) |
| y = 1 / (s * np.sqrt(2 * np.pi)) * np.exp(-a) |
|
|
| return x, y, u, s |
| |
| import gradio as gr |
| |
|
|
| set_fea1_mean_class1 = gr.inputs.Slider(0, 20, step=0.5, default=1, label = 'Feature_1 Mean (Class 1)') |
| set_fea1_var_class1 = gr.inputs.Slider(0, 10, step=0.5, default=2.5, label = 'Feature_1 Variance (Class 1)') |
|
|
| set_fea2_mean_class1 = gr.inputs.Slider(0, 20, step=0.5, default=2, label = 'Feature_2 Mean (Class 1)') |
| set_fea2_var_class1 = gr.inputs.Slider(0, 10, step=0.5, default=4.5, label = 'Feature_2 Variance (Class 1)') |
|
|
| set_fea_covariance_class1 = gr.inputs.Slider(0, 10, step=0.5, default=2.5, label = 'Feature_1_2 Co-Variance (Class 1)') |
|
|
| |
|
|
| set_fea1_mean_class2 = gr.inputs.Slider(0, 20, step=0.5, default=7, label = 'Feature_1 Mean (Class 2)') |
| set_fea1_var_class2 = gr.inputs.Slider(0, 10, step=0.5, default=3.5, label = 'Feature_1 Variance (Class 2)') |
|
|
| set_fea2_mean_class2 = gr.inputs.Slider(0, 20, step=0.5, default=8, label = 'Feature_2 Mean (Class 2)') |
| set_fea2_var_class2 = gr.inputs.Slider(0, 10, step=0.5, default=3.5, label = 'Feature_2 Variance (Class 2)') |
|
|
| set_fea_covariance_class2 = gr.inputs.Slider(0, 10, step=0.5, default=3.5, label = 'Feature_1_2 Co-Variance (Class 2)') |
|
|
| |
| set_number_points = gr.inputs.Slider(10, 100, step=5, default=20, label = 'Number of samples in each class') |
|
|
| |
| set_classifier = gr.inputs.Dropdown(["None", "LDA", "QDA", "NaiveBayes"]) |
|
|
| |
| set_out_plot_images = gr.outputs.Image(label="Data visualization") |
|
|
| set_out_plot_table = gr.outputs.Dataframe(type='pandas', label ='Simulated Dataset') |
|
|
|
|
|
|
| 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): |
| |
| |
| |
| import numpy as np |
| import matplotlib.pyplot as pp |
| pp.style.use('default') |
| val = 0. |
|
|
| np.random.seed(seed = 3) |
|
|
| mu1 = [fea1_u1, fea2_u1] |
| sigma1 = [[np.sqrt(fea1_var1), np.sqrt(covariance1)], [np.sqrt(covariance1), np.sqrt(fea2_var1)]] |
| points_class1_fea1, points_class1_fea2 = np.random.multivariate_normal(mu1, sigma1, N).T |
|
|
| mu2 = [fea1_u2, fea2_u2] |
| sigma2 = [[np.sqrt(fea1_var2), np.sqrt(covariance2)], [np.sqrt(covariance2), np.sqrt(fea2_var2)]] |
| points_class2_fea1, points_class2_fea2 = np.random.multivariate_normal(mu2, sigma2, N).T |
|
|
| mu_list = [mu1,mu2] |
| sigma_list = [sigma1,sigma2] |
| color_list = ['darkblue','darkgreen'] |
|
|
|
|
| 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))}) |
| 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))}) |
|
|
|
|
| pd_all = pd.concat([pd_class1, pd_class2]).reset_index(drop=True) |
|
|
| import numpy as np |
| |
| |
|
|
|
|
| |
| X_data = np.asarray(np.vstack((np.hstack((points_class1_fea1, points_class1_fea2)),np.hstack((points_class2_fea1, points_class2_fea2)))).T) |
| y_labels = np.hstack((np.zeros(N),np.ones(N))) |
|
|
| print("X_data: ",X_data.shape) |
|
|
| fig = pp.figure(figsize=(8, 6)) |
| fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.3, wspace=0.05) |
|
|
|
|
| |
| |
|
|
| |
| pp.plot(points_class1_fea1, points_class1_fea2 + val, 'x', label = 'Class 1', markersize = 10) |
| pp.plot(points_class2_fea1, points_class2_fea2 + val, 'o', label = 'Class 2', markersize = 10) |
|
|
|
|
|
|
| |
| |
| |
| |
|
|
| x_min, x_max = -5, 15 |
| y_min, y_max = -5, 15 |
| X_grid, Y_grid = np.meshgrid(np.linspace(x_min, x_max, 100), |
| np.linspace(y_min, y_max, 100)) |
| |
|
|
|
|
| |
|
|
| import numpy as np |
| from matplotlib import pyplot as plt |
| from sklearn import neighbors, datasets |
| from matplotlib.colors import ListedColormap |
|
|
| |
| cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA']) |
| cmap_bold = ListedColormap(['#FF0000', '#00FF00']) |
|
|
|
|
| if classifier == 'LDA': |
| from sklearn.discriminant_analysis import LinearDiscriminantAnalysis |
| model_sk = LinearDiscriminantAnalysis() |
| model_sk.fit(X_data,y_labels) |
|
|
| X_grid, Y_grid = np.meshgrid(np.linspace(x_min, x_max, N), |
| np.linspace(y_min, y_max, N)) |
| |
| zz = np.array( [model_sk.predict( [[xx,yy]])[0] for xx, yy in zip(np.ravel(X_grid), np.ravel(Y_grid)) ] ) |
|
|
| Z = zz.reshape(X_grid.shape) |
|
|
| pp.pcolormesh(X_grid, Y_grid, Z, cmap=cmap_light, alpha=0.2) |
| |
|
|
| elif classifier == 'QDA': |
| from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis |
| model_sk = QuadraticDiscriminantAnalysis() |
| model_sk.fit(X_data,y_labels) |
|
|
| X_grid, Y_grid = np.meshgrid(np.linspace(x_min, x_max, N), |
| np.linspace(y_min, y_max, N)) |
| |
| zz = np.array( [model_sk.predict( [[xx,yy]])[0] for xx, yy in zip(np.ravel(X_grid), np.ravel(Y_grid)) ] ) |
|
|
| Z = zz.reshape(X_grid.shape) |
|
|
| pp.pcolormesh(X_grid, Y_grid, Z, cmap=cmap_light, alpha=0.2) |
|
|
| elif classifier == 'NaiveBayes': |
| from sklearn.naive_bayes import GaussianNB |
| model_sk = GaussianNB(priors = None) |
| model_sk.fit(X_data,y_labels) |
|
|
| X_grid, Y_grid = np.meshgrid(np.linspace(x_min, x_max, N), |
| np.linspace(y_min, y_max, N)) |
| |
| zz = np.array( [model_sk.predict( [[xx,yy]])[0] for xx, yy in zip(np.ravel(X_grid), np.ravel(Y_grid)) ] ) |
|
|
| Z = zz.reshape(X_grid.shape) |
|
|
| pp.pcolormesh(X_grid, Y_grid, Z, cmap=cmap_light, alpha=0.2) |
|
|
|
|
| |
|
|
| |
| |
|
|
|
|
| print(x_min, x_max) |
| print(y_min, y_max) |
| |
| |
| pp.xlim([x_min, x_max]) |
| pp.ylim([y_min, y_max]) |
| pp.xlabel("Feature 1 (X)", size=20) |
| pp.xticks(fontsize=20) |
| pp.yticks(fontsize=20) |
| pp.ylabel("Feature 2 (X)", size=20) |
| pp.legend(loc='upper right', borderpad=0, handletextpad=0, fontsize = 20) |
| pp.savefig('plot.png') |
|
|
| return 'plot.png', pd_all |
|
|
|
|
| |
| 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], |
| outputs=[set_out_plot_images,set_out_plot_table], |
| examples_per_page = 2, |
| |
| title="CSCI4750/5750 Demo: Web Application for Probabilistic Classifier (Two features)", |
| description= "Click examples below for a quick demo", |
| theme = 'huggingface' |
| ) |
| interface.launch(debug=True) |
|
|