Dimensionality Reduction With Kernel PCA


# Load libraries
from sklearn.decomposition import PCA, KernelPCA
from sklearn.datasets import make_circles

Create Linearly Inseparable Data

# Create linearly inseparable data
X, _ = make_circles(n_samples=1000, random_state=1, noise=0.1, factor=0.1)

Conduct Kernel PCA

# Apply kernal PCA with radius basis function (RBF) kernel
kpca = KernelPCA(kernel="rbf", gamma=15, n_components=1)
X_kpca = kpca.fit_transform(X)

View Results

print('Original number of features:', X.shape[1])
print('Reduced number of features:', X_kpca.shape[1])
Original number of features: 2
Reduced number of features: 1