Spaces:
Sleeping
Sleeping
Add kernel selection
Browse files
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(
|
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=
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
btn = gr.Button(value="Submit")
|
42 |
-
|
|
|
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()
|