{"id":68,"date":"2025-12-06T13:29:41","date_gmt":"2025-12-06T05:29:41","guid":{"rendered":"https:\/\/connectword.dpdns.org\/?p=68"},"modified":"2025-12-06T13:29:41","modified_gmt":"2025-12-06T05:29:41","slug":"kernel-principal-component-analysis-pca-explained-with-an-example","status":"publish","type":"post","link":"https:\/\/connectword.dpdns.org\/?p=68&lang=en","title":{"rendered":"Kernel Principal Component Analysis (PCA): Explained with an Example"},"content":{"rendered":"<p>Dimensionality reduction techniques like PCA work wonderfully when datasets are linearly separable\u2014but they break down the moment nonlinear patterns appear. That\u2019s exactly what happens with datasets such as <em>two moons<\/em>: PCA flattens the structure and mixes the classes together.\u00a0<\/p>\n<p>Kernel PCA fixes this limitation by mapping the data into a higher-dimensional feature space where nonlinear patterns become linearly separable. In this article, we\u2019ll walk through how Kernel PCA works and use a simple example to visually compare PCA vs. Kernel PCA, showing how a nonlinear dataset that PCA fails to separate becomes perfectly separable after applying Kernel PCA.<\/p>\n<h1 class=\"wp-block-heading\"><strong>What is PCA and how is it different from Kernel PCA?<\/strong><\/h1>\n<p>Principal Component Analysis (PCA) is a linear dimensionality-reduction technique that identifies the directions (principal components) along which the data varies the most. It works by computing orthogonal linear combinations of the original features and projecting the dataset onto the directions of maximum variance.\u00a0<\/p>\n<p>These components are uncorrelated and ordered so that the first few capture most of the information in the data. PCA is powerful, but it comes with one important limitation: it can only uncover linear relationships in the data. When applied to nonlinear datasets\u2014like the \u201ctwo moons\u201d example\u2014it often fails to separate the underlying structure.<\/p>\n<p>Kernel PCA extends PCA to handle nonlinear relationships. Instead of directly applying PCA in the original feature space, Kernel PCA first uses a kernel function (such as RBF, polynomial, or sigmoid) to implicitly project the data into a higher-dimensional feature space where the nonlinear structure becomes linearly separable.\u00a0<\/p>\n<p>PCA is then performed in this transformed space using a kernel matrix, without explicitly computing the higher-dimensional projection. This \u201ckernel trick\u201d allows Kernel PCA to capture complex patterns that standard PCA cannot.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"968\" height=\"436\" data-attachment-id=\"76754\" data-permalink=\"https:\/\/www.marktechpost.com\/2025\/12\/05\/kernel-principal-component-analysis-pca-explained-with-an-example\/image-247\/\" data-orig-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-7.png\" data-orig-size=\"968,436\" data-comments-opened=\"1\" data-image-meta='{\"aperture\":\"0\",\"credit\":\"\",\"camera\":\"\",\"caption\":\"\",\"created_timestamp\":\"0\",\"copyright\":\"\",\"focal_length\":\"0\",\"iso\":\"0\",\"shutter_speed\":\"0\",\"title\":\"\",\"orientation\":\"0\"}' data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-7-300x135.png\" data-large-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-7.png\" src=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-7.png\" alt=\"\" class=\"wp-image-76754\" \/><\/figure>\n<\/div>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"975\" height=\"433\" data-attachment-id=\"76755\" data-permalink=\"https:\/\/www.marktechpost.com\/2025\/12\/05\/kernel-principal-component-analysis-pca-explained-with-an-example\/image-248\/\" data-orig-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-8.png\" data-orig-size=\"975,433\" data-comments-opened=\"1\" data-image-meta='{\"aperture\":\"0\",\"credit\":\"\",\"camera\":\"\",\"caption\":\"\",\"created_timestamp\":\"0\",\"copyright\":\"\",\"focal_length\":\"0\",\"iso\":\"0\",\"shutter_speed\":\"0\",\"title\":\"\",\"orientation\":\"0\"}' data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-8-300x133.png\" data-large-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-8.png\" src=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-8.png\" alt=\"\" class=\"wp-image-76755\" \/><\/figure>\n<\/div>\n<p>We will now create a dataset that is nonlinear and then apply PCA to the dataset.<\/p>\n<h1 class=\"wp-block-heading\"><strong>Code Implementation<\/strong><\/h1>\n<h2 class=\"wp-block-heading\"><strong>Generating the dataset<\/strong><\/h2>\n<p>We generate a nonlinear \u201ctwo moons\u201d dataset using make_moons, which is ideal for demonstrating why PCA fails and Kernel PCA succeeds.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">import matplotlib.pyplot as plt\nfrom sklearn.datasets import make_moons\n\nX, y = make_moons(n_samples=1000, noise=0.02, random_state=123)\n\nplt.scatter(X[:, 0], X[:, 1], c=y)\nplt.show()<\/code><\/pre>\n<\/div>\n<\/div>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"559\" height=\"413\" data-attachment-id=\"76756\" data-permalink=\"https:\/\/www.marktechpost.com\/2025\/12\/05\/kernel-principal-component-analysis-pca-explained-with-an-example\/image-249\/\" data-orig-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-9.png\" data-orig-size=\"559,413\" data-comments-opened=\"1\" data-image-meta='{\"aperture\":\"0\",\"credit\":\"\",\"camera\":\"\",\"caption\":\"\",\"created_timestamp\":\"0\",\"copyright\":\"\",\"focal_length\":\"0\",\"iso\":\"0\",\"shutter_speed\":\"0\",\"title\":\"\",\"orientation\":\"0\"}' data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-9-300x222.png\" data-large-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-9.png\" src=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-9.png\" alt=\"\" class=\"wp-image-76756\" \/><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\"><strong>Applying PCA on the dataset<\/strong><\/h2>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">from sklearn.decomposition import PCA\npca = PCA(n_components=2)\nX_pca = pca.fit_transform(X)\n\nplt.title(\"PCA\")\nplt.scatter(X_pca[:, 0], X_pca[:, 1], c=y)\nplt.xlabel(\"Component 1\")\nplt.ylabel(\"Component 2\")\nplt.show()\n<\/code><\/pre>\n<\/div>\n<\/div>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"578\" height=\"455\" data-attachment-id=\"76758\" data-permalink=\"https:\/\/www.marktechpost.com\/2025\/12\/05\/kernel-principal-component-analysis-pca-explained-with-an-example\/image-251\/\" data-orig-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-11.png\" data-orig-size=\"578,455\" data-comments-opened=\"1\" data-image-meta='{\"aperture\":\"0\",\"credit\":\"\",\"camera\":\"\",\"caption\":\"\",\"created_timestamp\":\"0\",\"copyright\":\"\",\"focal_length\":\"0\",\"iso\":\"0\",\"shutter_speed\":\"0\",\"title\":\"\",\"orientation\":\"0\"}' data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-11-300x236.png\" data-large-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-11.png\" src=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-11.png\" alt=\"\" class=\"wp-image-76758\" \/><\/figure>\n<\/div>\n<p>The PCA visualization shows that the two moon-shaped clusters remain intertwined even after dimensionality reduction. This happens because PCA is a strictly linear technique\u2014it can only rotate, scale, or flatten the data along straight directions of maximum variance.\u00a0<\/p>\n<p>Since the \u201ctwo moons\u201d dataset has a nonlinear structure, PCA is unable to separate the classes or untangle the curved shapes. As a result, the transformed data still looks almost identical to the original pattern, and the two classes remain overlapped in the projected space.<\/p>\n<h2 class=\"wp-block-heading\"><strong>Applying Kernel PCA on the dataset<\/strong><\/h2>\n<p>We now apply Kernel PCA using an RBF kernel, which maps the nonlinear data into a higher-dimensional space where it becomes linearly separable. In the kernel space the two classes in our dataset are linearly separable. Kernel PCA uses a kernel function to project the dataset into a higher-dimensional space, where it is linearly separable.<\/p>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\" no-line-numbers\"><code class=\" no-wrap language-php\">from sklearn.decomposition import KernelPCA\nkpca = KernelPCA(kernel='rbf', gamma=15)\nX_kpca = kpca.fit_transform(X)\n\nplt.title(\"Kernel PCA\")\nplt.scatter(X_kpca[:, 0], X_kpca[:, 1], c=y)\nplt.show()<\/code><\/pre>\n<\/div>\n<\/div>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"559\" height=\"435\" data-attachment-id=\"76759\" data-permalink=\"https:\/\/www.marktechpost.com\/2025\/12\/05\/kernel-principal-component-analysis-pca-explained-with-an-example\/image-252\/\" data-orig-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-12.png\" data-orig-size=\"559,435\" data-comments-opened=\"1\" data-image-meta='{\"aperture\":\"0\",\"credit\":\"\",\"camera\":\"\",\"caption\":\"\",\"created_timestamp\":\"0\",\"copyright\":\"\",\"focal_length\":\"0\",\"iso\":\"0\",\"shutter_speed\":\"0\",\"title\":\"\",\"orientation\":\"0\"}' data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-12-300x233.png\" data-large-file=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-12.png\" src=\"https:\/\/www.marktechpost.com\/wp-content\/uploads\/2025\/12\/image-12.png\" alt=\"\" class=\"wp-image-76759\" \/><\/figure>\n<\/div>\n<p>The goal of PCA (and dimensionality reduction in general) is not just to compress the data\u2014it\u2019s to reveal the underlying structure in a way that preserves meaningful variation. In nonlinear datasets like the two-moons example, traditional PCA cannot \u201cunfold\u201d the curved shapes because it only applies linear transformations.<\/p>\n<p>Kernel PCA, however, performs a nonlinear mapping before applying PCA, allowing the algorithm to untangle the moons into two clearly separated clusters. This separation is valuable because it makes downstream tasks like visualization, clustering, and even classification far more effective. When the data becomes linearly separable after transformation, simple models\u2014such as linear classifiers\u2014can successfully distinguish between the classes, something that would be impossible in the original or PCA-transformed space.<\/p>\n<h1 class=\"wp-block-heading\"><strong>Challenges involved with Kernel PCA<\/strong><\/h1>\n<p>While Kernel PCA is powerful for handling nonlinear datasets, it comes with several practical challenges. The biggest drawback is computational cost\u2014because it relies on computing pairwise similarities between all data points, the algorithm has <strong>O(n\u00b2) <\/strong>time and memory complexity, making it slow and memory-heavy for large datasets.\u00a0<\/p>\n<p>Another challenge is model selection: choosing the right kernel (RBF, polynomial, etc.) and tuning parameters like gamma can be tricky and often requires experimentation or domain expertise.\u00a0<\/p>\n<p>Kernel PCA can also be harder to interpret, since the transformed components no longer correspond to intuitive directions in the original feature space. Finally, it is sensitive to missing values and outliers, which can distort the kernel matrix and degrade performance.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p>Check out the\u00a0<strong><a href=\"https:\/\/github.com\/Marktechpost\/AI-Tutorial-Codes-Included\/blob\/main\/Data%20Science\/Kernel_PCA.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">FULL CODES here<\/a><\/strong>.\u00a0Feel free to check out our\u00a0<strong><mark><a href=\"https:\/\/github.com\/Marktechpost\/AI-Tutorial-Codes-Included\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub Page for Tutorials, Codes and Notebooks<\/a><\/mark><\/strong>.\u00a0Also,\u00a0feel free to follow us on\u00a0<strong><a href=\"https:\/\/x.com\/intent\/follow?screen_name=marktechpost\" target=\"_blank\" rel=\"noreferrer noopener\"><mark>Twitter<\/mark><\/a><\/strong>\u00a0and don\u2019t forget to join our\u00a0<strong><a href=\"https:\/\/www.reddit.com\/r\/machinelearningnews\/\" target=\"_blank\" rel=\"noreferrer noopener\">100k+ ML SubReddit<\/a><\/strong>\u00a0and Subscribe to\u00a0<strong><a href=\"https:\/\/www.aidevsignals.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">our Newsletter<\/a><\/strong>. Wait! are you on telegram?\u00a0<strong><a href=\"https:\/\/t.me\/machinelearningresearchnews\" target=\"_blank\" rel=\"noreferrer noopener\">now you can join us on telegram as well.<\/a><\/strong><\/p>\n<p>The post <a href=\"https:\/\/www.marktechpost.com\/2025\/12\/05\/kernel-principal-component-analysis-pca-explained-with-an-example\/\">Kernel Principal Component Analysis (PCA): Explained with an Example<\/a> appeared first on <a href=\"https:\/\/www.marktechpost.com\/\">MarkTechPost<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Dimensionality reduction techn&hellip;<\/p>\n","protected":false},"author":1,"featured_media":69,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68"}],"version-history":[{"count":0,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=\/wp\/v2\/media\/69"}],"wp:attachment":[{"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/connectword.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}