Title: Dimensionality Reduction With Kernel PCA
Slug: dimensionality_reduction_with_kernel_pca
Summary: How to reduce the dimensions of the feature matrix using kernels for machine learning in Python. Date: 2017-09-09 12:00
Category: Machine Learning
Tags: Feature Engineering Authors: Chris Albon

Preliminaries


In [1]:
# Load libraries
from sklearn.decomposition import PCA, KernelPCA
from sklearn.datasets import make_circles

Create Linearly Inseparable Data


In [2]:
# Create linearly inseparable data
X, _ = make_circles(n_samples=1000, random_state=1, noise=0.1, factor=0.1)

Conduct Kernel PCA


In [3]:
# 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


In [4]:
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