kris-123 commited on
Commit
736fdaf
1 Parent(s): e399c85

Add kernel selection

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -10,11 +10,18 @@ matplotlib.use('agg')
10
  import matplotlib.pyplot as plt
11
  import gradio as gr
12
 
13
- def fit_plot(n_comp, gamma, alpha):
14
  X, y = make_circles(n_samples=1_000, factor=0.3, noise=0.05, random_state=0)
15
  X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0)
16
- pca = PCA(n_components=n_comp)
17
- kernel_pca = KernelPCA(n_components=None, kernel="rbf", gamma=gamma, fit_inverse_transform=True, alpha=alpha)
 
 
 
 
 
 
 
18
  X_test_pca = pca.fit(X_train).transform(X_test)
19
  X_test_kernel_pca = kernel_pca.fit(X_train).transform(X_test)
20
  fig, (orig_data_ax, pca_proj_ax, kernel_pca_proj_ax) = plt.subplots(ncols=3, figsize=(14, 4))
@@ -34,11 +41,17 @@ def fit_plot(n_comp, gamma, alpha):
34
 
35
  with gr.Blocks() as demo:
36
  gr.Markdown("## PCA vs Kernel PCA")
37
- with gr.Row():
38
- p1 = gr.Slider(0, 10, label="Number of PCs", value=2, step=1)
39
- p2 = gr.Slider(0, 10, label="Kernel coefficient", value=10, step=1e-3)
40
- p3 = gr.Slider(0, 1, label="Hyperparameter for ridge regression", value=0.1, step=1e-3)
 
 
 
 
 
41
  btn = gr.Button(value="Submit")
42
- btn.click(fit_plot, inputs=[p1,p2,p3], outputs=gr.Plot(label="Projecting data with PCA and Kernel PCA "))
 
43
 
44
  demo.launch()
 
10
  import matplotlib.pyplot as plt
11
  import gradio as gr
12
 
13
+ def fit_plot(kernel, gamma, alpha, degree, coef0):
14
  X, y = make_circles(n_samples=1_000, factor=0.3, noise=0.05, random_state=0)
15
  X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0)
16
+ pca = PCA(n_components=2)
17
+ if kernel=="linear":
18
+ kernel_pca = KernelPCA(n_components=2, kernel=kernel, fit_inverse_transform=True, alpha=alpha)
19
+ elif kernel=="poly":
20
+ kernel_pca = KernelPCA(n_components=2, kernel=kernel, gamma=gamma, degree=degree, coef0=coef0, fit_inverse_transform=True, alpha=alpha)
21
+ elif kernel=="rbf":
22
+ kernel_pca = KernelPCA(n_components=2, kernel=kernel, gamma=gamma, fit_inverse_transform=True, alpha=alpha)
23
+ elif kernel=="cosine":
24
+ kernel_pca = KernelPCA(n_components=2, kernel=kernel, fit_inverse_transform=True, alpha=alpha)
25
  X_test_pca = pca.fit(X_train).transform(X_test)
26
  X_test_kernel_pca = kernel_pca.fit(X_train).transform(X_test)
27
  fig, (orig_data_ax, pca_proj_ax, kernel_pca_proj_ax) = plt.subplots(ncols=3, figsize=(14, 4))
 
41
 
42
  with gr.Blocks() as demo:
43
  gr.Markdown("## PCA vs Kernel PCA")
44
+ #state = gr.State([])
45
+ with gr.Row(variant='panel').style(equal_height=True):
46
+ p1 = gr.Dropdown(choices=["linear", "poly", "rbf", "cosine"], label="Kernel", value="rbf", interactive=True)
47
+ with gr.Row(variant='panel').style(equal_height=True):
48
+ p2 = gr.Slider(0, 10, label="Kernel coefficient (for rbf, poly and sigmoid kernels)", value=None, step=1e-3, interactive=True)
49
+ p3 = gr.Slider(0, 1, label="Alpha of ridge regression (for non-precomputed kernels)", value=1, step=1e-3, interactive=True)
50
+ with gr.Row(variant='panel').style(equal_height=True):
51
+ p4 = gr.Slider(0, 10, label="Degree (for poly kernel)", value=3, step=1, interactive=True)
52
+ p5 = gr.Slider(0, 10, label="Independent term (for poly and sigmoid kernels)", value=1, step=1e-1, interactive=True)
53
  btn = gr.Button(value="Submit")
54
+ out = gr.Plot(label="Projecting data with PCA and Kernel PCA")
55
+ btn.click(fit_plot, inputs=[p1,p2,p3,p4,p5], outputs=out)
56
 
57
  demo.launch()