update book

This commit is contained in:
Morten Hjorth-Jensen
2022-08-23 11:19:05 +02:00
parent 602b1c6ee0
commit 398fba5b97
233 changed files with 5964 additions and 6430 deletions
+230 -133
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,7 +31,7 @@
<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>
@@ -41,22 +41,24 @@
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.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 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 async="async" src="https://unpkg.com/thebe@0.5.1/lib/index.js"></script>
<script>
const thebe_selector = ".thebe"
const thebe_selector_input = "pre"
const thebe_selector_output = ".output"
</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 defer="defer" 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>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="12. Neural networks" href="chapter9.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">
@@ -91,11 +93,11 @@
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="intro.html">
Applied Data Analysis and Machine Learning, FYS-STK3155/4155 at the University of Oslo, Norway
Applied Data Analysis and Machine Learning
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
About the course
</span>
@@ -117,7 +119,7 @@
</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>
@@ -134,7 +136,7 @@
</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>
@@ -171,7 +173,7 @@
</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>
@@ -188,7 +190,7 @@
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Dimensionality Reduction
</span>
@@ -199,8 +201,13 @@
11. Basic ideas of the Principal Component Analysis (PCA)
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="clustering.html">
12. Clustering and Unsupervised Learning
</a>
</li>
</ul>
<p class="caption" role="heading">
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Deep Learning Methods
</span>
@@ -208,17 +215,27 @@
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="chapter9.html">
12. Neural networks
13. Neural networks
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapter10.html">
13. Building a Feed Forward Neural Network
14. Building a Feed Forward Neural Network
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapter11.html">
14. Solving Differential Equations with Deep Learning
15. Solving Differential Equations with Deep Learning
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapter12.html">
16. Convolutional Neural Networks
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="chapter13.html">
17. Recurrent neural networks: Overarching view
</a>
</li>
</ul>
@@ -267,7 +284,7 @@
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>
@@ -285,7 +302,7 @@
</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
@@ -374,7 +391,99 @@
</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">
@@ -530,10 +639,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.0358909447132981
4.176880142835407
[[ 1.09297039 3.38901478]
[ 3.38901478 11.48089797]]
<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]]
</pre></div>
</div>
</div>
@@ -573,10 +682,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.10044225464078282
2.1586300629904382
[[1. 0.72373129]
[0.72373129 1. ]]
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>0.08328216846752691
2.094472507965532
[[1. 0.67697934]
[0.67697934 1. ]]
</pre></div>
</div>
</div>
@@ -605,30 +714,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>[[-0.50416731 -0.8962476 ]
[-1.00198806 -2.48687342]
[-0.14988578 -0.24485843]
[ 1.33327369 3.92754397]
[ 0.47531107 0.66818635]
[-0.03278964 -1.08041015]
[-0.32992274 1.59591979]
[-1.06160438 -3.85324115]
[ 0.4644244 1.36689784]
[ 0.80734875 1.0030828 ]]
<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]]
0 1
0 -0.504167 -0.896248
1 -1.001988 -2.486873
2 -0.149886 -0.244858
3 1.333274 3.927544
4 0.475311 0.668186
5 -0.032790 -1.080410
6 -0.329923 1.595920
7 -1.061604 -3.853241
8 0.464424 1.366898
9 0.807349 1.003083
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.878297
1 0.878297 1.000000
0 1.000000 0.930583
1 0.930583 1.000000
</pre></div>
</div>
</div>
@@ -685,37 +794,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.068937 0.073016 0.069091 0.070612 0.071705 0.061898 0.062451
2 0.0 0.073016 0.079839 0.075573 0.078253 0.080256 0.068659 0.069754
3 0.0 0.069091 0.075573 0.074488 0.077022 0.078911 0.069912 0.070911
4 0.0 0.070612 0.078253 0.077022 0.080106 0.082451 0.072620 0.073907
5 0.0 0.071705 0.080256 0.078911 0.082451 0.085185 0.074666 0.076204
6 0.0 0.061898 0.068659 0.069912 0.072620 0.074666 0.067714 0.068809
7 0.0 0.062451 0.069754 0.070911 0.073907 0.076204 0.068809 0.070071
8 0.0 0.062884 0.070635 0.071698 0.074945 0.077467 0.069672 0.071086
9 0.0 0.063250 0.071387 0.072348 0.075820 0.078548 0.070379 0.071935
10 0.0 0.054585 0.060841 0.063657 0.066194 0.068120 0.063078 0.064108
11 0.0 0.054785 0.061321 0.064052 0.066753 0.068831 0.063513 0.064648
12 0.0 0.054963 0.061745 0.064384 0.067236 0.069455 0.063872 0.065106
13 0.0 0.055137 0.062142 0.064681 0.067675 0.070028 0.064184 0.065514
14 0.0 0.055320 0.062534 0.064964 0.068094 0.070576 0.064469 0.065892
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
8 9 10 11 12 13 14
0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
1 0.062884 0.063250 0.054585 0.054785 0.054963 0.055137 0.055320
2 0.070635 0.071387 0.060841 0.061321 0.061745 0.062142 0.062534
3 0.071698 0.072348 0.063657 0.064052 0.064384 0.064681 0.064964
4 0.074945 0.075820 0.066194 0.066753 0.067236 0.067675 0.068094
5 0.077467 0.078548 0.068120 0.068831 0.069455 0.070028 0.070576
6 0.069672 0.070379 0.063078 0.063513 0.063872 0.064184 0.064469
7 0.071086 0.071935 0.064108 0.064648 0.065106 0.065514 0.065892
8 0.072242 0.073225 0.064918 0.065557 0.066110 0.066609 0.067079
9 0.073225 0.074336 0.065575 0.066309 0.066954 0.067544 0.068103
10 0.064918 0.065575 0.059783 0.060183 0.060507 0.060781 0.061025
11 0.065557 0.066309 0.060183 0.060655 0.061048 0.061390 0.061701
12 0.066110 0.066954 0.060507 0.061048 0.061509 0.061917 0.062294
13 0.066609 0.067544 0.060781 0.061390 0.061917 0.062391 0.062834
14 0.067079 0.068103 0.061025 0.061701 0.062294 0.062834 0.063343
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
</pre></div>
</div>
</div>
@@ -904,10 +1013,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 3.947903 1.993972
1 1.993972 1.993535
[[3.94790323 1.99397245]
[1.99397245 1.99353454]]
0 4.038506 2.027865
1 2.027865 2.037559
[[4.03850557 2.02786488]
[2.02786488 2.03755944]]
</pre></div>
</div>
</div>
@@ -934,8 +1043,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
[[3.94790323 1.99397245]
[1.99397245 1.99353454]]
[[4.03850557 2.02786488]
[2.02786488 2.03755944]]
</pre></div>
</div>
<img alt="_images/chapter8_65_1.png" src="_images/chapter8_65_1.png" />
@@ -995,16 +1104,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.191262820314401
0.7501749450963715
5.299267190588216
0.7767978193240488
First eigenvector
[0.84854738 0.52911941]
[0.84924834 0.52799362]
Second eigenvector
[-0.52911941 0.84854738]
[-0.52799362 0.84924834]
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Eigenvector of largest eigenvalue
[-0.84854738 -0.52911941]
[0.84924834 0.52799362]
</pre></div>
</div>
</div>
@@ -1349,7 +1458,7 @@ Train set accuracy scaled data: 0.99
Train set accuracy scaled and PCA data: 0.96
</pre></div>
</div>
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/Users/MortenImac/anaconda3/lib/python3.8/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/Users/mhjensen/miniforge3/envs/myenv/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:814: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
Increase the number of iterations (max_iter) or scale the data as shown in:
@@ -1461,54 +1570,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="chapter9.html" title="next page">
<div class="prevnext-info">
<p class="prevnext-label">next</p>
<p class="prevnext-title"><span class="section-number">12. </span>Neural networks</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>