| |
| |
|
|
| 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 |
| |
| def plot_figure(N, u1, std1, show_dist): |
| |
| import numpy as np |
| import matplotlib.pyplot as pp |
| pp.style.use('default') |
| val = 0. |
|
|
| points_class1 = [stats.norm.rvs(loc=u1, scale = std1) for x in range(N)] |
|
|
| pd_class1 = pd.DataFrame({'Feature 1 (X)': points_class1}) |
|
|
|
|
| pd_all = pd_class1 |
|
|
| import numpy as np |
| X_data= pd_all['Feature 1 (X)'].to_numpy().reshape((len(pd_all),1)) |
|
|
|
|
| |
| x_min, x_max = X_data[:, 0].min() - 1, X_data[:, 0].max() + 1 |
| y_min, y_max = 0-0.5, 1 |
|
|
| fig = pp.figure(figsize=(8, 6)) |
| fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.3, wspace=0.05) |
|
|
|
|
| pp.tick_params(left = False, right = False , labelleft = False , |
| labelbottom = True, bottom = False) |
|
|
| |
| pp.plot(points_class1, np.zeros_like(points_class1) + val, 'x', label = 'Feature', markersize = 10) |
|
|
|
|
| if show_dist: |
| x = np.arange(x_min, x_max, 0.01) |
| x, y, u, s = gaussian(x, 10000, np.mean(points_class1), np.std(points_class1) ) |
| pp.plot(x, y) |
| |
|
|
|
|
| pp.xlim([x_min, x_max]) |
| pp.ylim([y_min, y_max]) |
| pp.xlabel("Feature 1 (X1)", size=20) |
| pp.xticks(fontsize=20) |
| pp.ylabel("Density") |
| pp.legend(loc='upper right', borderpad=0, handletextpad=0, fontsize = 20) |
| pp.savefig('plot.png') |
|
|
| return 'plot.png', pd_all |
|
|
|
|
|
|
| set_mean_class1 = gr.Slider(-20, 20, step=0.5, label = 'Mean (Feature)') |
| set_std_class1 = gr.Slider(1, 10, step=0.5, label = 'Standard Deviation (Feature)') |
|
|
| |
| set_number_points = gr.Slider(100, 1000, step=5, label = 'Number of samples') |
|
|
| |
| set_show_dist = gr.Checkbox(label="Show data distribution") |
|
|
| |
| set_out_plot_images = gr.Image(label="Data visualization") |
|
|
| set_out_plot_table = gr.Dataframe(type='pandas', label ='Simulated Dataset') |
|
|
|
|
|
|
|
|
|
|
| |
| interface = gr.Interface(fn=plot_figure, |
| inputs=[set_number_points,set_mean_class1,set_std_class1, set_show_dist], |
| outputs=[set_out_plot_images,set_out_plot_table], |
| examples_per_page = 2, |
| |
| title="CSCI4750/5750 Demo: Web Application for Practicing Gaussian Distribution", |
| description= "Click examples below for a quick demo", |
| theme = 'huggingface',live=True |
| ) |
| interface.launch(debug=True) |
|
|