How to do it...

Let's see how we can perform a kernel PCA:

  1. Create a new Python file and import the following packages (the full code is given in the kpca.py file that is provided for you):
import numpy as np 
import matplotlib.pyplot as plt 
 
from sklearn.decomposition import PCA, KernelPCA 
from sklearn.datasets import make_circles 
  1. Define the seed value for the random number generator. This is needed to generate data samples for analysis:
# Set the seed for random number generator 
np.random.seed(7) 
  1. Generate data that is distributed in concentric circles to demonstrate how PCA doesn't work in this case:
# Generate samples 
X, y = make_circles(n_samples=500, factor=0.2, noise=0.04)
  1. Perform PCA on this data:
# Perform PCA pca = PCA() ...

Get Python Machine Learning Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.