This commit is contained in:
Morten Hjorth-Jensen
2022-10-04 17:50:07 +02:00
parent ca80391647
commit e48ba29bba
225 changed files with 6376 additions and 4975 deletions
+218 -132
View File
@@ -7,8 +7,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>11. Basic ideas of the Principal Component Analysis (PCA) &#8212; Applied Data Analysis and Machine Learning</title>
<link href="_static/css/theme.css" rel="stylesheet" />
<link href="_static/css/index.c5995385ac14fb8791e8eb36b4908be2.css" rel="stylesheet" />
<link href="_static/css/theme.css" rel="stylesheet">
<link href="_static/css/index.ff1ffe594081f20da1ef19478df9384b.css" rel="stylesheet">
<link rel="stylesheet"
@@ -31,31 +31,37 @@
<link rel="stylesheet" type="text/css" href="_static/panels-main.c949a650a448cc0ae9fd3441c0e17fb0.css" />
<link rel="stylesheet" type="text/css" href="_static/panels-variables.06eb56fa6e07937060861dad626602ad.css" />
<link rel="preload" as="script" href="_static/js/index.1c5a1a01449ed65a7b51.js">
<link rel="preload" as="script" href="_static/js/index.be7d3bbb2ef33a8344ce.js">
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/togglebutton.js"></script>
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.js"></script>
<script>let toggleHintShow = 'Click to show';</script>
<script>let toggleHintHide = 'Click to hide';</script>
<script>let toggleOpenOnPrint = 'true';</script>
<script src="_static/togglebutton.js"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown, .tag_hide_input div.cell_input, .tag_hide-input div.cell_input, .tag_hide_output div.cell_output, .tag_hide-output div.cell_output, .tag_hide_cell.cell, .tag_hide-cell.cell';</script>
<script src="_static/sphinx-book-theme.12a9622fbb08dcb3a2a40b2c02b83a57.js"></script>
<script src="_static/sphinx-book-theme.d59cb220de22ca1c485ebbdc042f0030.js"></script>
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"
const thebe_selector = ".thebe,.cell"
const thebe_selector_input = "pre"
const thebe_selector_output = ".output, .cell_output"
</script>
<script async="async" src="_static/sphinx-thebe.js"></script>
<script async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>window.MathJax = {"options": {"processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="12. Clustering and Unsupervised Learning" href="clustering.html" />
<link rel="prev" title="10. Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods" href="chapter7.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="en" />
<meta name="docsearch:language" content="None">
<!-- Google Analytics -->
</head>
<body data-spy="scroll" data-target="#bd-toc-nav" data-offset="80">
@@ -94,7 +100,7 @@ const thebe_selector_output = ".output, .cell_output"
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
About the course
</span>
@@ -116,7 +122,7 @@ const thebe_selector_output = ".output, .cell_output"
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Review of Statistics with Resampling Techniques and Linear Algebra
</span>
@@ -133,7 +139,7 @@ const thebe_selector_output = ".output, .cell_output"
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
From Regression to Support Vector Machines
</span>
@@ -170,7 +176,7 @@ const thebe_selector_output = ".output, .cell_output"
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Decision Trees, Ensemble Methods and Boosting
</span>
@@ -187,7 +193,7 @@ const thebe_selector_output = ".output, .cell_output"
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Dimensionality Reduction
</span>
@@ -204,7 +210,7 @@ const thebe_selector_output = ".output, .cell_output"
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Deep Learning Methods
</span>
@@ -281,7 +287,7 @@ const thebe_selector_output = ".output, .cell_output"
data-placement="left">.ipynb</button></a>
<!-- Download PDF via print -->
<button type="button" id="download-print" class="btn btn-secondary topbarbtn" title="Print to PDF"
onClick="window.print()" data-toggle="tooltip" data-placement="left">.pdf</button>
onclick="printPdf(this)" data-toggle="tooltip" data-placement="left">.pdf</button>
</div>
</div>
@@ -299,7 +305,7 @@ const thebe_selector_output = ".output, .cell_output"
</div>
<!-- Table of contents -->
<div class="d-none d-md-block col-md-2 bd-toc show">
<div class="d-none d-md-block col-md-2 bd-toc show noprint">
<div class="tocsection onthispage pt-5 pb-3">
<i class="fas fa-list"></i> Contents
@@ -388,7 +394,99 @@ const thebe_selector_output = ".output, .cell_output"
</div>
<div id="main-content" class="row">
<div class="col-12 col-md-9 pl-md-3 pr-md-0">
<!-- Table of contents that is only displayed when printing the page -->
<div id="jb-print-docs-body" class="onlyprint">
<h1>Basic ideas of the Principal Component Analysis (PCA)</h1>
<!-- Table of contents -->
<div id="print-main-content">
<div id="jb-print-toc">
<div>
<h2> Contents </h2>
</div>
<nav aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#introducing-the-covariance-and-correlation-functions">
11.1. Introducing the Covariance and Correlation functions
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#correlation-matrix">
11.2. Correlation Matrix
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#towards-the-pca-theorem">
11.3. Towards the PCA theorem
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#the-algorithm-before-theorem">
11.3.1. The Algorithm before theorem
</a>
</li>
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#writing-our-own-pca-code">
11.3.2. Writing our own PCA code
</a>
</li>
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#diagonalize-the-sample-covariance-matrix-to-obtain-the-principal-components">
11.3.3. Diagonalize the sample covariance matrix to obtain the principal components
</a>
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#classical-pca-theorem">
11.4. Classical PCA Theorem
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#geometric-interpretation-and-link-with-singular-value-decomposition">
11.5. Geometric Interpretation and link with Singular Value Decomposition
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#pca-and-scikit-learn">
11.6. PCA and scikit-learn
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#back-to-the-cancer-data">
11.7. Back to the Cancer Data
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#incremental-pca">
11.7.1. Incremental PCA
</a>
</li>
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#randomized-pca">
11.7.2. Randomized PCA
</a>
</li>
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#kernel-pca">
11.7.3. Kernel PCA
</a>
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#other-techniques">
11.8. Other techniques
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
<div>
<div class="tex2jax_ignore mathjax_ignore section" id="basic-ideas-of-the-principal-component-analysis-pca">
@@ -544,10 +642,10 @@ covariance matrix through the <strong>np.linalg.eig()</strong> function.</p>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>0.03382304823545749
4.021250026482402
[[0.9252772 2.69061276]
[2.69061276 8.90540529]]
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>0.046785461905835435
4.240670854503034
[[0.94986593 2.88137798]
[2.88137798 9.93586895]]
</pre></div>
</div>
</div>
@@ -587,10 +685,10 @@ a more brute force way. Here we scale the mean values for each column of the des
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>0.08328216846752691
2.094472507965532
[[1. 0.67697934]
[0.67697934 1. ]]
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>0.08271198519070039
1.7306310662842432
[[1. 0.58084359]
[0.58084359 1. ]]
</pre></div>
</div>
</div>
@@ -619,30 +717,30 @@ this matrix we easily see that it is a positive definite matrix.</p>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[[ 2.38295101 5.51289697]
[ 0.51803019 2.61399851]
[-1.09849763 -1.02255619]
[-0.54016188 -0.53794784]
[ 0.28634473 1.32721178]
[-1.66619972 -6.88017651]
[ 1.52811546 4.29512284]
[ 0.427017 2.43493232]
[-0.80842254 -4.37964744]
[-1.02917662 -3.36383443]]
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[[-0.50488131 -2.2493023 ]
[-0.26115367 -1.92631966]
[-1.43556723 -2.99992698]
[ 0.64528459 2.57643113]
[-0.55273102 -2.09964817]
[ 0.31681097 1.26466619]
[-0.04673082 -0.56607416]
[ 1.53394148 5.38629412]
[ 0.15092012 -0.22014758]
[ 0.15410688 0.83402742]]
0 1
0 2.382951 5.512897
1 0.518030 2.613999
2 -1.098498 -1.022556
3 -0.540162 -0.537948
4 0.286345 1.327212
5 -1.666200 -6.880177
6 1.528115 4.295123
7 0.427017 2.434932
8 -0.808423 -4.379647
9 -1.029177 -3.363834
0 1
0 1.000000 0.930583
1 0.930583 1.000000
0 -0.504881 -2.249302
1 -0.261154 -1.926320
2 -1.435567 -2.999927
3 0.645285 2.576431
4 -0.552731 -2.099648
5 0.316811 1.264666
6 -0.046731 -0.566074
7 1.533941 5.386294
8 0.150920 -0.220148
9 0.154107 0.834027
0 1
0 1.00000 0.95302
1 0.95302 1.00000
</pre></div>
</div>
</div>
@@ -699,37 +797,37 @@ this matrix we easily see that it is a positive definite matrix.</p>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 0 1 2 3 4 5 6 7 \
0 0.0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
1 0.0 0.086074 0.080593 0.088526 0.083276 0.078269 0.081046 0.076604
2 0.0 0.080593 0.076130 0.082896 0.078254 0.073810 0.076216 0.072216
3 0.0 0.088526 0.082896 0.096472 0.091051 0.085879 0.091696 0.086956
4 0.0 0.083276 0.078254 0.091051 0.086112 0.081388 0.086890 0.082536
5 0.0 0.078269 0.073810 0.085879 0.081388 0.077083 0.082295 0.078299
6 0.0 0.081046 0.076216 0.091696 0.086890 0.082295 0.089501 0.085163
7 0.0 0.076604 0.072216 0.086956 0.082536 0.078299 0.085163 0.081144
8 0.0 0.072442 0.068461 0.082503 0.078436 0.074530 0.081072 0.077349
9 0.0 0.068538 0.064931 0.078314 0.074573 0.070972 0.077211 0.073761
10 0.0 0.073062 0.069085 0.084862 0.080738 0.076780 0.084484 0.080647
11 0.0 0.069275 0.065641 0.080706 0.076895 0.073230 0.080582 0.077013
12 0.0 0.065730 0.062411 0.076804 0.073280 0.069884 0.076905 0.073583
13 0.0 0.062409 0.059378 0.073136 0.069877 0.066729 0.073436 0.070343
14 0.0 0.059294 0.056528 0.069685 0.066670 0.063752 0.070163 0.067282
1 0.0 0.088104 0.081216 0.088760 0.086997 0.085010 0.080343 0.079306
2 0.0 0.081216 0.075612 0.080764 0.079618 0.078300 0.072530 0.071897
3 0.0 0.088760 0.080764 0.094925 0.092249 0.089310 0.089233 0.087615
4 0.0 0.086997 0.079618 0.092249 0.089982 0.087470 0.086257 0.084927
5 0.0 0.085010 0.078300 0.089310 0.087470 0.085410 0.083021 0.081989
6 0.0 0.080343 0.072530 0.089233 0.086257 0.083021 0.086109 0.084269
7 0.0 0.079306 0.071897 0.087615 0.084927 0.081989 0.084269 0.082642
8 0.0 0.078323 0.071329 0.086021 0.083629 0.080998 0.082431 0.081022
9 0.0 0.077372 0.070810 0.084426 0.082339 0.080026 0.080571 0.079383
10 0.0 0.071946 0.064637 0.081990 0.079001 0.075770 0.080642 0.078768
11 0.0 0.071044 0.064041 0.080699 0.077930 0.074922 0.079218 0.077511
12 0.0 0.070222 0.063524 0.079476 0.076927 0.074144 0.077846 0.076306
13 0.0 0.069475 0.063084 0.078314 0.075987 0.073433 0.076518 0.075145
14 0.0 0.068799 0.062719 0.077203 0.075102 0.072783 0.075222 0.074019
8 9 10 11 12 13 14
0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
1 0.072442 0.068538 0.073062 0.069275 0.065730 0.062409 0.059294
2 0.068461 0.064931 0.069085 0.065641 0.062411 0.059378 0.056528
3 0.082503 0.078314 0.084862 0.080706 0.076804 0.073136 0.069685
4 0.078436 0.074573 0.080738 0.076895 0.073280 0.069877 0.066670
5 0.074530 0.070972 0.076780 0.073230 0.069884 0.066729 0.063752
6 0.081072 0.077211 0.084484 0.080582 0.076905 0.073436 0.070163
7 0.077349 0.073761 0.080647 0.077013 0.073583 0.070343 0.067282
8 0.073827 0.070492 0.077015 0.073629 0.070429 0.067402 0.064538
9 0.070492 0.067392 0.073575 0.070419 0.067432 0.064604 0.061923
10 0.077015 0.073575 0.080984 0.077452 0.074111 0.070950 0.067956
11 0.073629 0.070419 0.077452 0.074149 0.071022 0.068059 0.065249
12 0.070429 0.067432 0.074111 0.071022 0.068093 0.065314 0.062676
13 0.067402 0.064604 0.070950 0.068059 0.065314 0.062706 0.060228
14 0.064538 0.061923 0.067956 0.065249 0.062676 0.060228 0.057899
1 0.078323 0.077372 0.071946 0.071044 0.070222 0.069475 0.068799
2 0.071329 0.070810 0.064637 0.064041 0.063524 0.063084 0.062719
3 0.086021 0.084426 0.081990 0.080699 0.079476 0.078314 0.077203
4 0.083629 0.082339 0.079001 0.077930 0.076927 0.075987 0.075102
5 0.080998 0.080026 0.075770 0.074922 0.074144 0.073433 0.072783
6 0.082431 0.080571 0.080642 0.079218 0.077846 0.076518 0.075222
7 0.081022 0.079383 0.078768 0.077511 0.076306 0.075145 0.074019
8 0.079622 0.078211 0.076886 0.075797 0.074760 0.073768 0.072813
9 0.078211 0.077033 0.074969 0.074050 0.073183 0.072364 0.071585
10 0.076886 0.074969 0.076632 0.075201 0.073811 0.072452 0.071115
11 0.075797 0.074050 0.075201 0.073904 0.072647 0.071421 0.070216
12 0.074760 0.073183 0.073811 0.072647 0.071523 0.070428 0.069355
13 0.073768 0.072364 0.072452 0.071421 0.070428 0.069465 0.068525
14 0.072813 0.071585 0.071115 0.070216 0.069355 0.068525 0.067719
</pre></div>
</div>
</div>
@@ -918,10 +1016,10 @@ We can write our own code or simply use either the functionaly of <strong>numpy<
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> 0 1
0 4.038506 2.027865
1 2.027865 2.037559
[[4.03850557 2.02786488]
[2.02786488 2.03755944]]
0 3.967536 1.983164
1 1.983164 2.000755
[[3.9675364 1.98316352]
[1.98316352 2.00075534]]
</pre></div>
</div>
</div>
@@ -948,8 +1046,8 @@ Our own code here is not very elegant and asks for obvious improvements. It is t
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Centered covariance using own code
[[4.03850557 2.02786488]
[2.02786488 2.03755944]]
[[3.9675364 1.98316352]
[1.98316352 2.00075534]]
</pre></div>
</div>
<img alt="_images/chapter8_65_1.png" src="_images/chapter8_65_1.png" />
@@ -1009,16 +1107,16 @@ questions.</p>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Eigenvalues of Covariance matrix
5.299267190588216
0.7767978193240488
5.197738983259782
0.7705527590466072
First eigenvector
[0.84924834 0.52799362]
[0.84977962 0.52713812]
Second eigenvector
[-0.52799362 0.84924834]
[-0.52713812 0.84977962]
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Eigenvector of largest eigenvalue
[0.84924834 0.52799362]
[-0.84977962 -0.52713812]
</pre></div>
</div>
</div>
@@ -1242,16 +1340,16 @@ training set, then extracts the first two principal components. First we center
7 0.0 0.0 0.0 0.0 0.0
8 0.0 0.0 0.0 0.0 0.0
9 0.0 0.0 0.0 0.0 0.0
[[-1.5378811 -0.94639099]
[ 0.86145244 0.89288636]
[-0.00445655 0.81633628]
[ 0.07145103 -1.00433417]
[ 2.03707133 -0.48476997]
[ 0.72174172 -1.4557763 ]
[-0.55854694 1.60673226]
[ 1.6999536 0.43766686]
[-1.10405456 0.31718909]
[-2.18673098 -0.17953942]]
[[-1.5378811 0.94639099]
[ 0.86145244 -0.89288636]
[-0.00445655 -0.81633628]
[ 0.07145103 1.00433417]
[ 2.03707133 0.48476997]
[ 0.72174172 1.4557763 ]
[-0.55854694 -1.60673226]
[ 1.6999536 -0.43766686]
[-1.10405456 -0.31718909]
[-2.18673098 0.17953942]]
</pre></div>
</div>
</div>
@@ -1475,54 +1573,42 @@ For example, the following code uses Scikit-Learns KernelPCA class to perform
</div>
<div class='prev-next-bottom'>
<div id="prev">
<a class="left-prev" href="chapter7.html" title="previous page">
<i class="prevnext-label fas fa-angle-left"></i>
<div class="prevnext-info">
<p class="prevnext-label">previous</p>
<p class="prevnext-title"><span class="section-number">10. </span>Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods</p>
</div>
</a>
<!-- Previous / next buttons -->
<div class='prev-next-area'>
<a class='left-prev' id="prev-link" href="chapter7.html" title="previous page">
<i class="fas fa-angle-left"></i>
<div class="prev-next-info">
<p class="prev-next-subtitle">previous</p>
<p class="prev-next-title"><span class="section-number">10. </span>Ensemble Methods: From a Single Tree to Many Trees and Extreme Boosting, Meet the Jungle of Methods</p>
</div>
</a>
<a class='right-next' id="next-link" href="clustering.html" title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title"><span class="section-number">12. </span>Clustering and Unsupervised Learning</p>
</div>
<div id="next">
<a class="right-next" href="clustering.html" title="next page">
<div class="prevnext-info">
<p class="prevnext-label">next</p>
<p class="prevnext-title"><span class="section-number">12. </span>Clustering and Unsupervised Learning</p>
</div>
<i class="prevnext-label fas fa-angle-right"></i>
</a>
</div>
<i class="fas fa-angle-right"></i>
</a>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<p>
By Morten Hjorth-Jensen<br/>
&copy; Copyright 2021.<br/>
</p>
</div>
</footer>
<p>
By Morten Hjorth-Jensen<br/>
&copy; Copyright 2021.<br/>
</p>
</footer>
</main>
</div>
</div>
<script src="_static/js/index.1c5a1a01449ed65a7b51.js"></script>
<script src="_static/js/index.be7d3bbb2ef33a8344ce.js"></script>
</body>
</html>