The Wayback Machine - https://web.archive.org/web/20240601141242/https://www.geeksforgeeks.org/ml-introduction-to-kernel-pca/
Open In App

ML | Introduction to Kernel PCA

Last Updated : 14 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

PRINCIPAL COMPONENT ANALYSIS: is a tool which is used to reduce the dimension of the data. It allows us to reduce the dimension of the data without much loss of information. PCA reduces the dimension by finding a few orthogonal linear combinations (principal components) of the original variables with the largest variance. The first principal component captures most of the variance in the data. The second principal component is orthogonal to the first principal component and captures the remaining variance, which is left of first principal component and so on. There are as many principal components as the number of original variables. These principal components are uncorrelated and are ordered in such a way that the first several principal components explain most of the variance of the original data. To learn more about PCA you can read the article Principal Component Analysis 

KERNEL PCA: PCA is a linear method. That is it can only be applied to datasets which are linearly separable. It does an excellent job for datasets, which are linearly separable. But, if we use it to non-linear datasets, we might get a result which may not be the optimal dimensionality reduction. Kernel PCA uses a kernel function to project dataset into a higher dimensional feature space, where it is linearly separable. It is similar to the idea of Support Vector Machines. There are various kernel methods like linear, polynomial, and gaussian. 

Kernel Principal Component Analysis (KPCA) is a technique used in machine learning for nonlinear dimensionality reduction. It is an extension of the classical Principal Component Analysis (PCA) algorithm, which is a linear method that identifies the most significant features or components of a dataset. KPCA applies a nonlinear mapping function to the data before applying PCA, allowing it to capture more complex and nonlinear relationships between the data points.

In KPCA, a kernel function is used to map the input data to a high-dimensional feature space, where the nonlinear relationships between the data points can be more easily captured by linear methods such as PCA. The principal components of the transformed data are then computed, which can be used for tasks such as data visualization, clustering, or classification.

One of the advantages of KPCA over traditional PCA is that it can handle nonlinear relationships between the input features, which can be useful for tasks such as image or speech recognition. KPCA can also handle high-dimensional datasets with many features by reducing the dimensionality of the data while preserving the most important information.

However, KPCA has some limitations, such as the need to choose an appropriate kernel function and its corresponding parameters, which can be difficult and time-consuming. KPCA can also be computationally expensive for large datasets, as it requires the computation of the kernel matrix for all pairs of data points.

References:

  1. Schölkopf, B., Smola, A., & Müller, K. R. (1998). Nonlinear component analysis as a kernel eigenvalue problem. Neural computation, 10(5), 1299-1319.
  2. Mika, S., Schölkopf, B., Smola, A. J., Müller, K. R., Scholz, M., & Rätsch, G. (1999). Kernel PCA and de-noising in feature spaces. Advances in neural information processing systems, 11, 536-542.
  3. Shawe-Taylor, J., & Cristianini, N. (2004). Kernel methods for pattern analysis. Cambridge University Press.
    Overall, KPCA is a powerful tool for nonlinear dimensionality reduction and feature extraction, but it requires careful consideration of the choice of kernel
  4. function and its parameters. It can be especially useful for high-dimensional datasets with complex relationships between the features.

Code: Create a dataset that is nonlinear and then apply PCA to the dataset. 

Python3




import matplotlib.pyplot as plt
from sklearn.datasets import make_moons
 
X, y = make_moons(n_samples=500, noise=0.02, random_state=417)
 
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.show()


non-linear data 

Code: Let’s apply PCA to this dataset.

Python3




from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
 
plt.title("PCA")
plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y)
plt.xlabel("Component 1")
plt.ylabel("Component 2")
plt.show()


 

As you can see PCA failed to distinguish the two classes.   

Code: Applying kernel PCA on this dataset with RBF kernel with a gamma value of 15. 

Python3




from sklearn.decomposition import KernelPCA
kpca = KernelPCA(kernel='rbf', gamma=15)
X_kpca = kpca.fit_transform(X)
 
plt.title("Kernel PCA")
plt.scatter(X_kpca[:, 0], X_kpca[:, 1], c=y)
plt.show()


 

In the kernel space the two classes are linearly separable. Kernel PCA uses a kernel function to project the dataset into a higher-dimensional space, where it is linearly separable. Finally, we applied the kernel PCA to a non-linear dataset using scikit-learn. 

Kernel Principal Component Analysis (PCA) is a technique for dimensionality reduction in machine learning that uses the concept of kernel functions to transform the data into a high-dimensional feature space. In traditional PCA, the data is transformed into a lower-dimensional space by finding the principal components of the covariance matrix of the data. In kernel PCA, the data is transformed into a high-dimensional feature space using a non-linear mapping function, called a kernel function, and then the principal components are found in this high-dimensional space.

Advantages of Kernel PCA:

  1. Non-linearity: Kernel PCA can capture non-linear patterns in the data that are not possible with traditional linear PCA.
  2. Robustness: Kernel PCA can be more robust to outliers and noise in the data, as it considers the global structure of the data, rather than just local distances between data points.
  3. Versatility: Different types of kernel functions can be used in kernel PCA to suit different types of data and different objectives.
  4. Kernel PCA can handle nonlinear relationships between the input features, allowing for more accurate dimensionality reduction and feature extraction compared to traditional linear PCA.
  5. It can preserve the most important information in high-dimensional datasets while reducing the dimensionality of the data, making it easier to visualize and analyze.
  6. Kernel PCA can be used for a variety of tasks, including data visualization, clustering, and classification.
  7. It is a well-established and widely used technique in machine learning, with many available libraries and resources for implementation.

Disadvantages of Kernel PCA:

  1. Complexity: Kernel PCA can be computationally expensive, especially for large datasets, as it requires the calculation of eigenvectors and eigenvalues.
  2. Model selection: Choosing the right kernel function and the right number of components can be challenging and may require expert knowledge or trial and error
  3. Choosing an appropriate kernel function and its parameters can be challenging and may require expert knowledge or extensive experimentation.
  4. Kernel PCA can be computationally expensive, especially for large datasets, as it requires the computation of the kernel matrix for all pairs of data points.
  5. It may not always be easy to interpret the results of kernel PCA, as the transformed data may not have a clear interpretation in the original feature space.
  6. Kernel PCA is not suitable for datasets with many missing values or outliers, as it assumes a complete and consistent dataset.


Previous Article
Next Article

Similar Reads

Implementing PCA in Python with scikit-learn
In this article, we will learn about PCA (Principal Component Analysis) in Python with scikit-learn. Let's start our learning step by step. WHY PCA? When there are many input attributes, it is difficult to visualize the data. There is a very famous term ‘Curse of dimensionality in the machine learning domain.Basically, it refers to the fact that a
4 min read
ML | Face Recognition Using PCA Implementation
Face Recognition is one of the most popular and controversial tasks of computer vision. One of the most important milestones is achieved using This approach was first developed by Sirovich and Kirby in 1987 and first used by Turk and Alex Pentland in face classification in 1991. It is easy to implement and thus used in many early face recognition a
6 min read
ML | Face Recognition Using Eigenfaces (PCA Algorithm)
In 1991, Turk and Pentland suggested an approach to face recognition that uses dimensionality reduction and linear algebra concepts to recognize faces. This approach is computationally less expensive and easy to implement and thus used in various applications at that time such as handwritten recognition, lip-reading, medical image analysis, etc. PC
4 min read
Difference between PCA VS t-SNE
Principal Component analysis (PCA): PCA is an unsupervised linear dimensionality reduction and data visualization technique for very high dimensional data. As having high dimensional data is very hard to gain insights from adding to that, it is very computationally intensive. The main idea behind this technique is to reduce the dimensionality of da
3 min read
Mathematical Approach to PCA
The main guiding principle for Principal Component Analysis is FEATURE EXTRACTION i.e. "Features of a data set should be less as well as the similarity between each other is very less." In PCA, a new set of features are extracted from the original features which are quite dissimilar in nature. So, an n-dimensional feature space gets transformed int
8 min read
Reduce Data Dimensionality using PCA - Python
IntroductionThe advancements in Data Science and Machine Learning have made it possible for us to solve several complex regression and classification problems. However, the performance of all these ML models depends on the data fed to them. Thus, it is imperative that we provide our ML models with an optimal dataset. Now, one might think that the m
7 min read
How is Autoencoder different from PCA
In this article, we are going to see how is Autoencoder different from Principal Component Analysis (PCA). Role of Dimensionality Reduction in MLWe often come into the curse of dimensionality issues in machine learning projects, when the amount of data records is not a significant component of the number of features. This often causes issues since
3 min read
KMeans Clustering and PCA on Wine Dataset
K-Means Clustering: K Means Clustering is an unsupervised learning algorithm that tries to cluster data based on their similarity. Unsupervised learning means that there is no outcome to be predicted, and the algorithm just tries to find patterns in the data. In k means clustering, we specify the number of clusters we want the data to be grouped in
6 min read
Comparison of LDA and PCA 2D projection of Iris dataset in Scikit Learn
LDA and PCA both are dimensionality reduction techniques in which we try to reduce the dimensionality of the dataset without losing much information and preserving the pattern present in the dataset. In this article, we will use the iris dataset along with scikit learn pre-implemented functions to perform LDA and PCA with a single line of code. Con
3 min read
Principal Component Analysis(PCA)
As the number of features or dimensions in a dataset increases, the amount of data required to obtain a statistically significant result increases exponentially. This can lead to issues such as overfitting, increased computation time, and reduced accuracy of machine learning models this is known as the curse of dimensionality problems that arise wh
13 min read