kris-123 commited on
Commit
4aefecb
1 Parent(s): 844f7ed

Add kernel pca fitting and testing

Browse files
Files changed (1) hide show
  1. app.py +36 -1
app.py CHANGED
@@ -4,4 +4,39 @@ def greet(name):
4
  return "Hello " + name + "!!"
5
 
6
  iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  return "Hello " + name + "!!"
5
 
6
  iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
+ iface.launch()
8
+
9
+ from sklearn.datasets import make_circles
10
+ from sklearn.model_selection import train_test_split
11
+
12
+ X, y = make_circles(n_samples=1_000, factor=0.3, noise=0.05, random_state=0)
13
+ X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0)
14
+
15
+ from sklearn.decomposition import PCA, KernelPCA
16
+
17
+ pca = PCA(n_components=2)
18
+ kernel_pca = KernelPCA(
19
+ n_components=None, kernel="rbf", gamma=10, fit_inverse_transform=True, alpha=0.1
20
+ )
21
+
22
+ X_test_pca = pca.fit(X_train).transform(X_test)
23
+ X_test_kernel_pca = kernel_pca.fit(X_train).transform(X_test)
24
+
25
+ fig, (orig_data_ax, pca_proj_ax, kernel_pca_proj_ax) = plt.subplots(
26
+ ncols=3, figsize=(14, 4)
27
+ )
28
+
29
+ orig_data_ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test)
30
+ orig_data_ax.set_ylabel("Feature #1")
31
+ orig_data_ax.set_xlabel("Feature #0")
32
+ orig_data_ax.set_title("Testing data")
33
+
34
+ pca_proj_ax.scatter(X_test_pca[:, 0], X_test_pca[:, 1], c=y_test)
35
+ pca_proj_ax.set_ylabel("Principal component #1")
36
+ pca_proj_ax.set_xlabel("Principal component #0")
37
+ pca_proj_ax.set_title("Projection of testing data\n using PCA")
38
+
39
+ kernel_pca_proj_ax.scatter(X_test_kernel_pca[:, 0], X_test_kernel_pca[:, 1], c=y_test)
40
+ kernel_pca_proj_ax.set_ylabel("Principal component #1")
41
+ kernel_pca_proj_ax.set_xlabel("Principal component #0")
42
+ _ = kernel_pca_proj_ax.set_title("Projection of testing data\n using KernelPCA")