4
))
train_ax
.
scatter
(
X_train
[:,
0
],
X_train
[:,
1
],
c
=
y_train
)
train_ax
.
set_ylabel
(
"Feature #1"
)
train_ax
.
set_xlabel
(
"Feature #0"
)
train_ax
.
set_title
(
"Training data"
)
test_ax
.
scatter
(
X_test
[:,
0
],
X_test
[:,
1
],
c
=
y_test
)
test_ax
.
set_xlabel
(
"Feature #0"
)
_
=
test_ax
.
set_title
(
"Testing data"
)
,
kernel
=
"rbf"
,
gamma
=
10
,
fit_inverse_transform
=
True
,
alpha
=
0.1
)
X_test_pca
=
pca
.
fit
(
X_train
)
.
transform
(
X_test
)
X_test_kernel_pca
=
kernel_pca
.
fit
(
X_train
)
.
transform
(
X_test
)
X_test
[:,
1
],
c
=
y_test
)
orig_data_ax
.
set_ylabel
(
"Feature #1"
)
orig_data_ax
.
set_xlabel
(
"Feature #0"
)
orig_data_ax
.
set_title
(
"Testing data"
)
pca_proj_ax
.
scatter
(
X_test_pca
[:,
0
],
X_test_pca
[:,
1
],
c
=
y_test
)
pca_proj_ax
.
set_ylabel
(
"Principal component #1"
)
pca_proj_ax
.
set_xlabel
(
"Principal component #0"
)
pca_proj_ax
.
set_title
(
"Projection of testing data
\n
using PCA"
)
kernel_pca_proj_ax
.
scatter
(
X_test_kernel_pca
[:,
0
],
X_test_kernel_pca
[:,
1
],
c
=
y_test
)
kernel_pca_proj_ax
.
set_ylabel
(
"Principal component #1"
)
kernel_pca_proj_ax
.
set_xlabel
(
"Principal component #0"
)
_
=
kernel_pca_proj_ax
.
set_title
(
"Projection of testing data
\n
using KernelPCA"
)
.
scatter
(
X_test
[:,
0
],
X_test
[:,
1
],
c
=
y_test
)
orig_data_ax
.
set_ylabel
(
"Feature #1"
)
orig_data_ax
.
set_xlabel
(
"Feature #0"
)
orig_data_ax
.
set_title
(
"Original test data"
)
pca_back_proj_ax
.
scatter
(
X_reconstructed_pca
[:,
0
],
X_reconstructed_pca
[:,
1
],
c
=
y_test
)
pca_back_proj_ax
.
set_xlabel
(
"Feature #0"
)
pca_back_proj_ax
.
set_title
(
"Reconstruction via PCA"
)
kernel_pca_back_proj_ax
.
scatter
(
X_reconstructed_kernel_pca
[:,
0
],
X_reconstructed_kernel_pca
[:,
1
],
c
=
y_test
)
kernel_pca_back_proj_ax
.
set_xlabel
(
"Feature #0"
)
_
=
kernel_pca_back_proj_ax
.
set_title
(
"Reconstruction via KernelPCA"
)
Download zipped: plot_kernel_pca.zip