updating week 43
This commit is contained in:
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,7 +187,7 @@ MathJax.Hub.Config({
|
||||
|
||||
|
||||
<div class="jumbotron">
|
||||
<center><h1>Week 43: Dimensionality Reduction</h1></center> <!-- document title -->
|
||||
<center><h1>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</h1></center> <!-- document title -->
|
||||
|
||||
<p>
|
||||
<!-- author(s): Morten Hjorth-Jensen -->
|
||||
@@ -199,7 +203,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Sep 16, 2020</h4></center> <!-- date -->
|
||||
<center><h4>Oct 17, 2020</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
@@ -223,7 +227,7 @@ MathJax.Hub.Config({
|
||||
<li><a href="._week43-bs008.html">9</a></li>
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs001.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,86 +184,12 @@ MathJax.Hub.Config({
|
||||
<a name="part0001"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec0" class="anchor">Why should we think of reducing the dimensionality </h2>
|
||||
<ul>
|
||||
<li> Thursday: Wrapping up Recurrent Neural Networks and solving differential equations.</li>
|
||||
<li> Friday: Principal Component Analysis and Dimensionality Reduction</li>
|
||||
</ul>
|
||||
|
||||
<p>
|
||||
In addition to the plot of the features, we study now also the covariance (and the correlation matrix).
|
||||
We use also <b>Pandas</b> to compute the correlation matrix.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
|
||||
cancer <span style="color: #666666">=</span> load_breast_cancer()
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
<span style="color: #408080; font-style: italic"># Making a data frame</span>
|
||||
cancerpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(cancer<span style="color: #666666">.</span>data, columns<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>feature_names)
|
||||
|
||||
fig, axes <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(<span style="color: #666666">15</span>,<span style="color: #666666">2</span>,figsize<span style="color: #666666">=</span>(<span style="color: #666666">10</span>,<span style="color: #666666">20</span>))
|
||||
malignant <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">0</span>]
|
||||
benign <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">1</span>]
|
||||
ax <span style="color: #666666">=</span> axes<span style="color: #666666">.</span>ravel()
|
||||
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">30</span>):
|
||||
_, bins <span style="color: #666666">=</span> np<span style="color: #666666">.</span>histogram(cancer<span style="color: #666666">.</span>data[:,i], bins <span style="color: #666666">=50</span>)
|
||||
ax[i]<span style="color: #666666">.</span>hist(malignant[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
|
||||
ax[i]<span style="color: #666666">.</span>hist(benign[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
|
||||
ax[i]<span style="color: #666666">.</span>set_title(cancer<span style="color: #666666">.</span>feature_names[i])
|
||||
ax[i]<span style="color: #666666">.</span>set_yticks(())
|
||||
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">"Feature magnitude"</span>)
|
||||
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">"Frequency"</span>)
|
||||
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>legend([<span style="color: #BA2121">"Malignant"</span>, <span style="color: #BA2121">"Benign"</span>], loc <span style="color: #666666">=</span><span style="color: #BA2121">"best"</span>)
|
||||
fig<span style="color: #666666">.</span>tight_layout()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">seaborn</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">sns</span>
|
||||
correlation_matrix <span style="color: #666666">=</span> cancerpd<span style="color: #666666">.</span>corr()<span style="color: #666666">.</span>round(<span style="color: #666666">1</span>)
|
||||
<span style="color: #408080; font-style: italic"># use the heatmap function from seaborn to plot the correlation matrix</span>
|
||||
<span style="color: #408080; font-style: italic"># annot = True to print the values inside the square</span>
|
||||
sns<span style="color: #666666">.</span>heatmap(data<span style="color: #666666">=</span>correlation_matrix, annot<span style="color: #666666">=</span><span style="color: #008000">True</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic">#print eigvalues of correlation matrix</span>
|
||||
EigValues, EigVectors <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>eig(correlation_matrix)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(EigValues)
|
||||
</pre></div>
|
||||
<p>
|
||||
In the above example we note two things. In the first plot we display
|
||||
the overlap of benign and malignant tumors as functions of the various
|
||||
features in the Wisconsing breast cancer data set. We see that for
|
||||
some of the features we can distinguish clearly the benign and
|
||||
malignant cases while for other features we cannot. This can point to
|
||||
us which features may be of greater interest when we wish to classify
|
||||
a benign or not benign tumour.
|
||||
|
||||
<p>
|
||||
In the second figure we have computed the so-called correlation
|
||||
matrix, which in our case with thirty features becomes a \( 30\times 30 \)
|
||||
matrix.
|
||||
|
||||
<p>
|
||||
We constructed this matrix using <b>pandas</b> via the statements
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>cancerpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(cancer<span style="color: #666666">.</span>data, columns<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>feature_names)
|
||||
</pre></div>
|
||||
<p>
|
||||
and then
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>correlation_matrix <span style="color: #666666">=</span> cancerpd<span style="color: #666666">.</span>corr()<span style="color: #666666">.</span>round(<span style="color: #666666">1</span>)
|
||||
</pre></div>
|
||||
<p>
|
||||
Diagonalizing this matrix we can in turn say something about which
|
||||
features are of relevance and which are not. But before we proceed we
|
||||
need to define covariance and correlation matrices. This leads us to
|
||||
the classical Principal Component Analysis (PCA) theorem with
|
||||
applications.
|
||||
Reading suggestions for both days: "Aurelien Geron's chapters 8
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -278,7 +208,7 @@ applications.
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs002.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,26 +184,9 @@ MathJax.Hub.Config({
|
||||
<a name="part0002"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec1" class="anchor">Basic ideas of the Principal Component Analysis (PCA) </h2>
|
||||
<h2 id="___sec0" class="anchor">Recurrent Neural Networks </h2>
|
||||
|
||||
<p>
|
||||
The principal component analysis deals with the problem of fitting a
|
||||
low-dimensional affine subspace \( S \) of dimension \( d \) much smaller than
|
||||
the totaldimension \( D \) of the problem at hand (our data
|
||||
set). Mathematically it can be formulated as a statistical problem or
|
||||
a geometric problem. In our discussion of the theorem for the
|
||||
classical PCA, we will stay with a statistical approach. This is also
|
||||
what set the scene historically which for the PCA.
|
||||
|
||||
<p>
|
||||
We have a data set defined by a design/feature matrix \( \boldsymbol{X} \) (see below for its definition)
|
||||
|
||||
<ul>
|
||||
<li> Each data point is determined by \( p \) extrinsic (measurement) variables</li>
|
||||
<li> We may want to ask the following question: Are there fewer intrinsic variables (say \( d < < p \)) that still approximately describe the data?</li>
|
||||
<li> If so, these intrinsic variables may tell us something important and finding these intrinsic variables is what dimension reduction methods do.</li>
|
||||
</ul>
|
||||
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
@@ -217,7 +204,7 @@ We have a data set defined by a design/feature matrix \( \boldsymbol{X} \) (see
|
||||
<li><a href="._week43-bs010.html">11</a></li>
|
||||
<li><a href="._week43-bs011.html">12</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs003.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,63 +184,7 @@ MathJax.Hub.Config({
|
||||
<a name="part0003"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec2" class="anchor">Introducing the Covariance and Correlation functions </h2>
|
||||
|
||||
<p>
|
||||
Before we discuss the PCA theorem, we need to remind ourselves about
|
||||
the definition of the covariance and the correlation function. These are quantities
|
||||
|
||||
<p>
|
||||
Suppose we have defined two vectors
|
||||
\( \hat{x} \) and \( \hat{y} \) with \( n \) elements each. The covariance matrix \( \boldsymbol{C} \) is defined as
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{cov}[\boldsymbol{x},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\
|
||||
\mathrm{cov}[\boldsymbol{y},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{y},\boldsymbol{y}] \\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
|
||||
where for example
|
||||
$$
|
||||
\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] =\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})(y_i- \overline{y}).
|
||||
$$
|
||||
|
||||
With this definition and recalling that the variance is defined as
|
||||
$$
|
||||
\mathrm{var}[\boldsymbol{x}]=\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})^2,
|
||||
$$
|
||||
|
||||
we can rewrite the covariance matrix as
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{var}[\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\
|
||||
\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] & \mathrm{var}[\boldsymbol{y}] \\
|
||||
\end{bmatrix}.
|
||||
$$
|
||||
|
||||
<p>
|
||||
The covariance takes values between zero and infinity and may thus
|
||||
lead to problems with loss of numerical precision for particularly
|
||||
large values. It is common to scale the covariance matrix by
|
||||
introducing instead the correlation matrix defined via the so-called
|
||||
correlation function
|
||||
|
||||
$$
|
||||
\mathrm{corr}[\boldsymbol{x},\boldsymbol{y}]=\frac{\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{\mathrm{var}[\boldsymbol{x}] \mathrm{var}[\boldsymbol{y}]}}.
|
||||
$$
|
||||
|
||||
<p>
|
||||
The correlation function is then given by values \( \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}]
|
||||
\in [-1,1] \). This avoids eventual problems with too large values. We
|
||||
can then define the correlation matrix for the two vectors \( \boldsymbol{x} \)
|
||||
and \( \boldsymbol{y} \) as
|
||||
|
||||
$$
|
||||
\boldsymbol{K}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} 1 & \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] \\
|
||||
\mathrm{corr}[\boldsymbol{y},\boldsymbol{x}] & 1 \\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
|
||||
<p>
|
||||
In the above example this is the function we constructed using <b>pandas</b>.
|
||||
<h2 id="___sec1" class="anchor">Solving ODEs with Deep Learning </h2>
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -257,7 +205,7 @@ In the above example this is the function we constructed using <b>pandas</b>.
|
||||
<li><a href="._week43-bs011.html">12</a></li>
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs004.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,63 +184,86 @@ MathJax.Hub.Config({
|
||||
<a name="part0004"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec3" class="anchor">Correlation Function and Design/Feature Matrix </h2>
|
||||
<h2 id="___sec2" class="anchor">Why should we think of reducing the dimensionality </h2>
|
||||
|
||||
<p>
|
||||
In our derivation of the various regression algorithms like <b>Ordinary Least Squares</b> or <b>Ridge regression</b>
|
||||
we defined the design/feature matrix \( \boldsymbol{X} \) as
|
||||
In addition to the plot of the features, we study now also the covariance (and the correlation matrix).
|
||||
We use also <b>Pandas</b> to compute the correlation matrix.
|
||||
<p>
|
||||
|
||||
$$
|
||||
\boldsymbol{X}=\begin{bmatrix}
|
||||
x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\
|
||||
x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\
|
||||
x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\
|
||||
\dots & \dots & \dots & \dots \dots & \dots \\
|
||||
x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\
|
||||
x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
|
||||
cancer <span style="color: #666666">=</span> load_breast_cancer()
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
<span style="color: #408080; font-style: italic"># Making a data frame</span>
|
||||
cancerpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(cancer<span style="color: #666666">.</span>data, columns<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>feature_names)
|
||||
|
||||
with \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predictors/features \( p \) refering to the column numbers and the
|
||||
entries \( n \) being the row elements.
|
||||
We can rewrite the design/feature matrix in terms of its column vectors as
|
||||
$$
|
||||
\boldsymbol{X}=\begin{bmatrix} \boldsymbol{x}_0 & \boldsymbol{x}_1 & \boldsymbol{x}_2 & \dots & \dots & \boldsymbol{x}_{p-1}\end{bmatrix},
|
||||
$$
|
||||
fig, axes <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(<span style="color: #666666">15</span>,<span style="color: #666666">2</span>,figsize<span style="color: #666666">=</span>(<span style="color: #666666">10</span>,<span style="color: #666666">20</span>))
|
||||
malignant <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">0</span>]
|
||||
benign <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">1</span>]
|
||||
ax <span style="color: #666666">=</span> axes<span style="color: #666666">.</span>ravel()
|
||||
|
||||
with a given vector
|
||||
$$
|
||||
\boldsymbol{x}_i^T = \begin{bmatrix}x_{0,i} & x_{1,i} & x_{2,i}& \dots & \dots x_{n-1,i}\end{bmatrix}.
|
||||
$$
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">30</span>):
|
||||
_, bins <span style="color: #666666">=</span> np<span style="color: #666666">.</span>histogram(cancer<span style="color: #666666">.</span>data[:,i], bins <span style="color: #666666">=50</span>)
|
||||
ax[i]<span style="color: #666666">.</span>hist(malignant[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
|
||||
ax[i]<span style="color: #666666">.</span>hist(benign[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
|
||||
ax[i]<span style="color: #666666">.</span>set_title(cancer<span style="color: #666666">.</span>feature_names[i])
|
||||
ax[i]<span style="color: #666666">.</span>set_yticks(())
|
||||
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">"Feature magnitude"</span>)
|
||||
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">"Frequency"</span>)
|
||||
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>legend([<span style="color: #BA2121">"Malignant"</span>, <span style="color: #BA2121">"Benign"</span>], loc <span style="color: #666666">=</span><span style="color: #BA2121">"best"</span>)
|
||||
fig<span style="color: #666666">.</span>tight_layout()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">seaborn</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">sns</span>
|
||||
correlation_matrix <span style="color: #666666">=</span> cancerpd<span style="color: #666666">.</span>corr()<span style="color: #666666">.</span>round(<span style="color: #666666">1</span>)
|
||||
<span style="color: #408080; font-style: italic"># use the heatmap function from seaborn to plot the correlation matrix</span>
|
||||
<span style="color: #408080; font-style: italic"># annot = True to print the values inside the square</span>
|
||||
sns<span style="color: #666666">.</span>heatmap(data<span style="color: #666666">=</span>correlation_matrix, annot<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic">#print eigvalues of correlation matrix</span>
|
||||
EigValues, EigVectors <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>eig(correlation_matrix)
|
||||
<span style="color: #008000">print</span>(EigValues)
|
||||
</pre></div>
|
||||
<p>
|
||||
In the above example we note two things. In the first plot we display
|
||||
the overlap of benign and malignant tumors as functions of the various
|
||||
features in the Wisconsing breast cancer data set. We see that for
|
||||
some of the features we can distinguish clearly the benign and
|
||||
malignant cases while for other features we cannot. This can point to
|
||||
us which features may be of greater interest when we wish to classify
|
||||
a benign or not benign tumour.
|
||||
|
||||
<p>
|
||||
With these definitions, we can now rewrite our \( 2\times 2 \)
|
||||
correaltion/covariance matrix in terms of a moe general design/feature
|
||||
matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \). This leads to a \( p\times p \)
|
||||
covariance matrix for the vectors \( \boldsymbol{x}_i \) with \( i=0,1,\dots,p-1 \)
|
||||
In the second figure we have computed the so-called correlation
|
||||
matrix, which in our case with thirty features becomes a \( 30\times 30 \)
|
||||
matrix.
|
||||
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{x}] = \begin{bmatrix}
|
||||
\mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\
|
||||
\mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\
|
||||
\mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_1] & \mathrm{var}[\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\
|
||||
\dots & \dots & \dots & \dots & \dots & \dots \\
|
||||
\dots & \dots & \dots & \dots & \dots & \dots \\
|
||||
\mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & \mathrm{var}[\boldsymbol{x}_{p-1}]\\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
<p>
|
||||
We constructed this matrix using <b>pandas</b> via the statements
|
||||
<p>
|
||||
|
||||
and the correlation matrix
|
||||
$$
|
||||
\boldsymbol{K}[\boldsymbol{x}] = \begin{bmatrix}
|
||||
1 & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\
|
||||
\mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_0] & 1 & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\
|
||||
\mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_1] & 1 & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\
|
||||
\dots & \dots & \dots & \dots & \dots & \dots \\
|
||||
\dots & \dots & \dots & \dots & \dots & \dots \\
|
||||
\mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & 1\\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>cancerpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(cancer<span style="color: #666666">.</span>data, columns<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>feature_names)
|
||||
</pre></div>
|
||||
<p>
|
||||
and then
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>correlation_matrix <span style="color: #666666">=</span> cancerpd<span style="color: #666666">.</span>corr()<span style="color: #666666">.</span>round(<span style="color: #666666">1</span>)
|
||||
</pre></div>
|
||||
<p>
|
||||
Diagonalizing this matrix we can in turn say something about which
|
||||
features are of relevance and which are not. But before we proceed we
|
||||
need to define covariance and correlation matrices. This leads us to
|
||||
the classical Principal Component Analysis (PCA) theorem with
|
||||
applications.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -258,7 +285,7 @@ $$
|
||||
<li><a href="._week43-bs012.html">13</a></li>
|
||||
<li><a href="._week43-bs013.html">14</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs005.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,47 +184,26 @@ MathJax.Hub.Config({
|
||||
<a name="part0005"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec4" class="anchor">Covariance Matrix Examples </h2>
|
||||
<h2 id="___sec3" class="anchor">Basic ideas of the Principal Component Analysis (PCA) </h2>
|
||||
|
||||
<p>
|
||||
The Numpy function <b>np.cov</b> calculates the covariance elements using
|
||||
the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have
|
||||
the exact mean values. The following simple function uses the
|
||||
<b>np.vstack</b> function which takes each vector of dimension \( 1\times n \)
|
||||
and produces a \( 2\times n \) matrix \( \boldsymbol{W} \)
|
||||
|
||||
$$
|
||||
\boldsymbol{W} = \begin{bmatrix} x_0 & y_0 \\
|
||||
x_1 & y_1 \\
|
||||
x_2 & y_2\\
|
||||
\dots & \dots \\
|
||||
x_{n-2} & y_{n-2}\\
|
||||
x_{n-1} & y_{n-1} &
|
||||
\end{bmatrix},
|
||||
$$
|
||||
The principal component analysis deals with the problem of fitting a
|
||||
low-dimensional affine subspace \( S \) of dimension \( d \) much smaller than
|
||||
the totaldimension \( D \) of the problem at hand (our data
|
||||
set). Mathematically it can be formulated as a statistical problem or
|
||||
a geometric problem. In our discussion of the theorem for the
|
||||
classical PCA, we will stay with a statistical approach. This is also
|
||||
what set the scene historically which for the PCA.
|
||||
|
||||
<p>
|
||||
which in turn is converted into into the \( 2\times 2 \) covariance matrix
|
||||
\( \boldsymbol{C} \) via the Numpy function <b>np.cov()</b>. We note that we can also calculate
|
||||
the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy
|
||||
function <b>np.mean(x)</b>. We can also extract the eigenvalues of the
|
||||
covariance matrix through the <b>np.linalg.eig()</b> function.
|
||||
We have a data set defined by a design/feature matrix \( \boldsymbol{X} \) (see below for its definition)
|
||||
|
||||
<p>
|
||||
<ul>
|
||||
<li> Each data point is determined by \( p \) extrinsic (measurement) variables</li>
|
||||
<li> We may want to ask the following question: Are there fewer intrinsic variables (say \( d < < p \)) that still approximately describe the data?</li>
|
||||
<li> If so, these intrinsic variables may tell us something important and finding these intrinsic variables is what dimension reduction methods do.</li>
|
||||
</ul>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Importing various packages</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(np<span style="color: #666666">.</span>mean(x))
|
||||
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(np<span style="color: #666666">.</span>mean(y))
|
||||
W <span style="color: #666666">=</span> np<span style="color: #666666">.</span>vstack((x, y))
|
||||
C <span style="color: #666666">=</span> np<span style="color: #666666">.</span>cov(W)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(C)
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
@@ -241,7 +224,7 @@ C <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c
|
||||
<li><a href="._week43-bs013.html">14</a></li>
|
||||
<li><a href="._week43-bs014.html">15</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs006.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,47 +184,63 @@ MathJax.Hub.Config({
|
||||
<a name="part0006"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec5" class="anchor">Correlation Matrix </h2>
|
||||
<h2 id="___sec4" class="anchor">Introducing the Covariance and Correlation functions </h2>
|
||||
|
||||
<p>
|
||||
The previous example can be converted into the correlation matrix by
|
||||
simply scaling the matrix elements with the variances. We should also
|
||||
subtract the mean values for each column. This leads to the following
|
||||
code which sets up the correlations matrix for the previous example in
|
||||
a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the \( 2\times 2 \) correlation matrix (since we have only two vectors).
|
||||
Before we discuss the PCA theorem, we need to remind ourselves about
|
||||
the definition of the covariance and the correlation function. These are quantities
|
||||
|
||||
<p>
|
||||
Suppose we have defined two vectors
|
||||
\( \hat{x} \) and \( \hat{y} \) with \( n \) elements each. The covariance matrix \( \boldsymbol{C} \) is defined as
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{cov}[\boldsymbol{x},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\
|
||||
\mathrm{cov}[\boldsymbol{y},\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{y},\boldsymbol{y}] \\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
|
||||
<span style="color: #408080; font-style: italic"># define two vectors </span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>random(size<span style="color: #666666">=</span>n)
|
||||
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
<span style="color: #408080; font-style: italic">#scaling the x and y vectors </span>
|
||||
x <span style="color: #666666">=</span> x <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(x)
|
||||
y <span style="color: #666666">=</span> y <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(y)
|
||||
variance_x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #AA22FF">@x</span>)<span style="color: #666666">/</span>n
|
||||
variance_y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(y<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>n
|
||||
<span style="color: #008000; font-weight: bold">print</span>(variance_x)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(variance_y)
|
||||
cov_xy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>n
|
||||
cov_xx <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #AA22FF">@x</span>)<span style="color: #666666">/</span>n
|
||||
cov_yy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(y<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>n
|
||||
C <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #666666">2</span>,<span style="color: #666666">2</span>))
|
||||
C[<span style="color: #666666">0</span>,<span style="color: #666666">0</span>]<span style="color: #666666">=</span> cov_xx<span style="color: #666666">/</span>variance_x
|
||||
C[<span style="color: #666666">1</span>,<span style="color: #666666">1</span>]<span style="color: #666666">=</span> cov_yy<span style="color: #666666">/</span>variance_y
|
||||
C[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>]<span style="color: #666666">=</span> cov_xy<span style="color: #666666">/</span>np<span style="color: #666666">.</span>sqrt(variance_y<span style="color: #666666">*</span>variance_x)
|
||||
C[<span style="color: #666666">1</span>,<span style="color: #666666">0</span>]<span style="color: #666666">=</span> C[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>]
|
||||
<span style="color: #008000; font-weight: bold">print</span>(C)
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that the matrix elements along the diagonal are one as they
|
||||
should be and that the matrix is symmetric. Furthermore, diagonalizing
|
||||
this matrix we easily see that it is a positive definite matrix.
|
||||
where for example
|
||||
$$
|
||||
\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] =\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})(y_i- \overline{y}).
|
||||
$$
|
||||
|
||||
With this definition and recalling that the variance is defined as
|
||||
$$
|
||||
\mathrm{var}[\boldsymbol{x}]=\frac{1}{n} \sum_{i=0}^{n-1}(x_i- \overline{x})^2,
|
||||
$$
|
||||
|
||||
we can rewrite the covariance matrix as
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} \mathrm{var}[\boldsymbol{x}] & \mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] \\
|
||||
\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}] & \mathrm{var}[\boldsymbol{y}] \\
|
||||
\end{bmatrix}.
|
||||
$$
|
||||
|
||||
<p>
|
||||
The above procedure with <b>numpy</b> can be made more compact if we use <b>pandas</b>.
|
||||
The covariance takes values between zero and infinity and may thus
|
||||
lead to problems with loss of numerical precision for particularly
|
||||
large values. It is common to scale the covariance matrix by
|
||||
introducing instead the correlation matrix defined via the so-called
|
||||
correlation function
|
||||
|
||||
$$
|
||||
\mathrm{corr}[\boldsymbol{x},\boldsymbol{y}]=\frac{\mathrm{cov}[\boldsymbol{x},\boldsymbol{y}]}{\sqrt{\mathrm{var}[\boldsymbol{x}] \mathrm{var}[\boldsymbol{y}]}}.
|
||||
$$
|
||||
|
||||
<p>
|
||||
The correlation function is then given by values \( \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}]
|
||||
\in [-1,1] \). This avoids eventual problems with too large values. We
|
||||
can then define the correlation matrix for the two vectors \( \boldsymbol{x} \)
|
||||
and \( \boldsymbol{y} \) as
|
||||
|
||||
$$
|
||||
\boldsymbol{K}[\boldsymbol{x},\boldsymbol{y}] = \begin{bmatrix} 1 & \mathrm{corr}[\boldsymbol{x},\boldsymbol{y}] \\
|
||||
\mathrm{corr}[\boldsymbol{y},\boldsymbol{x}] & 1 \\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
|
||||
<p>
|
||||
In the above example this is the function we constructed using <b>pandas</b>.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -244,7 +264,7 @@ The above procedure with <b>numpy</b> can be made more compact if we use <b>pand
|
||||
<li><a href="._week43-bs014.html">15</a></li>
|
||||
<li><a href="._week43-bs015.html">16</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs007.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,29 +184,63 @@ MathJax.Hub.Config({
|
||||
<a name="part0007"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec6" class="anchor">Correlation Matrix with Pandas </h2>
|
||||
<h2 id="___sec5" class="anchor">Correlation Function and Design/Feature Matrix </h2>
|
||||
|
||||
<p>
|
||||
We whow here how we can set up the correlation matrix using <b>pandas</b>, as done in this simple code
|
||||
<p>
|
||||
In our derivation of the various regression algorithms like <b>Ordinary Least Squares</b> or <b>Ridge regression</b>
|
||||
we defined the design/feature matrix \( \boldsymbol{X} \) as
|
||||
|
||||
$$
|
||||
\boldsymbol{X}=\begin{bmatrix}
|
||||
x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\
|
||||
x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\
|
||||
x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\
|
||||
\dots & \dots & \dots & \dots \dots & \dots \\
|
||||
x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\
|
||||
x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
|
||||
with \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predictors/features \( p \) refering to the column numbers and the
|
||||
entries \( n \) being the row elements.
|
||||
We can rewrite the design/feature matrix in terms of its column vectors as
|
||||
$$
|
||||
\boldsymbol{X}=\begin{bmatrix} \boldsymbol{x}_0 & \boldsymbol{x}_1 & \boldsymbol{x}_2 & \dots & \dots & \boldsymbol{x}_{p-1}\end{bmatrix},
|
||||
$$
|
||||
|
||||
with a given vector
|
||||
$$
|
||||
\boldsymbol{x}_i^T = \begin{bmatrix}x_{0,i} & x_{1,i} & x_{2,i}& \dots & \dots x_{n-1,i}\end{bmatrix}.
|
||||
$$
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">10</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
x <span style="color: #666666">=</span> x <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(x)
|
||||
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
y <span style="color: #666666">=</span> y <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(y)
|
||||
X <span style="color: #666666">=</span> (np<span style="color: #666666">.</span>vstack((x, y)))<span style="color: #666666">.</span>T
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X)
|
||||
Xpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(X)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(Xpd)
|
||||
correlation_matrix <span style="color: #666666">=</span> Xpd<span style="color: #666666">.</span>corr()
|
||||
<span style="color: #008000; font-weight: bold">print</span>(correlation_matrix)
|
||||
</pre></div>
|
||||
<p>
|
||||
We expand this model to the Franke function discussed above.
|
||||
With these definitions, we can now rewrite our \( 2\times 2 \)
|
||||
correaltion/covariance matrix in terms of a moe general design/feature
|
||||
matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \). This leads to a \( p\times p \)
|
||||
covariance matrix for the vectors \( \boldsymbol{x}_i \) with \( i=0,1,\dots,p-1 \)
|
||||
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{x}] = \begin{bmatrix}
|
||||
\mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\
|
||||
\mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\
|
||||
\mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_1] & \mathrm{var}[\boldsymbol{x}_2] & \dots & \dots & \mathrm{cov}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\
|
||||
\dots & \dots & \dots & \dots & \dots & \dots \\
|
||||
\dots & \dots & \dots & \dots & \dots & \dots \\
|
||||
\mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{cov}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & \mathrm{var}[\boldsymbol{x}_{p-1}]\\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
|
||||
and the correlation matrix
|
||||
$$
|
||||
\boldsymbol{K}[\boldsymbol{x}] = \begin{bmatrix}
|
||||
1 & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_0,\boldsymbol{x}_{p-1}]\\
|
||||
\mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_0] & 1 & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_2] & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_1,\boldsymbol{x}_{p-1}]\\
|
||||
\mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_1] & 1 & \dots & \dots & \mathrm{corr}[\boldsymbol{x}_2,\boldsymbol{x}_{p-1}]\\
|
||||
\dots & \dots & \dots & \dots & \dots & \dots \\
|
||||
\dots & \dots & \dots & \dots & \dots & \dots \\
|
||||
\mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_0] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_1] & \mathrm{corr}[\boldsymbol{x}_{p-1},\boldsymbol{x}_{2}] & \dots & \dots & 1\\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -227,7 +265,7 @@ We expand this model to the Franke function discussed above.
|
||||
<li><a href="._week43-bs015.html">16</a></li>
|
||||
<li><a href="._week43-bs016.html">17</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs008.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,66 +184,46 @@ MathJax.Hub.Config({
|
||||
<a name="part0008"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec7" class="anchor">Correlation Matrix with Pandas and the Franke function </h2>
|
||||
<h2 id="___sec6" class="anchor">Covariance Matrix Examples </h2>
|
||||
|
||||
<p>
|
||||
The Numpy function <b>np.cov</b> calculates the covariance elements using
|
||||
the factor \( 1/(n-1) \) instead of \( 1/n \) since it assumes we do not have
|
||||
the exact mean values. The following simple function uses the
|
||||
<b>np.vstack</b> function which takes each vector of dimension \( 1\times n \)
|
||||
and produces a \( 2\times n \) matrix \( \boldsymbol{W} \)
|
||||
|
||||
$$
|
||||
\boldsymbol{W} = \begin{bmatrix} x_0 & y_0 \\
|
||||
x_1 & y_1 \\
|
||||
x_2 & y_2\\
|
||||
\dots & \dots \\
|
||||
x_{n-2} & y_{n-2}\\
|
||||
x_{n-1} & y_{n-1} &
|
||||
\end{bmatrix},
|
||||
$$
|
||||
|
||||
<p>
|
||||
which in turn is converted into into the \( 2\times 2 \) covariance matrix
|
||||
\( \boldsymbol{C} \) via the Numpy function <b>np.cov()</b>. We note that we can also calculate
|
||||
the mean value of each set of samples \( \boldsymbol{x} \) etc using the Numpy
|
||||
function <b>np.mean(x)</b>. We can also extract the eigenvalues of the
|
||||
covariance matrix through the <b>np.linalg.eig()</b> function.
|
||||
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Common imports</span>
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Importing various packages</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">FrankeFunction</span>(x,y):
|
||||
term1 <span style="color: #666666">=</span> <span style="color: #666666">0.75*</span>np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(<span style="color: #666666">0.25*</span>(<span style="color: #666666">9*</span>x<span style="color: #666666">-2</span>)<span style="color: #666666">**2</span>) <span style="color: #666666">-</span> <span style="color: #666666">0.25*</span>((<span style="color: #666666">9*</span>y<span style="color: #666666">-2</span>)<span style="color: #666666">**2</span>))
|
||||
term2 <span style="color: #666666">=</span> <span style="color: #666666">0.75*</span>np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>((<span style="color: #666666">9*</span>x<span style="color: #666666">+1</span>)<span style="color: #666666">**2</span>)<span style="color: #666666">/49.0</span> <span style="color: #666666">-</span> <span style="color: #666666">0.1*</span>(<span style="color: #666666">9*</span>y<span style="color: #666666">+1</span>))
|
||||
term3 <span style="color: #666666">=</span> <span style="color: #666666">0.5*</span>np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(<span style="color: #666666">9*</span>x<span style="color: #666666">-7</span>)<span style="color: #666666">**2/4.0</span> <span style="color: #666666">-</span> <span style="color: #666666">0.25*</span>((<span style="color: #666666">9*</span>y<span style="color: #666666">-3</span>)<span style="color: #666666">**2</span>))
|
||||
term4 <span style="color: #666666">=</span> <span style="color: #666666">-0.2*</span>np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(<span style="color: #666666">9*</span>x<span style="color: #666666">-4</span>)<span style="color: #666666">**2</span> <span style="color: #666666">-</span> (<span style="color: #666666">9*</span>y<span style="color: #666666">-7</span>)<span style="color: #666666">**2</span>)
|
||||
<span style="color: #008000; font-weight: bold">return</span> term1 <span style="color: #666666">+</span> term2 <span style="color: #666666">+</span> term3 <span style="color: #666666">+</span> term4
|
||||
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_X</span>(x, y, n ):
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(x<span style="color: #666666">.</span>shape) <span style="color: #666666">></span> <span style="color: #666666">1</span>:
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>ravel(x)
|
||||
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>ravel(y)
|
||||
|
||||
N <span style="color: #666666">=</span> <span style="color: #008000">len</span>(x)
|
||||
l <span style="color: #666666">=</span> <span style="color: #008000">int</span>((n<span style="color: #666666">+1</span>)<span style="color: #666666">*</span>(n<span style="color: #666666">+2</span>)<span style="color: #666666">/2</span>) <span style="color: #408080; font-style: italic"># Number of elements in beta</span>
|
||||
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>ones((N,l))
|
||||
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">1</span>,n<span style="color: #666666">+1</span>):
|
||||
q <span style="color: #666666">=</span> <span style="color: #008000">int</span>((i)<span style="color: #666666">*</span>(i<span style="color: #666666">+1</span>)<span style="color: #666666">/2</span>)
|
||||
<span style="color: #008000; font-weight: bold">for</span> k <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(i<span style="color: #666666">+1</span>):
|
||||
X[:,q<span style="color: #666666">+</span>k] <span style="color: #666666">=</span> (x<span style="color: #666666">**</span>(i<span style="color: #666666">-</span>k))<span style="color: #666666">*</span>(y<span style="color: #666666">**</span>k)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">return</span> X
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Making meshgrid of datapoints and compute Franke's function</span>
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">4</span>
|
||||
N <span style="color: #666666">=</span> <span style="color: #666666">100</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sort(np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>uniform(<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, N))
|
||||
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sort(np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>uniform(<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, N))
|
||||
z <span style="color: #666666">=</span> FrankeFunction(x, y)
|
||||
X <span style="color: #666666">=</span> create_X(x, y, n<span style="color: #666666">=</span>n)
|
||||
|
||||
Xpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(X)
|
||||
<span style="color: #408080; font-style: italic"># subtract the mean values and set up the covariance matrix</span>
|
||||
Xpd <span style="color: #666666">=</span> Xpd <span style="color: #666666">-</span> Xpd<span style="color: #666666">.</span>mean()
|
||||
covariance_matrix <span style="color: #666666">=</span> Xpd<span style="color: #666666">.</span>cov()
|
||||
<span style="color: #008000; font-weight: bold">print</span>(covariance_matrix)
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
<span style="color: #008000">print</span>(np<span style="color: #666666">.</span>mean(x))
|
||||
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
<span style="color: #008000">print</span>(np<span style="color: #666666">.</span>mean(y))
|
||||
W <span style="color: #666666">=</span> np<span style="color: #666666">.</span>vstack((x, y))
|
||||
C <span style="color: #666666">=</span> np<span style="color: #666666">.</span>cov(W)
|
||||
<span style="color: #008000">print</span>(C)
|
||||
</pre></div>
|
||||
<p>
|
||||
We note here that the covariance is zero for the first rows and
|
||||
columns since all matrix elements in the design matrix were set to one
|
||||
(we are fitting the function in terms of a polynomial of degree \( n \)).
|
||||
|
||||
<p>
|
||||
This means that the variance for these elements will be zero and will
|
||||
cause problems when we set up the correlation matrix. We can simply
|
||||
drop these elements and construct a correlation
|
||||
matrix without these elements.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -264,7 +248,7 @@ matrix without these elements.
|
||||
<li><a href="._week43-bs016.html">17</a></li>
|
||||
<li><a href="._week43-bs017.html">18</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs009.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,45 +184,47 @@ MathJax.Hub.Config({
|
||||
<a name="part0009"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec8" class="anchor">Rewriting the Covariance and/or Correlation Matrix </h2>
|
||||
<h2 id="___sec7" class="anchor">Correlation Matrix </h2>
|
||||
|
||||
<p>
|
||||
We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}\boldsymbol{X}^T= \mathbb{E}[\boldsymbol{X}\boldsymbol{X}^T].
|
||||
$$
|
||||
The previous example can be converted into the correlation matrix by
|
||||
simply scaling the matrix elements with the variances. We should also
|
||||
subtract the mean values for each column. This leads to the following
|
||||
code which sets up the correlations matrix for the previous example in
|
||||
a more brute force way. Here we scale the mean values for each column of the design matrix, calculate the relevant mean values and variances and then finally set up the \( 2\times 2 \) correlation matrix (since we have only two vectors).
|
||||
|
||||
<p>
|
||||
To see this let us simply look at a design matrix \( \boldsymbol{X}\in {\mathbb{R}}^{2\times 2} \)
|
||||
$$
|
||||
\boldsymbol{X}=\begin{bmatrix}
|
||||
x_{00} & x_{01}\\
|
||||
x_{10} & x_{11}\\
|
||||
\end{bmatrix}=\begin{bmatrix}
|
||||
\boldsymbol{x}_{0} & \boldsymbol{x}_{1}\\
|
||||
\end{bmatrix}.
|
||||
$$
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
|
||||
<span style="color: #408080; font-style: italic"># define two vectors </span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>random(size<span style="color: #666666">=</span>n)
|
||||
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
<span style="color: #408080; font-style: italic">#scaling the x and y vectors </span>
|
||||
x <span style="color: #666666">=</span> x <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(x)
|
||||
y <span style="color: #666666">=</span> y <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(y)
|
||||
variance_x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #AA22FF">@x</span>)<span style="color: #666666">/</span>n
|
||||
variance_y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(y<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>n
|
||||
<span style="color: #008000">print</span>(variance_x)
|
||||
<span style="color: #008000">print</span>(variance_y)
|
||||
cov_xy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>n
|
||||
cov_xx <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #AA22FF">@x</span>)<span style="color: #666666">/</span>n
|
||||
cov_yy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(y<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>n
|
||||
C <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #666666">2</span>,<span style="color: #666666">2</span>))
|
||||
C[<span style="color: #666666">0</span>,<span style="color: #666666">0</span>]<span style="color: #666666">=</span> cov_xx<span style="color: #666666">/</span>variance_x
|
||||
C[<span style="color: #666666">1</span>,<span style="color: #666666">1</span>]<span style="color: #666666">=</span> cov_yy<span style="color: #666666">/</span>variance_y
|
||||
C[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>]<span style="color: #666666">=</span> cov_xy<span style="color: #666666">/</span>np<span style="color: #666666">.</span>sqrt(variance_y<span style="color: #666666">*</span>variance_x)
|
||||
C[<span style="color: #666666">1</span>,<span style="color: #666666">0</span>]<span style="color: #666666">=</span> C[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>]
|
||||
<span style="color: #008000">print</span>(C)
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that the matrix elements along the diagonal are one as they
|
||||
should be and that the matrix is symmetric. Furthermore, diagonalizing
|
||||
this matrix we easily see that it is a positive definite matrix.
|
||||
|
||||
<p>
|
||||
If we then compute the expectation value
|
||||
$$
|
||||
\mathbb{E}[\boldsymbol{X}\boldsymbol{X}^T] = \frac{1}{n}\boldsymbol{X}\boldsymbol{X}^T=\begin{bmatrix}
|
||||
x_{00}^2+x_{01}^2 & x_{00}x_{10}+x_{01}x_{11}\\
|
||||
x_{10}x_{00}+x_{11}x_{01} & x_{10}^2+x_{11}^2\\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
|
||||
which is just
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]=\begin{bmatrix} \mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] \\
|
||||
\mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] \\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
|
||||
where we wrote $$\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]$$ to indicate that this the covariance of the vectors \( \boldsymbol{x} \) of the design/feature matrix \( \boldsymbol{X} \).
|
||||
|
||||
<p>
|
||||
It is easy to generalize this to a matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \).
|
||||
The above procedure with <b>numpy</b> can be made more compact if we use <b>pandas</b>.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -245,7 +251,7 @@ It is easy to generalize this to a matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\t
|
||||
<li><a href="._week43-bs017.html">18</a></li>
|
||||
<li><a href="._week43-bs018.html">19</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs010.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,53 +184,29 @@ MathJax.Hub.Config({
|
||||
<a name="part0010"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec9" class="anchor">Towards the PCA theorem </h2>
|
||||
<h2 id="___sec8" class="anchor">Correlation Matrix with Pandas </h2>
|
||||
|
||||
<p>
|
||||
We have that the covariance matrix (the correlation matrix involves a simple rescaling) is given as
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}\boldsymbol{X}^T= \mathbb{E}[\boldsymbol{X}\boldsymbol{X}^T].
|
||||
$$
|
||||
|
||||
Let us now assume that we can perform a series of orthogonal transformations where we employ some orthogonal matrices \( \boldsymbol{S} \).
|
||||
These matrices are defined as \( \boldsymbol{S}\in {\mathbb{R}}^{p\times p} \) and obey the orthogonality requirements \( \boldsymbol{S}\boldsymbol{S}^T=\boldsymbol{S}^T\boldsymbol{S}=\boldsymbol{I} \). The matrix can be written out in terms of the column vectors \( \boldsymbol{s}_i \) as \( \boldsymbol{S}=[\boldsymbol{s}_0,\boldsymbol{s}_1,\dots,\boldsymbol{s}_{p-1}] \) and \( \boldsymbol{s}_i \in {\mathbb{R}}^{p} \).
|
||||
|
||||
We whow here how we can set up the correlation matrix using <b>pandas</b>, as done in this simple code
|
||||
<p>
|
||||
Assume also that there is a transformation \( \boldsymbol{S}\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}^T=\boldsymbol{C}[\boldsymbol{y}] \) such that the new matrix \( \boldsymbol{C}[\boldsymbol{y}] \) is diagonal with elements \( [\lambda_0,\lambda_1,\lambda_2,\dots,\lambda_{p-1}] \).
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">10</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
x <span style="color: #666666">=</span> x <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(x)
|
||||
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
y <span style="color: #666666">=</span> y <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(y)
|
||||
X <span style="color: #666666">=</span> (np<span style="color: #666666">.</span>vstack((x, y)))<span style="color: #666666">.</span>T
|
||||
<span style="color: #008000">print</span>(X)
|
||||
Xpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(X)
|
||||
<span style="color: #008000">print</span>(Xpd)
|
||||
correlation_matrix <span style="color: #666666">=</span> Xpd<span style="color: #666666">.</span>corr()
|
||||
<span style="color: #008000">print</span>(correlation_matrix)
|
||||
</pre></div>
|
||||
<p>
|
||||
That is we have
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{y}] = \mathbb{E}[\boldsymbol{S}\boldsymbol{X}\boldsymbol{X}^T\boldsymbol{S}^T]=\boldsymbol{S}\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}^T,
|
||||
$$
|
||||
|
||||
since the matrix \( \boldsymbol{S} \) is not a data dependent matrix. Multiplying with \( \boldsymbol{S}^T \) from the left we have
|
||||
$$
|
||||
\boldsymbol{S}^T\boldsymbol{C}[\boldsymbol{y}] = \boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}^T,
|
||||
$$
|
||||
|
||||
and since \( \boldsymbol{C}[\boldsymbol{y}] \) is diagonal we have for a given eigenvalue \( i \) of the covariance matrix that
|
||||
|
||||
$$
|
||||
\boldsymbol{S}^T_i\lambda_i = \boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}^T_i.
|
||||
$$
|
||||
|
||||
<p>
|
||||
In the derivation of the PCA theorem we will assume that the eigenvalues are ordered in descending order, that is
|
||||
\( \lambda_0 > \lambda_1 > \dots > \lambda_{p-1} \).
|
||||
|
||||
<p>
|
||||
The eigenvalues tell us then how much we need to stretch the
|
||||
corresponding eigenvectors. Dimensions with large eigenvalues have
|
||||
thus large variations (large variance) and define therefore useful
|
||||
dimensions. The data points are more spread out in the direction of
|
||||
these eigenvectors. Smaller eigenvalues mean on the other hand that
|
||||
the corresponding eigenvectors are shrunk accordingly and the data
|
||||
points are tightly bunched together and there is not much variation in
|
||||
these specific directions. Hopefully then we could leave it out
|
||||
dimensions where the eigenvalues are very small. If \( p \) is very large,
|
||||
we could then aim at reducing \( p \) to \( l < < p \) and handle only \( l \)
|
||||
features/predictors.
|
||||
We expand this model to the Franke function discussed above.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -254,7 +234,7 @@ features/predictors.
|
||||
<li><a href="._week43-bs018.html">19</a></li>
|
||||
<li><a href="._week43-bs019.html">20</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs011.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,35 +184,67 @@ MathJax.Hub.Config({
|
||||
<a name="part0011"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec10" class="anchor">The Algorithm before theorem </h2>
|
||||
<h2 id="___sec9" class="anchor">Correlation Matrix with Pandas and the Franke function </h2>
|
||||
|
||||
<p>
|
||||
Here's how we would proceed in setting up the algorithm for the PCA, see also discussion below here.
|
||||
|
||||
<ul>
|
||||
<li> Set up the datapoints for the design/feature matrix \( \boldsymbol{X} \) with \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predictors/features \( p \) referring to the column numbers and the entries \( n \) being the row elements.</li>
|
||||
</ul>
|
||||
|
||||
$$
|
||||
\boldsymbol{X}=\begin{bmatrix}
|
||||
x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\
|
||||
x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\
|
||||
x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\
|
||||
\dots & \dots & \dots & \dots \dots & \dots \\
|
||||
x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\
|
||||
x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Common imports</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
|
||||
|
||||
<ul>
|
||||
<li> Center the data by subtracting the mean value for each column. This leads to a new matrix \( \boldsymbol{X}\rightarrow \overline{\boldsymbol{X}} \).</li>
|
||||
<li> Compute then the covariance/correlation matrix \( \mathbb{E}[\overline{\boldsymbol{X}}\overline{\boldsymbol{X}}^T] \).</li>
|
||||
<li> Find the eigenpairs of \( \boldsymbol{C} \) with eigenvalues \( [\lambda_0,\lambda_1,\dots,\lambda_{p-1}] \) and eigenvectors \( [\boldsymbol{s}_0,\boldsymbol{s}_1,\dots,\boldsymbol{s}_{p-1}] \).</li>
|
||||
<li> Order the eigenvalue (and the eigenvectors accordingly) in order of decreasing eigenvalues.</li>
|
||||
<li> Keep only those \( l \) eigenvalues larger than a selected threshold value, discarding thus \( p-l \) features since we expect small variations in the data here.</li>
|
||||
</ul>
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">FrankeFunction</span>(x,y):
|
||||
term1 <span style="color: #666666">=</span> <span style="color: #666666">0.75*</span>np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(<span style="color: #666666">0.25*</span>(<span style="color: #666666">9*</span>x<span style="color: #666666">-2</span>)<span style="color: #666666">**2</span>) <span style="color: #666666">-</span> <span style="color: #666666">0.25*</span>((<span style="color: #666666">9*</span>y<span style="color: #666666">-2</span>)<span style="color: #666666">**2</span>))
|
||||
term2 <span style="color: #666666">=</span> <span style="color: #666666">0.75*</span>np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>((<span style="color: #666666">9*</span>x<span style="color: #666666">+1</span>)<span style="color: #666666">**2</span>)<span style="color: #666666">/49.0</span> <span style="color: #666666">-</span> <span style="color: #666666">0.1*</span>(<span style="color: #666666">9*</span>y<span style="color: #666666">+1</span>))
|
||||
term3 <span style="color: #666666">=</span> <span style="color: #666666">0.5*</span>np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(<span style="color: #666666">9*</span>x<span style="color: #666666">-7</span>)<span style="color: #666666">**2/4.0</span> <span style="color: #666666">-</span> <span style="color: #666666">0.25*</span>((<span style="color: #666666">9*</span>y<span style="color: #666666">-3</span>)<span style="color: #666666">**2</span>))
|
||||
term4 <span style="color: #666666">=</span> <span style="color: #666666">-0.2*</span>np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>(<span style="color: #666666">9*</span>x<span style="color: #666666">-4</span>)<span style="color: #666666">**2</span> <span style="color: #666666">-</span> (<span style="color: #666666">9*</span>y<span style="color: #666666">-7</span>)<span style="color: #666666">**2</span>)
|
||||
<span style="color: #008000; font-weight: bold">return</span> term1 <span style="color: #666666">+</span> term2 <span style="color: #666666">+</span> term3 <span style="color: #666666">+</span> term4
|
||||
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">create_X</span>(x, y, n ):
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">len</span>(x<span style="color: #666666">.</span>shape) <span style="color: #666666">></span> <span style="color: #666666">1</span>:
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>ravel(x)
|
||||
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>ravel(y)
|
||||
|
||||
N <span style="color: #666666">=</span> <span style="color: #008000">len</span>(x)
|
||||
l <span style="color: #666666">=</span> <span style="color: #008000">int</span>((n<span style="color: #666666">+1</span>)<span style="color: #666666">*</span>(n<span style="color: #666666">+2</span>)<span style="color: #666666">/2</span>) <span style="color: #408080; font-style: italic"># Number of elements in beta</span>
|
||||
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>ones((N,l))
|
||||
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">1</span>,n<span style="color: #666666">+1</span>):
|
||||
q <span style="color: #666666">=</span> <span style="color: #008000">int</span>((i)<span style="color: #666666">*</span>(i<span style="color: #666666">+1</span>)<span style="color: #666666">/2</span>)
|
||||
<span style="color: #008000; font-weight: bold">for</span> k <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(i<span style="color: #666666">+1</span>):
|
||||
X[:,q<span style="color: #666666">+</span>k] <span style="color: #666666">=</span> (x<span style="color: #666666">**</span>(i<span style="color: #666666">-</span>k))<span style="color: #666666">*</span>(y<span style="color: #666666">**</span>k)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">return</span> X
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Making meshgrid of datapoints and compute Franke's function</span>
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">4</span>
|
||||
N <span style="color: #666666">=</span> <span style="color: #666666">100</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sort(np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>uniform(<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, N))
|
||||
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sort(np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>uniform(<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, N))
|
||||
z <span style="color: #666666">=</span> FrankeFunction(x, y)
|
||||
X <span style="color: #666666">=</span> create_X(x, y, n<span style="color: #666666">=</span>n)
|
||||
|
||||
Xpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(X)
|
||||
<span style="color: #408080; font-style: italic"># subtract the mean values and set up the covariance matrix</span>
|
||||
Xpd <span style="color: #666666">=</span> Xpd <span style="color: #666666">-</span> Xpd<span style="color: #666666">.</span>mean()
|
||||
covariance_matrix <span style="color: #666666">=</span> Xpd<span style="color: #666666">.</span>cov()
|
||||
<span style="color: #008000">print</span>(covariance_matrix)
|
||||
</pre></div>
|
||||
<p>
|
||||
We note here that the covariance is zero for the first rows and
|
||||
columns since all matrix elements in the design matrix were set to one
|
||||
(we are fitting the function in terms of a polynomial of degree \( n \)).
|
||||
|
||||
<p>
|
||||
This means that the variance for these elements will be zero and will
|
||||
cause problems when we set up the correlation matrix. We can simply
|
||||
drop these elements and construct a correlation
|
||||
matrix without these elements.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
@@ -234,7 +270,7 @@ $$
|
||||
<li><a href="._week43-bs019.html">20</a></li>
|
||||
<li><a href="._week43-bs020.html">21</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs012.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,178 +184,45 @@ MathJax.Hub.Config({
|
||||
<a name="part0012"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec11" class="anchor">Writing our own PCA code </h2>
|
||||
<h2 id="___sec10" class="anchor">Rewriting the Covariance and/or Correlation Matrix </h2>
|
||||
|
||||
<p>
|
||||
We will use a simple example first with two-dimensional data
|
||||
drawn from a multivariate normal distribution with the following mean and covariance matrix:
|
||||
We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as
|
||||
$$
|
||||
\mu = (-1,2) \qquad \Sigma = \begin{bmatrix} 4 & 2 \\
|
||||
2 & 2
|
||||
\end{bmatrix}
|
||||
\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}\boldsymbol{X}^T= \mathbb{E}[\boldsymbol{X}\boldsymbol{X}^T].
|
||||
$$
|
||||
|
||||
Note that the mean refers to each column of data.
|
||||
We will generate \( n = 1000 \) points \( X = \{ x_1, \ldots, x_N \} \) from
|
||||
this distribution, and store them in the \( 1000 \times 2 \) matrix \( \boldsymbol{X} \).
|
||||
|
||||
<p>
|
||||
The following Python code aids in setting up the data and writing out the design matrix.
|
||||
Note that the function <b>multivariate</b> returns also the covariance discussed above and that it is defined by dividing by \( n-1 \) instead of \( n \).
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> display
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">10000</span>
|
||||
mean <span style="color: #666666">=</span> (<span style="color: #666666">-1</span>, <span style="color: #666666">2</span>)
|
||||
cov <span style="color: #666666">=</span> [[<span style="color: #666666">4</span>, <span style="color: #666666">2</span>], [<span style="color: #666666">2</span>, <span style="color: #666666">2</span>]]
|
||||
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>multivariate_normal(mean, cov, n)
|
||||
</pre></div>
|
||||
<p>
|
||||
Now we are going to implement the PCA algorithm. We will break it down into various substeps.
|
||||
|
||||
<h3 id="___sec12" class="anchor">Compute the sample mean and center the data </h3>
|
||||
|
||||
<p>
|
||||
The first step of PCA is to compute the sample mean of the data and use it to center the data. Recall that the sample mean is
|
||||
To see this let us simply look at a design matrix \( \boldsymbol{X}\in {\mathbb{R}}^{2\times 2} \)
|
||||
$$
|
||||
\mu_n = \frac{1}{n} \sum_{i=1}^n x_i
|
||||
\boldsymbol{X}=\begin{bmatrix}
|
||||
x_{00} & x_{01}\\
|
||||
x_{10} & x_{11}\\
|
||||
\end{bmatrix}=\begin{bmatrix}
|
||||
\boldsymbol{x}_{0} & \boldsymbol{x}_{1}\\
|
||||
\end{bmatrix}.
|
||||
$$
|
||||
|
||||
and the mean-centered data \( \bar{X} = \{ \bar{x}_1, \ldots, \bar{x}_n \} \) takes the form
|
||||
<p>
|
||||
If we then compute the expectation value
|
||||
$$
|
||||
\bar{x}_i = x_i - \mu_n.
|
||||
\mathbb{E}[\boldsymbol{X}\boldsymbol{X}^T] = \frac{1}{n}\boldsymbol{X}\boldsymbol{X}^T=\begin{bmatrix}
|
||||
x_{00}^2+x_{01}^2 & x_{00}x_{10}+x_{01}x_{11}\\
|
||||
x_{10}x_{00}+x_{11}x_{01} & x_{10}^2+x_{11}^2\\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
|
||||
When you are done with these steps, print out \( \mu_n \) to verify it is
|
||||
close to \( \mu \) and plot your mean centered data to verify it is
|
||||
centered at the origin! Compare your code with the functionality from <b>Scikit-Learn</b> discussed above.
|
||||
The following code elements perform these operations using <b>pandas</b> or using our own functionality for doing so. The latter, using <b>numpy</b> is rather simple through the <b>mean()</b> function.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>df <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(X)
|
||||
<span style="color: #408080; font-style: italic"># Pandas does the centering for us</span>
|
||||
df <span style="color: #666666">=</span> df <span style="color: #666666">-</span>df<span style="color: #666666">.</span>mean()
|
||||
<span style="color: #408080; font-style: italic"># we center it ourselves</span>
|
||||
X_centered <span style="color: #666666">=</span> X <span style="color: #666666">-</span> X<span style="color: #666666">.</span>mean(axis<span style="color: #666666">=0</span>)
|
||||
</pre></div>
|
||||
<p>
|
||||
Alternatively, we could use the functions we discussed
|
||||
earlier for scaling the data set. That is, we could have used the
|
||||
<b>StandardScaler</b> function in <b>Scikit-Learn</b>, a function which ensures
|
||||
that for each feature/predictor we study the mean value is zero and
|
||||
the variance is one (every column in the design/feature matrix). You
|
||||
would then not get the same results, since we divide by the
|
||||
variance. The diagonal covariance matrix elements will then be one,
|
||||
while the non-diagonal ones need to be divided by \( 2\sqrt{2} \) for our
|
||||
specific case.
|
||||
|
||||
<h3 id="___sec13" class="anchor">Compute the sample covariance </h3>
|
||||
|
||||
<p>
|
||||
Now we are going to use the mean centered data to compute the sample covariance of the data by using the following equation
|
||||
which is just
|
||||
$$
|
||||
\begin{equation*}
|
||||
\Sigma_n = \frac{1}{n-1} \sum_{i=1}^n \bar{x}_i^T \bar{x}_i = \frac{1}{n-1} \sum_{i=1}^n (x_i - \mu_n)^T (x_i - \mu_n)
|
||||
\end{equation*}
|
||||
\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]=\begin{bmatrix} \mathrm{var}[\boldsymbol{x}_0] & \mathrm{cov}[\boldsymbol{x}_0,\boldsymbol{x}_1] \\
|
||||
\mathrm{cov}[\boldsymbol{x}_1,\boldsymbol{x}_0] & \mathrm{var}[\boldsymbol{x}_1] \\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
|
||||
where the data points \( x_i \in \mathbb{R}^p \) (here in this example \( p = 2 \)) are column vectors and \( x^T \) is the transpose of \( x \).
|
||||
We can write our own code or simply use either the functionaly of <b>numpy</b> or that of <b>pandas</b>, as follows
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">print</span>(df<span style="color: #666666">.</span>cov())
|
||||
<span style="color: #008000; font-weight: bold">print</span>(np<span style="color: #666666">.</span>cov(X_centered<span style="color: #666666">.</span>T))
|
||||
</pre></div>
|
||||
<p>
|
||||
Note that the way we define the covariance matrix here has a factor \( n-1 \) instead of \( n \). This is included in the <b>cov()</b> function by <b>numpy</b> and <b>pandas</b>.
|
||||
Our own code here is not very elegant and asks for obvious improvements. It is tailored to this specific \( 2\times 2 \) covariance matrix.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># extract the relevant columns from the centered design matrix of dim n x 2</span>
|
||||
x <span style="color: #666666">=</span> X_centered[:,<span style="color: #666666">0</span>]
|
||||
y <span style="color: #666666">=</span> X_centered[:,<span style="color: #666666">1</span>]
|
||||
Cov <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #666666">2</span>,<span style="color: #666666">2</span>))
|
||||
Cov[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #666666">.</span>T<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>(n<span style="color: #666666">-1.0</span>)
|
||||
Cov[<span style="color: #666666">0</span>,<span style="color: #666666">0</span>] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #666666">.</span>T<span style="color: #AA22FF">@x</span>)<span style="color: #666666">/</span>(n<span style="color: #666666">-1.0</span>)
|
||||
Cov[<span style="color: #666666">1</span>,<span style="color: #666666">1</span>] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(y<span style="color: #666666">.</span>T<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>(n<span style="color: #666666">-1.0</span>)
|
||||
Cov[<span style="color: #666666">1</span>,<span style="color: #666666">0</span>]<span style="color: #666666">=</span> Cov[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>]
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Centered covariance using own code"</span>)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(Cov)
|
||||
plt<span style="color: #666666">.</span>plot(x, y, <span style="color: #BA2121">'x'</span>)
|
||||
plt<span style="color: #666666">.</span>axis(<span style="color: #BA2121">'equal'</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
Depending on the number of points \( n \), we will get results that are close to the covariance values defined above.
|
||||
The plot shows how the data are clustered around a line with slope close to one. Is this expected?
|
||||
|
||||
<h3 id="___sec14" class="anchor">Diagonalize the sample covariance matrix to obtain the principal components </h3>
|
||||
where we wrote $$\boldsymbol{C}[\boldsymbol{x}_0,\boldsymbol{x}_1] = \boldsymbol{C}[\boldsymbol{x}]$$ to indicate that this the covariance of the vectors \( \boldsymbol{x} \) of the design/feature matrix \( \boldsymbol{X} \).
|
||||
|
||||
<p>
|
||||
Now we are ready to solve for the principal components! To do so we
|
||||
diagonalize the sample covariance matrix \( \Sigma \). We can use the
|
||||
function <b>np.linalg.eig</b> to do so. It will return the eigenvalues and
|
||||
eigenvectors of \( \Sigma \). Once we have these we can perform the
|
||||
following tasks:
|
||||
|
||||
<ul>
|
||||
<li> We compute the percentage of the total variance captured by the first principal component</li>
|
||||
<li> We plot the mean centered data and lines along the first and second principal components</li>
|
||||
<li> Then we project the mean centered data onto the first and second principal components, and plot the projected data.</li>
|
||||
<li> Finally, we approximate the data as</li>
|
||||
</ul>
|
||||
|
||||
$$
|
||||
\begin{equation*}
|
||||
x_i \approx \tilde{x}_i = \mu_n + \langle x_i, v_0 \rangle v_0
|
||||
\end{equation*}
|
||||
$$
|
||||
|
||||
where \( v_0 \) is the first principal component.
|
||||
|
||||
<p>
|
||||
Collecting all these steps we can write our own PCA function and
|
||||
compare this with the functionality included in <b>Scikit-Learn</b>.
|
||||
|
||||
<p>
|
||||
The code here outlines some of the elements we could include in the
|
||||
analysis. Feel free to extend upon this in order to address the above
|
||||
questions.
|
||||
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># diagonalize and obtain eigenvalues, not necessarily sorted</span>
|
||||
EigValues, EigVectors <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>eig(Cov)
|
||||
<span style="color: #408080; font-style: italic"># sort eigenvectors and eigenvalues</span>
|
||||
<span style="color: #408080; font-style: italic">#permute = EigValues.argsort()</span>
|
||||
<span style="color: #408080; font-style: italic">#EigValues = EigValues[permute]</span>
|
||||
<span style="color: #408080; font-style: italic">#EigVectors = EigVectors[:,permute]</span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Eigenvalues of Covariance matrix"</span>)
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">2</span>):
|
||||
<span style="color: #008000; font-weight: bold">print</span>(EigValues[i])
|
||||
FirstEigvector <span style="color: #666666">=</span> EigVectors[:,<span style="color: #666666">0</span>]
|
||||
SecondEigvector <span style="color: #666666">=</span> EigVectors[:,<span style="color: #666666">1</span>]
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"First eigenvector"</span>)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(FirstEigvector)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Second eigenvector"</span>)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(SecondEigvector)
|
||||
<span style="color: #408080; font-style: italic">#thereafter we do a PCA with Scikit-learn</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.decomposition</span> <span style="color: #008000; font-weight: bold">import</span> PCA
|
||||
pca <span style="color: #666666">=</span> PCA(n_components <span style="color: #666666">=</span> <span style="color: #666666">2</span>)
|
||||
X2Dsl <span style="color: #666666">=</span> pca<span style="color: #666666">.</span>fit_transform(X)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Eigenvector of largest eigenvalue"</span>)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(pca<span style="color: #666666">.</span>components_<span style="color: #666666">.</span>T[:, <span style="color: #666666">0</span>])
|
||||
</pre></div>
|
||||
<p>
|
||||
This code does not contain all the above elements, but it shows how we can use <b>Scikit-Learn</b> to extract the eigenvector which corresponds to the largest eigenvalue. Try to address the questions we pose before the above code. Try also to change the values of the covariance matrix by making one of the diagonal elements much larger than the other. What do you observe then?
|
||||
It is easy to generalize this to a matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \).
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -379,7 +250,7 @@ This code does not contain all the above elements, but it shows how we can use <
|
||||
<li><a href="._week43-bs020.html">21</a></li>
|
||||
<li><a href="._week43-bs021.html">22</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs013.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,35 +184,53 @@ MathJax.Hub.Config({
|
||||
<a name="part0013"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec15" class="anchor">Classical PCA Theorem </h2>
|
||||
<h2 id="___sec11" class="anchor">Towards the PCA theorem </h2>
|
||||
|
||||
<p>
|
||||
We assume now that we have a design matrix \( \boldsymbol{X} \) which has been
|
||||
centered as discussed above. For the sake of simplicity we skip the
|
||||
overline symbol. The matrix is defined in terms of the various column
|
||||
vectors \( [\boldsymbol{x}_0,\boldsymbol{x}_1,\dots, \boldsymbol{x}_{p-1}] \) each with dimension
|
||||
\( \boldsymbol{x}\in {\mathbb{R}}^{n} \).
|
||||
|
||||
<p>
|
||||
We assume also that we have an orthogonal transformation \( \boldsymbol{W}\in {\mathbb{R}}^{p\times p} \). We define the reconstruction error (which is similar to the mean squared error we have seen before) as
|
||||
We have that the covariance matrix (the correlation matrix involves a simple rescaling) is given as
|
||||
$$
|
||||
J(\boldsymbol{W},\boldsymbol{Z}) = \frac{1}{n}\sum_i (\boldsymbol{x}_i - \overline{\boldsymbol{x}}_i)^2,
|
||||
\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}\boldsymbol{X}^T= \mathbb{E}[\boldsymbol{X}\boldsymbol{X}^T].
|
||||
$$
|
||||
|
||||
with \( \overline{\boldsymbol{x}}_i = \boldsymbol{W}\boldsymbol{z}_i \), where \( \boldsymbol{z}_i \) is a row vector with dimension \( {\mathbb{R}}^{n} \) of the matrix
|
||||
\( \boldsymbol{Z}\in{\mathbb{R}}^{p\times n} \). When doing PCA we want to reduce this dimensionality.
|
||||
Let us now assume that we can perform a series of orthogonal transformations where we employ some orthogonal matrices \( \boldsymbol{S} \).
|
||||
These matrices are defined as \( \boldsymbol{S}\in {\mathbb{R}}^{p\times p} \) and obey the orthogonality requirements \( \boldsymbol{S}\boldsymbol{S}^T=\boldsymbol{S}^T\boldsymbol{S}=\boldsymbol{I} \). The matrix can be written out in terms of the column vectors \( \boldsymbol{s}_i \) as \( \boldsymbol{S}=[\boldsymbol{s}_0,\boldsymbol{s}_1,\dots,\boldsymbol{s}_{p-1}] \) and \( \boldsymbol{s}_i \in {\mathbb{R}}^{p} \).
|
||||
|
||||
<p>
|
||||
The PCA theorem states that minimizing the above reconstruction error
|
||||
corresponds to setting \( \boldsymbol{W}=\boldsymbol{S} \), the orthogonal matrix which
|
||||
diagonalizes the empirical covariance(correlation) matrix. The optimal
|
||||
low-dimensional encoding of the data is then given by a set of vectors
|
||||
\( \boldsymbol{z}_i \) with at most \( l \) vectors, with \( l < < p \), defined by the
|
||||
orthogonal projection of the data onto the columns spanned by the
|
||||
eigenvectors of the covariance(correlations matrix).
|
||||
Assume also that there is a transformation \( \boldsymbol{S}\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}^T=\boldsymbol{C}[\boldsymbol{y}] \) such that the new matrix \( \boldsymbol{C}[\boldsymbol{y}] \) is diagonal with elements \( [\lambda_0,\lambda_1,\lambda_2,\dots,\lambda_{p-1}] \).
|
||||
|
||||
<p>
|
||||
The proof which follows will be updated by mid January 2020.
|
||||
That is we have
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{y}] = \mathbb{E}[\boldsymbol{S}\boldsymbol{X}\boldsymbol{X}^T\boldsymbol{S}^T]=\boldsymbol{S}\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}^T,
|
||||
$$
|
||||
|
||||
since the matrix \( \boldsymbol{S} \) is not a data dependent matrix. Multiplying with \( \boldsymbol{S}^T \) from the left we have
|
||||
$$
|
||||
\boldsymbol{S}^T\boldsymbol{C}[\boldsymbol{y}] = \boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}^T,
|
||||
$$
|
||||
|
||||
and since \( \boldsymbol{C}[\boldsymbol{y}] \) is diagonal we have for a given eigenvalue \( i \) of the covariance matrix that
|
||||
|
||||
$$
|
||||
\boldsymbol{S}^T_i\lambda_i = \boldsymbol{C}[\boldsymbol{x}]\boldsymbol{S}^T_i.
|
||||
$$
|
||||
|
||||
<p>
|
||||
In the derivation of the PCA theorem we will assume that the eigenvalues are ordered in descending order, that is
|
||||
\( \lambda_0 > \lambda_1 > \dots > \lambda_{p-1} \).
|
||||
|
||||
<p>
|
||||
The eigenvalues tell us then how much we need to stretch the
|
||||
corresponding eigenvectors. Dimensions with large eigenvalues have
|
||||
thus large variations (large variance) and define therefore useful
|
||||
dimensions. The data points are more spread out in the direction of
|
||||
these eigenvectors. Smaller eigenvalues mean on the other hand that
|
||||
the corresponding eigenvectors are shrunk accordingly and the data
|
||||
points are tightly bunched together and there is not much variation in
|
||||
these specific directions. Hopefully then we could leave it out
|
||||
dimensions where the eigenvalues are very small. If \( p \) is very large,
|
||||
we could then aim at reducing \( p \) to \( l < < p \) and handle only \( l \)
|
||||
features/predictors.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -236,7 +258,7 @@ The proof which follows will be updated by mid January 2020.
|
||||
<li><a href="._week43-bs021.html">22</a></li>
|
||||
<li><a href="._week43-bs022.html">23</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs014.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,27 +184,35 @@ MathJax.Hub.Config({
|
||||
<a name="part0014"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec16" class="anchor">Proof of the PCA Theorem </h2>
|
||||
<h2 id="___sec12" class="anchor">The Algorithm before theorem </h2>
|
||||
|
||||
<p>
|
||||
To show the PCA theorem let us start with the assumption that there is one vector \( \boldsymbol{w}_0 \) which corresponds to a solution which minimized the reconstruction error \( J \). This is an orthogonal vector. It means that we now approximate the reconstruction error in terms of \( \boldsymbol{w}_0 \) and \( \boldsymbol{z}_0 \) as
|
||||
Here's how we would proceed in setting up the algorithm for the PCA, see also discussion below here.
|
||||
|
||||
<ul>
|
||||
<li> Set up the datapoints for the design/feature matrix \( \boldsymbol{X} \) with \( \boldsymbol{X}\in {\mathbb{R}}^{n\times p} \), with the predictors/features \( p \) referring to the column numbers and the entries \( n \) being the row elements.</li>
|
||||
</ul>
|
||||
|
||||
$$
|
||||
J(\boldsymbol{w}_0,\boldsymbol{z}_0)= \frac{1}{n}\sum_i (\boldsymbol{x}_i - z_{i0}\boldsymbol{w}_0)^2=\frac{1}{n}\sum_i (\boldsymbol{x}_i^T\boldsymbol{x}_i - 2z_{i0}\boldsymbol{w}_0^T\boldsymbol{x}_i+z_{i0}^2\boldsymbol{w}_0^T\boldsymbol{w}_0),
|
||||
\boldsymbol{X}=\begin{bmatrix}
|
||||
x_{0,0} & x_{0,1} & x_{0,2}& \dots & \dots x_{0,p-1}\\
|
||||
x_{1,0} & x_{1,1} & x_{1,2}& \dots & \dots x_{1,p-1}\\
|
||||
x_{2,0} & x_{2,1} & x_{2,2}& \dots & \dots x_{2,p-1}\\
|
||||
\dots & \dots & \dots & \dots \dots & \dots \\
|
||||
x_{n-2,0} & x_{n-2,1} & x_{n-2,2}& \dots & \dots x_{n-2,p-1}\\
|
||||
x_{n-1,0} & x_{n-1,1} & x_{n-1,2}& \dots & \dots x_{n-1,p-1}\\
|
||||
\end{bmatrix},
|
||||
$$
|
||||
|
||||
which we can rewrite due to the orthogonality of \( \boldsymbol{w}_i \) as
|
||||
$$
|
||||
J(\boldsymbol{w}_0,\boldsymbol{z}_0)=\frac{1}{n}\sum_i (\boldsymbol{x}_i^T\boldsymbol{x}_i - 2z_{i0}\boldsymbol{w}_0^T\boldsymbol{x}_i+z_{i0}^2).
|
||||
$$
|
||||
|
||||
Minimizing \( J \) with respect to the unknown parameters \( z_{0i} \) we obtain that
|
||||
$$
|
||||
z_{i0}=\boldsymbol{w}_0^T\boldsymbol{x}_i,
|
||||
$$
|
||||
<ul>
|
||||
<li> Center the data by subtracting the mean value for each column. This leads to a new matrix \( \boldsymbol{X}\rightarrow \overline{\boldsymbol{X}} \).</li>
|
||||
<li> Compute then the covariance/correlation matrix \( \mathbb{E}[\overline{\boldsymbol{X}}\overline{\boldsymbol{X}}^T] \).</li>
|
||||
<li> Find the eigenpairs of \( \boldsymbol{C} \) with eigenvalues \( [\lambda_0,\lambda_1,\dots,\lambda_{p-1}] \) and eigenvectors \( [\boldsymbol{s}_0,\boldsymbol{s}_1,\dots,\boldsymbol{s}_{p-1}] \).</li>
|
||||
<li> Order the eigenvalue (and the eigenvectors accordingly) in order of decreasing eigenvalues.</li>
|
||||
<li> Keep only those \( l \) eigenvalues larger than a selected threshold value, discarding thus \( p-l \) features since we expect small variations in the data here.</li>
|
||||
</ul>
|
||||
|
||||
where the vectors on the rhs are known.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
@@ -226,7 +238,7 @@ where the vectors on the rhs are known.
|
||||
<li><a href="._week43-bs022.html">23</a></li>
|
||||
<li><a href="._week43-bs023.html">24</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs015.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,43 +184,178 @@ MathJax.Hub.Config({
|
||||
<a name="part0015"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec17" class="anchor">PCA Proof continued </h2>
|
||||
<h2 id="___sec13" class="anchor">Writing our own PCA code </h2>
|
||||
|
||||
<p>
|
||||
We have now found the unknown parameters \( z_{i0} \). These correspond to the projected coordinates and we can write
|
||||
We will use a simple example first with two-dimensional data
|
||||
drawn from a multivariate normal distribution with the following mean and covariance matrix:
|
||||
$$
|
||||
J(\boldsymbol{w}_0)= \frac{1}{p}\sum_i (\boldsymbol{x}_i^T\boldsymbol{x}_i - z_{i0}^2)=\mathrm{const}-\frac{1}{n}\sum_i z_{i0}^2.
|
||||
\mu = (-1,2) \qquad \Sigma = \begin{bmatrix} 4 & 2 \\
|
||||
2 & 2
|
||||
\end{bmatrix}
|
||||
$$
|
||||
|
||||
Note that the mean refers to each column of data.
|
||||
We will generate \( n = 1000 \) points \( X = \{ x_1, \ldots, x_N \} \) from
|
||||
this distribution, and store them in the \( 1000 \times 2 \) matrix \( \boldsymbol{X} \).
|
||||
|
||||
<p>
|
||||
The following Python code aids in setting up the data and writing out the design matrix.
|
||||
Note that the function <b>multivariate</b> returns also the covariance discussed above and that it is defined by dividing by \( n-1 \) instead of \( n \).
|
||||
<p>
|
||||
We can show that the variance of the projected coordinates defined by \( \boldsymbol{w}_0^T\boldsymbol{x}_i \) are given by
|
||||
$$
|
||||
\mathrm{var}[\boldsymbol{w}_0^T\boldsymbol{x}_i] = \frac{1}{n}\sum_i z_{i0}^2,
|
||||
$$
|
||||
|
||||
since the expectation value of
|
||||
$$
|
||||
\mathbb{E}[\boldsymbol{w}_0^T\boldsymbol{x}_i] = \mathbb{E}[z_{i0}]= \boldsymbol{w}_0^T\mathbb{E}[\boldsymbol{x}_i]=0,
|
||||
$$
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> display
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">10000</span>
|
||||
mean <span style="color: #666666">=</span> (<span style="color: #666666">-1</span>, <span style="color: #666666">2</span>)
|
||||
cov <span style="color: #666666">=</span> [[<span style="color: #666666">4</span>, <span style="color: #666666">2</span>], [<span style="color: #666666">2</span>, <span style="color: #666666">2</span>]]
|
||||
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>multivariate_normal(mean, cov, n)
|
||||
</pre></div>
|
||||
<p>
|
||||
Now we are going to implement the PCA algorithm. We will break it down into various substeps.
|
||||
|
||||
where we have used the fact that our data are centered.
|
||||
<h3 id="___sec14" class="anchor">Compute the sample mean and center the data </h3>
|
||||
|
||||
<p>
|
||||
Recalling our definition of the covariance as
|
||||
The first step of PCA is to compute the sample mean of the data and use it to center the data. Recall that the sample mean is
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}\boldsymbol{X}^T=\mathbb{E}[\boldsymbol{X}\boldsymbol{X}^T],
|
||||
\mu_n = \frac{1}{n} \sum_{i=1}^n x_i
|
||||
$$
|
||||
|
||||
we have thus that
|
||||
and the mean-centered data \( \bar{X} = \{ \bar{x}_1, \ldots, \bar{x}_n \} \) takes the form
|
||||
$$
|
||||
\mathrm{var}[\boldsymbol{w}_0^T\boldsymbol{x}_i] = \frac{1}{n}\sum_i z_{i0}^2=\boldsymbol{w}_0^T\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0.
|
||||
\bar{x}_i = x_i - \mu_n.
|
||||
$$
|
||||
|
||||
When you are done with these steps, print out \( \mu_n \) to verify it is
|
||||
close to \( \mu \) and plot your mean centered data to verify it is
|
||||
centered at the origin! Compare your code with the functionality from <b>Scikit-Learn</b> discussed above.
|
||||
The following code elements perform these operations using <b>pandas</b> or using our own functionality for doing so. The latter, using <b>numpy</b> is rather simple through the <b>mean()</b> function.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>df <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(X)
|
||||
<span style="color: #408080; font-style: italic"># Pandas does the centering for us</span>
|
||||
df <span style="color: #666666">=</span> df <span style="color: #666666">-</span>df<span style="color: #666666">.</span>mean()
|
||||
<span style="color: #408080; font-style: italic"># we center it ourselves</span>
|
||||
X_centered <span style="color: #666666">=</span> X <span style="color: #666666">-</span> X<span style="color: #666666">.</span>mean(axis<span style="color: #666666">=0</span>)
|
||||
</pre></div>
|
||||
<p>
|
||||
Alternatively, we could use the functions we discussed
|
||||
earlier for scaling the data set. That is, we could have used the
|
||||
<b>StandardScaler</b> function in <b>Scikit-Learn</b>, a function which ensures
|
||||
that for each feature/predictor we study the mean value is zero and
|
||||
the variance is one (every column in the design/feature matrix). You
|
||||
would then not get the same results, since we divide by the
|
||||
variance. The diagonal covariance matrix elements will then be one,
|
||||
while the non-diagonal ones need to be divided by \( 2\sqrt{2} \) for our
|
||||
specific case.
|
||||
|
||||
<h3 id="___sec15" class="anchor">Compute the sample covariance </h3>
|
||||
|
||||
<p>
|
||||
We are almost there, we have obtained a relation between minimizing
|
||||
the reconstruction error and the variance and the covariance
|
||||
matrix. Minimizing the error is equivalent to maximizing the variance
|
||||
of the projected data.
|
||||
Now we are going to use the mean centered data to compute the sample covariance of the data by using the following equation
|
||||
$$
|
||||
\begin{equation*}
|
||||
\Sigma_n = \frac{1}{n-1} \sum_{i=1}^n \bar{x}_i^T \bar{x}_i = \frac{1}{n-1} \sum_{i=1}^n (x_i - \mu_n)^T (x_i - \mu_n)
|
||||
\end{equation*}
|
||||
$$
|
||||
|
||||
where the data points \( x_i \in \mathbb{R}^p \) (here in this example \( p = 2 \)) are column vectors and \( x^T \) is the transpose of \( x \).
|
||||
We can write our own code or simply use either the functionaly of <b>numpy</b> or that of <b>pandas</b>, as follows
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000">print</span>(df<span style="color: #666666">.</span>cov())
|
||||
<span style="color: #008000">print</span>(np<span style="color: #666666">.</span>cov(X_centered<span style="color: #666666">.</span>T))
|
||||
</pre></div>
|
||||
<p>
|
||||
Note that the way we define the covariance matrix here has a factor \( n-1 \) instead of \( n \). This is included in the <b>cov()</b> function by <b>numpy</b> and <b>pandas</b>.
|
||||
Our own code here is not very elegant and asks for obvious improvements. It is tailored to this specific \( 2\times 2 \) covariance matrix.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># extract the relevant columns from the centered design matrix of dim n x 2</span>
|
||||
x <span style="color: #666666">=</span> X_centered[:,<span style="color: #666666">0</span>]
|
||||
y <span style="color: #666666">=</span> X_centered[:,<span style="color: #666666">1</span>]
|
||||
Cov <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #666666">2</span>,<span style="color: #666666">2</span>))
|
||||
Cov[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #666666">.</span>T<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>(n<span style="color: #666666">-1.0</span>)
|
||||
Cov[<span style="color: #666666">0</span>,<span style="color: #666666">0</span>] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #666666">.</span>T<span style="color: #AA22FF">@x</span>)<span style="color: #666666">/</span>(n<span style="color: #666666">-1.0</span>)
|
||||
Cov[<span style="color: #666666">1</span>,<span style="color: #666666">1</span>] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(y<span style="color: #666666">.</span>T<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>(n<span style="color: #666666">-1.0</span>)
|
||||
Cov[<span style="color: #666666">1</span>,<span style="color: #666666">0</span>]<span style="color: #666666">=</span> Cov[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>]
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Centered covariance using own code"</span>)
|
||||
<span style="color: #008000">print</span>(Cov)
|
||||
plt<span style="color: #666666">.</span>plot(x, y, <span style="color: #BA2121">'x'</span>)
|
||||
plt<span style="color: #666666">.</span>axis(<span style="color: #BA2121">'equal'</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
Depending on the number of points \( n \), we will get results that are close to the covariance values defined above.
|
||||
The plot shows how the data are clustered around a line with slope close to one. Is this expected?
|
||||
|
||||
<h3 id="___sec16" class="anchor">Diagonalize the sample covariance matrix to obtain the principal components </h3>
|
||||
|
||||
<p>
|
||||
Now we are ready to solve for the principal components! To do so we
|
||||
diagonalize the sample covariance matrix \( \Sigma \). We can use the
|
||||
function <b>np.linalg.eig</b> to do so. It will return the eigenvalues and
|
||||
eigenvectors of \( \Sigma \). Once we have these we can perform the
|
||||
following tasks:
|
||||
|
||||
<ul>
|
||||
<li> We compute the percentage of the total variance captured by the first principal component</li>
|
||||
<li> We plot the mean centered data and lines along the first and second principal components</li>
|
||||
<li> Then we project the mean centered data onto the first and second principal components, and plot the projected data.</li>
|
||||
<li> Finally, we approximate the data as</li>
|
||||
</ul>
|
||||
|
||||
$$
|
||||
\begin{equation*}
|
||||
x_i \approx \tilde{x}_i = \mu_n + \langle x_i, v_0 \rangle v_0
|
||||
\end{equation*}
|
||||
$$
|
||||
|
||||
where \( v_0 \) is the first principal component.
|
||||
|
||||
<p>
|
||||
Collecting all these steps we can write our own PCA function and
|
||||
compare this with the functionality included in <b>Scikit-Learn</b>.
|
||||
|
||||
<p>
|
||||
The code here outlines some of the elements we could include in the
|
||||
analysis. Feel free to extend upon this in order to address the above
|
||||
questions.
|
||||
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># diagonalize and obtain eigenvalues, not necessarily sorted</span>
|
||||
EigValues, EigVectors <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>eig(Cov)
|
||||
<span style="color: #408080; font-style: italic"># sort eigenvectors and eigenvalues</span>
|
||||
<span style="color: #408080; font-style: italic">#permute = EigValues.argsort()</span>
|
||||
<span style="color: #408080; font-style: italic">#EigValues = EigValues[permute]</span>
|
||||
<span style="color: #408080; font-style: italic">#EigVectors = EigVectors[:,permute]</span>
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Eigenvalues of Covariance matrix"</span>)
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">2</span>):
|
||||
<span style="color: #008000">print</span>(EigValues[i])
|
||||
FirstEigvector <span style="color: #666666">=</span> EigVectors[:,<span style="color: #666666">0</span>]
|
||||
SecondEigvector <span style="color: #666666">=</span> EigVectors[:,<span style="color: #666666">1</span>]
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"First eigenvector"</span>)
|
||||
<span style="color: #008000">print</span>(FirstEigvector)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Second eigenvector"</span>)
|
||||
<span style="color: #008000">print</span>(SecondEigvector)
|
||||
<span style="color: #408080; font-style: italic">#thereafter we do a PCA with Scikit-learn</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.decomposition</span> <span style="color: #008000; font-weight: bold">import</span> PCA
|
||||
pca <span style="color: #666666">=</span> PCA(n_components <span style="color: #666666">=</span> <span style="color: #666666">2</span>)
|
||||
X2Dsl <span style="color: #666666">=</span> pca<span style="color: #666666">.</span>fit_transform(X)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Eigenvector of largest eigenvalue"</span>)
|
||||
<span style="color: #008000">print</span>(pca<span style="color: #666666">.</span>components_<span style="color: #666666">.</span>T[:, <span style="color: #666666">0</span>])
|
||||
</pre></div>
|
||||
<p>
|
||||
This code does not contain all the above elements, but it shows how we can use <b>Scikit-Learn</b> to extract the eigenvector which corresponds to the largest eigenvalue. Try to address the questions we pose before the above code. Try also to change the values of the covariance matrix by making one of the diagonal elements much larger than the other. What do you observe then?
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -244,7 +383,7 @@ of the projected data.
|
||||
<li><a href="._week43-bs023.html">24</a></li>
|
||||
<li><a href="._week43-bs024.html">25</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs016.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,56 +184,35 @@ MathJax.Hub.Config({
|
||||
<a name="part0016"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec18" class="anchor">The final step </h2>
|
||||
<h2 id="___sec17" class="anchor">Classical PCA Theorem </h2>
|
||||
|
||||
<p>
|
||||
We could trivially maximize the variance of the projection (and
|
||||
thereby minimize the error in the reconstruction function) by letting
|
||||
the norm-2 of \( \boldsymbol{w}_0 \) go to infinity. However, this norm since we
|
||||
want the matrix \( \boldsymbol{W} \) to be an orthogonal matrix, is constrained by
|
||||
\( \vert\vert \boldsymbol{w}_0 \vert\vert_2^2=1 \). Imposing this condition via a
|
||||
Lagrange multiplier we can then in turn maximize
|
||||
|
||||
$$
|
||||
J(\boldsymbol{w}_0)= \boldsymbol{w}_0^T\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0+\lambda_0(1-\boldsymbol{w}_0^T\boldsymbol{w}_0).
|
||||
$$
|
||||
|
||||
Taking the derivative with respect to \( \boldsymbol{w}_0 \) we obtain
|
||||
|
||||
$$
|
||||
\frac{\partial J(\boldsymbol{w}_0)}{\partial \boldsymbol{w}_0}= 2\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0-2\lambda_0\boldsymbol{w}_0=0,
|
||||
$$
|
||||
|
||||
meaning that
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0=\lambda_0\boldsymbol{w}_0.
|
||||
$$
|
||||
|
||||
<b>The direction that maximizes the variance (or minimizes the construction error) is an eigenvector of the covariance matrix</b>! If we left multiply with \( \boldsymbol{w}_0^T \) we have the variance of the projected data is
|
||||
$$
|
||||
\boldsymbol{w}_0^T\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0=\lambda_0.
|
||||
$$
|
||||
We assume now that we have a design matrix \( \boldsymbol{X} \) which has been
|
||||
centered as discussed above. For the sake of simplicity we skip the
|
||||
overline symbol. The matrix is defined in terms of the various column
|
||||
vectors \( [\boldsymbol{x}_0,\boldsymbol{x}_1,\dots, \boldsymbol{x}_{p-1}] \) each with dimension
|
||||
\( \boldsymbol{x}\in {\mathbb{R}}^{n} \).
|
||||
|
||||
<p>
|
||||
If we want to maximize the variance (minimize the construction error)
|
||||
we simply pick the eigenvector of the covariance matrix with the
|
||||
largest eigenvalue. This establishes the link between the minimization
|
||||
of the reconstruction function \( J \) in terms of an orthogonal matrix
|
||||
and the maximization of the variance and thereby the covariance of our
|
||||
observations encoded in the design/feature matrix \( \boldsymbol{X} \).
|
||||
We assume also that we have an orthogonal transformation \( \boldsymbol{W}\in {\mathbb{R}}^{p\times p} \). We define the reconstruction error (which is similar to the mean squared error we have seen before) as
|
||||
$$
|
||||
J(\boldsymbol{W},\boldsymbol{Z}) = \frac{1}{n}\sum_i (\boldsymbol{x}_i - \overline{\boldsymbol{x}}_i)^2,
|
||||
$$
|
||||
|
||||
with \( \overline{\boldsymbol{x}}_i = \boldsymbol{W}\boldsymbol{z}_i \), where \( \boldsymbol{z}_i \) is a row vector with dimension \( {\mathbb{R}}^{n} \) of the matrix
|
||||
\( \boldsymbol{Z}\in{\mathbb{R}}^{p\times n} \). When doing PCA we want to reduce this dimensionality.
|
||||
|
||||
<p>
|
||||
The proof
|
||||
for the other eigenvectors \( \boldsymbol{w}_1,\boldsymbol{w}_2,\dots \) can be
|
||||
established by applying the above arguments and using the fact that
|
||||
our basis of eigenvectors is orthogonal, see <a href="https://mitpress.mit.edu/books/machine-learning-1" target="_self">Murphy chapter
|
||||
12.2</a>. The
|
||||
discussion in chapter 12.2 of Murphy's text has also a nice link with
|
||||
the Singular Value Decomposition theorem. For categorical data, see
|
||||
chapter 12.4 and discussion therein.
|
||||
The PCA theorem states that minimizing the above reconstruction error
|
||||
corresponds to setting \( \boldsymbol{W}=\boldsymbol{S} \), the orthogonal matrix which
|
||||
diagonalizes the empirical covariance(correlation) matrix. The optimal
|
||||
low-dimensional encoding of the data is then given by a set of vectors
|
||||
\( \boldsymbol{z}_i \) with at most \( l \) vectors, with \( l < < p \), defined by the
|
||||
orthogonal projection of the data onto the columns spanned by the
|
||||
eigenvectors of the covariance(correlations matrix).
|
||||
|
||||
<p>
|
||||
Additional part of the proof for the other eigenvectors will be added by mid January 2020.
|
||||
The proof which follows will be updated by mid January 2020.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -257,7 +240,7 @@ Additional part of the proof for the other eigenvectors will be added by mid Jan
|
||||
<li><a href="._week43-bs024.html">25</a></li>
|
||||
<li><a href="._week43-bs025.html">26</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs017.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,10 +184,25 @@ MathJax.Hub.Config({
|
||||
<a name="part0017"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec19" class="anchor">Geometric Interpretation and link with Singular Value Decomposition </h2>
|
||||
<h2 id="___sec18" class="anchor">Proof of the PCA Theorem </h2>
|
||||
|
||||
<p>
|
||||
This material will be added by mid January 2020.
|
||||
To show the PCA theorem let us start with the assumption that there is one vector \( \boldsymbol{w}_0 \) which corresponds to a solution which minimized the reconstruction error \( J \). This is an orthogonal vector. It means that we now approximate the reconstruction error in terms of \( \boldsymbol{w}_0 \) and \( \boldsymbol{z}_0 \) as
|
||||
$$
|
||||
J(\boldsymbol{w}_0,\boldsymbol{z}_0)= \frac{1}{n}\sum_i (\boldsymbol{x}_i - z_{i0}\boldsymbol{w}_0)^2=\frac{1}{n}\sum_i (\boldsymbol{x}_i^T\boldsymbol{x}_i - 2z_{i0}\boldsymbol{w}_0^T\boldsymbol{x}_i+z_{i0}^2\boldsymbol{w}_0^T\boldsymbol{w}_0),
|
||||
$$
|
||||
|
||||
which we can rewrite due to the orthogonality of \( \boldsymbol{w}_i \) as
|
||||
$$
|
||||
J(\boldsymbol{w}_0,\boldsymbol{z}_0)=\frac{1}{n}\sum_i (\boldsymbol{x}_i^T\boldsymbol{x}_i - 2z_{i0}\boldsymbol{w}_0^T\boldsymbol{x}_i+z_{i0}^2).
|
||||
$$
|
||||
|
||||
Minimizing \( J \) with respect to the unknown parameters \( z_{0i} \) we obtain that
|
||||
$$
|
||||
z_{i0}=\boldsymbol{w}_0^T\boldsymbol{x}_i,
|
||||
$$
|
||||
|
||||
where the vectors on the rhs are known.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -210,6 +229,8 @@ This material will be added by mid January 2020.
|
||||
<li><a href="._week43-bs024.html">25</a></li>
|
||||
<li><a href="._week43-bs025.html">26</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs018.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,58 +184,44 @@ MathJax.Hub.Config({
|
||||
<a name="part0018"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec20" class="anchor">Principal Component Analysis </h2>
|
||||
<h2 id="___sec19" class="anchor">PCA Proof continued </h2>
|
||||
|
||||
<p>
|
||||
Principal Component Analysis (PCA) is by far the most popular dimensionality reduction algorithm.
|
||||
First it identifies the hyperplane that lies closest to the data, and then it projects the data onto it.
|
||||
We have now found the unknown parameters \( z_{i0} \). These correspond to the projected coordinates and we can write
|
||||
$$
|
||||
J(\boldsymbol{w}_0)= \frac{1}{p}\sum_i (\boldsymbol{x}_i^T\boldsymbol{x}_i - z_{i0}^2)=\mathrm{const}-\frac{1}{n}\sum_i z_{i0}^2.
|
||||
$$
|
||||
|
||||
<p>
|
||||
The following Python code uses NumPy’s <b>svd()</b> function to obtain all the principal components of the
|
||||
training set, then extracts the first two principal components. First we center the data using either <b>pandas</b> or our own code
|
||||
<p>
|
||||
We can show that the variance of the projected coordinates defined by \( \boldsymbol{w}_0^T\boldsymbol{x}_i \) are given by
|
||||
$$
|
||||
\mathrm{var}[\boldsymbol{w}_0^T\boldsymbol{x}_i] = \frac{1}{n}\sum_i z_{i0}^2,
|
||||
$$
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> display
|
||||
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">100</span>)
|
||||
<span style="color: #408080; font-style: italic"># setting up a 10 x 5 vanilla matrix </span>
|
||||
rows <span style="color: #666666">=</span> <span style="color: #666666">10</span>
|
||||
cols <span style="color: #666666">=</span> <span style="color: #666666">5</span>
|
||||
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(rows,cols)
|
||||
df <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(X)
|
||||
<span style="color: #408080; font-style: italic"># Pandas does the centering for us</span>
|
||||
df <span style="color: #666666">=</span> df <span style="color: #666666">-</span>df<span style="color: #666666">.</span>mean()
|
||||
display(df)
|
||||
since the expectation value of
|
||||
$$
|
||||
\mathbb{E}[\boldsymbol{w}_0^T\boldsymbol{x}_i] = \mathbb{E}[z_{i0}]= \boldsymbol{w}_0^T\mathbb{E}[\boldsymbol{x}_i]=0,
|
||||
$$
|
||||
|
||||
<span style="color: #408080; font-style: italic"># we center it ourselves</span>
|
||||
X_centered <span style="color: #666666">=</span> X <span style="color: #666666">-</span> X<span style="color: #666666">.</span>mean(axis<span style="color: #666666">=0</span>)
|
||||
<span style="color: #408080; font-style: italic"># Then check the difference between pandas and our own set up</span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X_centered<span style="color: #666666">-</span>df)
|
||||
<span style="color: #408080; font-style: italic">#Now we do an SVD</span>
|
||||
U, s, V <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>svd(X_centered)
|
||||
c1 <span style="color: #666666">=</span> V<span style="color: #666666">.</span>T[:, <span style="color: #666666">0</span>]
|
||||
c2 <span style="color: #666666">=</span> V<span style="color: #666666">.</span>T[:, <span style="color: #666666">1</span>]
|
||||
W2 <span style="color: #666666">=</span> V<span style="color: #666666">.</span>T[:, :<span style="color: #666666">2</span>]
|
||||
X2D <span style="color: #666666">=</span> X_centered<span style="color: #666666">.</span>dot(W2)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X2D)
|
||||
</pre></div>
|
||||
<p>
|
||||
PCA assumes that the dataset is centered around the origin. Scikit-Learn’s PCA classes take care of centering
|
||||
the data for you. However, if you implement PCA yourself (as in the preceding example), or if you use other libraries, don’t
|
||||
forget to center the data first.
|
||||
where we have used the fact that our data are centered.
|
||||
|
||||
<p>
|
||||
Once you have identified all the principal components, you can reduce the dimensionality of the dataset
|
||||
down to \( d \) dimensions by projecting it onto the hyperplane defined by the first \( d \) principal components.
|
||||
Selecting this hyperplane ensures that the projection will preserve as much variance as possible.
|
||||
<p>
|
||||
Recalling our definition of the covariance as
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{x}] = \frac{1}{n}\boldsymbol{X}\boldsymbol{X}^T=\mathbb{E}[\boldsymbol{X}\boldsymbol{X}^T],
|
||||
$$
|
||||
|
||||
we have thus that
|
||||
$$
|
||||
\mathrm{var}[\boldsymbol{w}_0^T\boldsymbol{x}_i] = \frac{1}{n}\sum_i z_{i0}^2=\boldsymbol{w}_0^T\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0.
|
||||
$$
|
||||
|
||||
<p>
|
||||
We are almost there, we have obtained a relation between minimizing
|
||||
the reconstruction error and the variance and the covariance
|
||||
matrix. Minimizing the error is equivalent to maximizing the variance
|
||||
of the projected data.
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>W2 <span style="color: #666666">=</span> V<span style="color: #666666">.</span>T[:, :<span style="color: #666666">2</span>]
|
||||
X2D <span style="color: #666666">=</span> X_centered<span style="color: #666666">.</span>dot(W2)
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -256,6 +246,9 @@ X2D <span style="color: #666666">=</span> X_centered<span style="color: #666666"
|
||||
<li><a href="._week43-bs024.html">25</a></li>
|
||||
<li><a href="._week43-bs025.html">26</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs027.html">28</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs019.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -178,36 +182,58 @@ MathJax.Hub.Config({
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
|
||||
<a name="part0019"></a>
|
||||
<!-- !split -->
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec21" class="anchor">PCA and scikit-learn </h2>
|
||||
<h2 id="___sec20" class="anchor">The final step </h2>
|
||||
|
||||
<p>
|
||||
Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
|
||||
following code applies PCA to reduce the dimensionality of the dataset down to two dimensions (note
|
||||
that it automatically takes care of centering the data):
|
||||
<p>
|
||||
We could trivially maximize the variance of the projection (and
|
||||
thereby minimize the error in the reconstruction function) by letting
|
||||
the norm-2 of \( \boldsymbol{w}_0 \) go to infinity. However, this norm since we
|
||||
want the matrix \( \boldsymbol{W} \) to be an orthogonal matrix, is constrained by
|
||||
\( \vert\vert \boldsymbol{w}_0 \vert\vert_2^2=1 \). Imposing this condition via a
|
||||
Lagrange multiplier we can then in turn maximize
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic">#thereafter we do a PCA with Scikit-learn</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.decomposition</span> <span style="color: #008000; font-weight: bold">import</span> PCA
|
||||
pca <span style="color: #666666">=</span> PCA(n_components <span style="color: #666666">=</span> <span style="color: #666666">2</span>)
|
||||
X2D <span style="color: #666666">=</span> pca<span style="color: #666666">.</span>fit_transform(X)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X2D)
|
||||
</pre></div>
|
||||
<p>
|
||||
After fitting the PCA transformer to the dataset, you can access the principal components using the
|
||||
components variable (note that it contains the PCs as horizontal vectors, so, for example, the first
|
||||
principal component is equal to
|
||||
<p>
|
||||
$$
|
||||
J(\boldsymbol{w}_0)= \boldsymbol{w}_0^T\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0+\lambda_0(1-\boldsymbol{w}_0^T\boldsymbol{w}_0).
|
||||
$$
|
||||
|
||||
Taking the derivative with respect to \( \boldsymbol{w}_0 \) we obtain
|
||||
|
||||
$$
|
||||
\frac{\partial J(\boldsymbol{w}_0)}{\partial \boldsymbol{w}_0}= 2\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0-2\lambda_0\boldsymbol{w}_0=0,
|
||||
$$
|
||||
|
||||
meaning that
|
||||
$$
|
||||
\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0=\lambda_0\boldsymbol{w}_0.
|
||||
$$
|
||||
|
||||
<b>The direction that maximizes the variance (or minimizes the construction error) is an eigenvector of the covariance matrix</b>! If we left multiply with \( \boldsymbol{w}_0^T \) we have the variance of the projected data is
|
||||
$$
|
||||
\boldsymbol{w}_0^T\boldsymbol{C}[\boldsymbol{x}]\boldsymbol{w}_0=\lambda_0.
|
||||
$$
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>pca<span style="color: #666666">.</span>components_<span style="color: #666666">.</span>T[:, <span style="color: #666666">0</span>]<span style="color: #666666">.</span>
|
||||
</pre></div>
|
||||
<p>
|
||||
Another very useful piece of information is the explained variance ratio of each principal component,
|
||||
available via the \( explained\_variance\_ratio \) variable. It indicates the proportion of the dataset’s
|
||||
variance that lies along the axis of each principal component.
|
||||
If we want to maximize the variance (minimize the construction error)
|
||||
we simply pick the eigenvector of the covariance matrix with the
|
||||
largest eigenvalue. This establishes the link between the minimization
|
||||
of the reconstruction function \( J \) in terms of an orthogonal matrix
|
||||
and the maximization of the variance and thereby the covariance of our
|
||||
observations encoded in the design/feature matrix \( \boldsymbol{X} \).
|
||||
|
||||
<p>
|
||||
The proof
|
||||
for the other eigenvectors \( \boldsymbol{w}_1,\boldsymbol{w}_2,\dots \) can be
|
||||
established by applying the above arguments and using the fact that
|
||||
our basis of eigenvectors is orthogonal, see <a href="https://mitpress.mit.edu/books/machine-learning-1" target="_self">Murphy chapter
|
||||
12.2</a>. The
|
||||
discussion in chapter 12.2 of Murphy's text has also a nice link with
|
||||
the Singular Value Decomposition theorem. For categorical data, see
|
||||
chapter 12.4 and discussion therein.
|
||||
|
||||
<p>
|
||||
Additional part of the proof for the other eigenvectors will be added by mid January 2020.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -232,6 +258,10 @@ variance that lies along the axis of each principal component.
|
||||
<li><a href="._week43-bs024.html">25</a></li>
|
||||
<li><a href="._week43-bs025.html">26</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs027.html">28</a></li>
|
||||
<li><a href="._week43-bs028.html">29</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs020.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,43 +184,10 @@ MathJax.Hub.Config({
|
||||
<a name="part0020"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec22" class="anchor">Back to the Cancer Data </h2>
|
||||
We can now repeat the above but applied to real data, in this case our breast cancer data.
|
||||
Here we compute performance scores on the training data using logistic regression.
|
||||
<h2 id="___sec21" class="anchor">Geometric Interpretation and link with Singular Value Decomposition </h2>
|
||||
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
|
||||
cancer <span style="color: #666666">=</span> load_breast_cancer()
|
||||
|
||||
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(cancer<span style="color: #666666">.</span>data,cancer<span style="color: #666666">.</span>target,random_state<span style="color: #666666">=0</span>)
|
||||
|
||||
logreg <span style="color: #666666">=</span> LogisticRegression()
|
||||
logreg<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Train set accuracy from Logistic Regression: {:.2f}"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_train,y_train)))
|
||||
<span style="color: #408080; font-style: italic"># We scale the data</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
|
||||
scaler <span style="color: #666666">=</span> StandardScaler()
|
||||
scaler<span style="color: #666666">.</span>fit(X_train)
|
||||
X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_train)
|
||||
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
|
||||
<span style="color: #408080; font-style: italic"># Then perform again a log reg fit</span>
|
||||
logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Train set accuracy scaled data: {:.2f}"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_train_scaled,y_train)))
|
||||
<span style="color: #408080; font-style: italic">#thereafter we do a PCA with Scikit-learn</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.decomposition</span> <span style="color: #008000; font-weight: bold">import</span> PCA
|
||||
pca <span style="color: #666666">=</span> PCA(n_components <span style="color: #666666">=</span> <span style="color: #666666">2</span>)
|
||||
X2D_train <span style="color: #666666">=</span> pca<span style="color: #666666">.</span>fit_transform(X_train_scaled)
|
||||
<span style="color: #408080; font-style: italic"># and finally compute the log reg fit and the score on the training data </span>
|
||||
logreg<span style="color: #666666">.</span>fit(X2D_train,y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Train set accuracy scaled and PCA data: {:.2f}"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X2D_train,y_train)))
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that our training data after the PCA decomposition has a performance similar to the non-scaled data.
|
||||
This material will be added by mid January 2020.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -240,6 +211,9 @@ We see that our training data after the PCA decomposition has a performance simi
|
||||
<li><a href="._week43-bs024.html">25</a></li>
|
||||
<li><a href="._week43-bs025.html">26</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs027.html">28</a></li>
|
||||
<li><a href="._week43-bs028.html">29</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs021.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,32 +184,57 @@ MathJax.Hub.Config({
|
||||
<a name="part0021"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec23" class="anchor">More on the PCA </h2>
|
||||
<h2 id="___sec22" class="anchor">Principal Component Analysis </h2>
|
||||
|
||||
<p>
|
||||
Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to
|
||||
choose the number of dimensions that add up to a sufficiently large portion of the variance (e.g., 95%).
|
||||
Unless, of course, you are reducing dimensionality for data visualization — in that case you will
|
||||
generally want to reduce the dimensionality down to 2 or 3.
|
||||
The following code computes PCA without reducing dimensionality, then computes the minimum number
|
||||
of dimensions required to preserve 95% of the training set’s variance:
|
||||
Principal Component Analysis (PCA) is by far the most popular dimensionality reduction algorithm.
|
||||
First it identifies the hyperplane that lies closest to the data, and then it projects the data onto it.
|
||||
|
||||
<p>
|
||||
The following Python code uses NumPy’s <b>svd()</b> function to obtain all the principal components of the
|
||||
training set, then extracts the first two principal components. First we center the data using either <b>pandas</b> or our own code
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>pca <span style="color: #666666">=</span> PCA()
|
||||
pca<span style="color: #666666">.</span>fit(X)
|
||||
cumsum <span style="color: #666666">=</span> np<span style="color: #666666">.</span>cumsum(pca<span style="color: #666666">.</span>explained_variance_ratio_)
|
||||
d <span style="color: #666666">=</span> np<span style="color: #666666">.</span>argmax(cumsum <span style="color: #666666">>=</span> <span style="color: #666666">0.95</span>) <span style="color: #666666">+</span> <span style="color: #666666">1</span>
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">IPython.display</span> <span style="color: #008000; font-weight: bold">import</span> display
|
||||
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">100</span>)
|
||||
<span style="color: #408080; font-style: italic"># setting up a 10 x 5 vanilla matrix </span>
|
||||
rows <span style="color: #666666">=</span> <span style="color: #666666">10</span>
|
||||
cols <span style="color: #666666">=</span> <span style="color: #666666">5</span>
|
||||
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(rows,cols)
|
||||
df <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(X)
|
||||
<span style="color: #408080; font-style: italic"># Pandas does the centering for us</span>
|
||||
df <span style="color: #666666">=</span> df <span style="color: #666666">-</span>df<span style="color: #666666">.</span>mean()
|
||||
display(df)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># we center it ourselves</span>
|
||||
X_centered <span style="color: #666666">=</span> X <span style="color: #666666">-</span> X<span style="color: #666666">.</span>mean(axis<span style="color: #666666">=0</span>)
|
||||
<span style="color: #408080; font-style: italic"># Then check the difference between pandas and our own set up</span>
|
||||
<span style="color: #008000">print</span>(X_centered<span style="color: #666666">-</span>df)
|
||||
<span style="color: #408080; font-style: italic">#Now we do an SVD</span>
|
||||
U, s, V <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>svd(X_centered)
|
||||
c1 <span style="color: #666666">=</span> V<span style="color: #666666">.</span>T[:, <span style="color: #666666">0</span>]
|
||||
c2 <span style="color: #666666">=</span> V<span style="color: #666666">.</span>T[:, <span style="color: #666666">1</span>]
|
||||
W2 <span style="color: #666666">=</span> V<span style="color: #666666">.</span>T[:, :<span style="color: #666666">2</span>]
|
||||
X2D <span style="color: #666666">=</span> X_centered<span style="color: #666666">.</span>dot(W2)
|
||||
<span style="color: #008000">print</span>(X2D)
|
||||
</pre></div>
|
||||
<p>
|
||||
You could then set \( n\_components=d \) and run PCA again. However, there is a much better option: instead
|
||||
of specifying the number of principal components you want to preserve, you can set \( n\_components \) to be
|
||||
a float between 0.0 and 1.0, indicating the ratio of variance you wish to preserve:
|
||||
PCA assumes that the dataset is centered around the origin. Scikit-Learn’s PCA classes take care of centering
|
||||
the data for you. However, if you implement PCA yourself (as in the preceding example), or if you use other libraries, don’t
|
||||
forget to center the data first.
|
||||
|
||||
<p>
|
||||
Once you have identified all the principal components, you can reduce the dimensionality of the dataset
|
||||
down to \( d \) dimensions by projecting it onto the hyperplane defined by the first \( d \) principal components.
|
||||
Selecting this hyperplane ensures that the projection will preserve as much variance as possible.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>pca <span style="color: #666666">=</span> PCA(n_components<span style="color: #666666">=0.95</span>)
|
||||
X_reduced <span style="color: #666666">=</span> pca<span style="color: #666666">.</span>fit_transform(X)
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>W2 <span style="color: #666666">=</span> V<span style="color: #666666">.</span>T[:, :<span style="color: #666666">2</span>]
|
||||
X2D <span style="color: #666666">=</span> X_centered<span style="color: #666666">.</span>dot(W2)
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
@@ -228,6 +257,9 @@ X_reduced <span style="color: #666666">=</span> pca<span style="color: #666666">
|
||||
<li><a href="._week43-bs024.html">25</a></li>
|
||||
<li><a href="._week43-bs025.html">26</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs027.html">28</a></li>
|
||||
<li><a href="._week43-bs028.html">29</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs022.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -178,16 +182,36 @@ MathJax.Hub.Config({
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
|
||||
<a name="part0022"></a>
|
||||
<!-- !split -->
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec24" class="anchor">Incremental PCA </h2>
|
||||
<h2 id="___sec23" class="anchor">PCA and scikit-learn </h2>
|
||||
|
||||
<p>
|
||||
One problem with the preceding implementation of PCA is that it requires the whole training set to fit in
|
||||
memory in order for the SVD algorithm to run. Fortunately, Incremental PCA (IPCA) algorithms have
|
||||
been developed: you can split the training set into mini-batches and feed an IPCA algorithm one minibatch
|
||||
at a time. This is useful for large training sets, and also to apply PCA online (i.e., on the fly, as new
|
||||
instances arrive).
|
||||
Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
|
||||
following code applies PCA to reduce the dimensionality of the dataset down to two dimensions (note
|
||||
that it automatically takes care of centering the data):
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic">#thereafter we do a PCA with Scikit-learn</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.decomposition</span> <span style="color: #008000; font-weight: bold">import</span> PCA
|
||||
pca <span style="color: #666666">=</span> PCA(n_components <span style="color: #666666">=</span> <span style="color: #666666">2</span>)
|
||||
X2D <span style="color: #666666">=</span> pca<span style="color: #666666">.</span>fit_transform(X)
|
||||
<span style="color: #008000">print</span>(X2D)
|
||||
</pre></div>
|
||||
<p>
|
||||
After fitting the PCA transformer to the dataset, you can access the principal components using the
|
||||
components variable (note that it contains the PCs as horizontal vectors, so, for example, the first
|
||||
principal component is equal to
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>pca<span style="color: #666666">.</span>components_<span style="color: #666666">.</span>T[:, <span style="color: #666666">0</span>]<span style="color: #666666">.</span>
|
||||
</pre></div>
|
||||
<p>
|
||||
Another very useful piece of information is the explained variance ratio of each principal component,
|
||||
available via the \( explained\_variance\_ratio \) variable. It indicates the proportion of the dataset’s
|
||||
variance that lies along the axis of each principal component.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -209,6 +233,9 @@ instances arrive).
|
||||
<li><a href="._week43-bs024.html">25</a></li>
|
||||
<li><a href="._week43-bs025.html">26</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs027.html">28</a></li>
|
||||
<li><a href="._week43-bs028.html">29</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs023.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,13 +184,43 @@ MathJax.Hub.Config({
|
||||
<a name="part0023"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec25" class="anchor">Randomized PCA </h2>
|
||||
|
||||
<h2 id="___sec24" class="anchor">Back to the Cancer Data </h2>
|
||||
We can now repeat the above but applied to real data, in this case our breast cancer data.
|
||||
Here we compute performance scores on the training data using logistic regression.
|
||||
<p>
|
||||
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
|
||||
algorithm that quickly finds an approximation of the first d principal components. Its computational
|
||||
complexity is \( O(m \times d^2)+O(d^3) \), instead of \( O(m \times n^2) + O(n^3) \), so it is dramatically faster than the
|
||||
previous algorithms when \( d \) is much smaller than \( n \).
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
|
||||
cancer <span style="color: #666666">=</span> load_breast_cancer()
|
||||
|
||||
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(cancer<span style="color: #666666">.</span>data,cancer<span style="color: #666666">.</span>target,random_state<span style="color: #666666">=0</span>)
|
||||
|
||||
logreg <span style="color: #666666">=</span> LogisticRegression()
|
||||
logreg<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Train set accuracy from Logistic Regression: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_train,y_train)))
|
||||
<span style="color: #408080; font-style: italic"># We scale the data</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
|
||||
scaler <span style="color: #666666">=</span> StandardScaler()
|
||||
scaler<span style="color: #666666">.</span>fit(X_train)
|
||||
X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_train)
|
||||
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
|
||||
<span style="color: #408080; font-style: italic"># Then perform again a log reg fit</span>
|
||||
logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Train set accuracy scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_train_scaled,y_train)))
|
||||
<span style="color: #408080; font-style: italic">#thereafter we do a PCA with Scikit-learn</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.decomposition</span> <span style="color: #008000; font-weight: bold">import</span> PCA
|
||||
pca <span style="color: #666666">=</span> PCA(n_components <span style="color: #666666">=</span> <span style="color: #666666">2</span>)
|
||||
X2D_train <span style="color: #666666">=</span> pca<span style="color: #666666">.</span>fit_transform(X_train_scaled)
|
||||
<span style="color: #408080; font-style: italic"># and finally compute the log reg fit and the score on the training data </span>
|
||||
logreg<span style="color: #666666">.</span>fit(X2D_train,y_train)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Train set accuracy scaled and PCA data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X2D_train,y_train)))
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that our training data after the PCA decomposition has a performance similar to the non-scaled data.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -207,6 +241,9 @@ previous algorithms when \( d \) is much smaller than \( n \).
|
||||
<li><a href="._week43-bs024.html">25</a></li>
|
||||
<li><a href="._week43-bs025.html">26</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs027.html">28</a></li>
|
||||
<li><a href="._week43-bs028.html">29</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs024.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,33 +184,33 @@ MathJax.Hub.Config({
|
||||
<a name="part0024"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec26" class="anchor">Kernel PCA </h2>
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<p> <!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
|
||||
<h2 id="___sec25" class="anchor">More on the PCA </h2>
|
||||
|
||||
<p>
|
||||
The kernel trick is a mathematical technique that implicitly maps instances into a
|
||||
very high-dimensional space (called the feature space), enabling nonlinear classification and regression
|
||||
with Support Vector Machines. Recall that a linear decision boundary in the high-dimensional feature
|
||||
space corresponds to a complex nonlinear decision boundary in the original space.
|
||||
It turns out that the same trick can be applied to PCA, making it possible to perform complex nonlinear
|
||||
projections for dimensionality reduction. This is called Kernel PCA (kPCA). It is often good at
|
||||
preserving clusters of instances after projection, or sometimes even unrolling datasets that lie close to a
|
||||
twisted manifold.
|
||||
For example, the following code uses Scikit-Learn’s KernelPCA class to perform kPCA with an
|
||||
Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to
|
||||
choose the number of dimensions that add up to a sufficiently large portion of the variance (e.g., 95%).
|
||||
Unless, of course, you are reducing dimensionality for data visualization — in that case you will
|
||||
generally want to reduce the dimensionality down to 2 or 3.
|
||||
The following code computes PCA without reducing dimensionality, then computes the minimum number
|
||||
of dimensions required to preserve 95% of the training set’s variance:
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.decomposition</span> <span style="color: #008000; font-weight: bold">import</span> KernelPCA
|
||||
rbf_pca <span style="color: #666666">=</span> KernelPCA(n_components <span style="color: #666666">=</span> <span style="color: #666666">2</span>, kernel<span style="color: #666666">=</span><span style="color: #BA2121">"rbf"</span>, gamma<span style="color: #666666">=0.04</span>)
|
||||
X_reduced <span style="color: #666666">=</span> rbf_pca<span style="color: #666666">.</span>fit_transform(X)
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>pca <span style="color: #666666">=</span> PCA()
|
||||
pca<span style="color: #666666">.</span>fit(X)
|
||||
cumsum <span style="color: #666666">=</span> np<span style="color: #666666">.</span>cumsum(pca<span style="color: #666666">.</span>explained_variance_ratio_)
|
||||
d <span style="color: #666666">=</span> np<span style="color: #666666">.</span>argmax(cumsum <span style="color: #666666">>=</span> <span style="color: #666666">0.95</span>) <span style="color: #666666">+</span> <span style="color: #666666">1</span>
|
||||
</pre></div>
|
||||
<p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
You could then set \( n\_components=d \) and run PCA again. However, there is a much better option: instead
|
||||
of specifying the number of principal components you want to preserve, you can set \( n\_components \) to be
|
||||
a float between 0.0 and 1.0, indicating the ratio of variance you wish to preserve:
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>pca <span style="color: #666666">=</span> PCA(n_components<span style="color: #666666">=0.95</span>)
|
||||
X_reduced <span style="color: #666666">=</span> pca<span style="color: #666666">.</span>fit_transform(X)
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -225,6 +229,9 @@ X_reduced <span style="color: #666666">=</span> rbf_pca<span style="color: #6666
|
||||
<li class="active"><a href="._week43-bs024.html">25</a></li>
|
||||
<li><a href="._week43-bs025.html">26</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs027.html">28</a></li>
|
||||
<li><a href="._week43-bs028.html">29</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs025.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,14 +184,14 @@ MathJax.Hub.Config({
|
||||
<a name="part0025"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec27" class="anchor">LLE </h2>
|
||||
<h2 id="___sec26" class="anchor">Incremental PCA </h2>
|
||||
|
||||
<p>
|
||||
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
|
||||
(NLDR) technique. It is a Manifold Learning technique that does not rely on projections like the previous
|
||||
algorithms. In a nutshell, LLE works by first measuring how each training instance linearly relates to its
|
||||
closest neighbors (c.n.), and then looking for a low-dimensional representation of the training set where
|
||||
these local relationships are best preserved (more details shortly).
|
||||
One problem with the preceding implementation of PCA is that it requires the whole training set to fit in
|
||||
memory in order for the SVD algorithm to run. Fortunately, Incremental PCA (IPCA) algorithms have
|
||||
been developed: you can split the training set into mini-batches and feed an IPCA algorithm one minibatch
|
||||
at a time. This is useful for large training sets, and also to apply PCA online (i.e., on the fly, as new
|
||||
instances arrive).
|
||||
|
||||
<p>
|
||||
<p>
|
||||
@@ -206,6 +210,9 @@ these local relationships are best preserved (more details shortly).
|
||||
<li><a href="._week43-bs024.html">25</a></li>
|
||||
<li class="active"><a href="._week43-bs025.html">26</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs027.html">28</a></li>
|
||||
<li><a href="._week43-bs028.html">29</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs026.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -180,22 +184,15 @@ MathJax.Hub.Config({
|
||||
<a name="part0026"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec28" class="anchor">Other techniques </h2>
|
||||
<h2 id="___sec27" class="anchor">Randomized PCA </h2>
|
||||
|
||||
<p>
|
||||
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
|
||||
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
|
||||
algorithm that quickly finds an approximation of the first d principal components. Its computational
|
||||
complexity is \( O(m \times d^2)+O(d^3) \), instead of \( O(m \times n^2) + O(n^3) \), so it is dramatically faster than the
|
||||
previous algorithms when \( d \) is much smaller than \( n \).
|
||||
|
||||
<p>
|
||||
Here are some of the most popular:
|
||||
|
||||
<ul>
|
||||
<li> <b>Multidimensional Scaling (MDS)</b> reduces dimensionality while trying to preserve the distances between the instances.</li>
|
||||
<li> <b>Isomap</b> creates a graph by connecting each instance to its nearest neighbors, then reduces dimensionality while trying to preserve the geodesic distances between the instances.</li>
|
||||
<li> <b>t-Distributed Stochastic Neighbor Embedding</b> (t-SNE) reduces dimensionality while trying to keep similar instances close and dissimilar instances apart. It is mostly used for visualization, in particular to visualize clusters of instances in high-dimensional space (e.g., to visualize the MNIST images in 2D).</li>
|
||||
<li> Linear Discriminant Analysis (LDA) is actually a classification algorithm, but during training it learns the most discriminative axes between the classes, and these axes can then be used to define a hyperplane onto which to project the data. The benefit is that the projection will keep classes as far apart as possible, so LDA is a good technique to reduce dimensionality before running another classification algorithm such as a Support Vector Machine (SVM) classifier discussed in the SVM lectures.</li>
|
||||
</ul>
|
||||
|
||||
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
@@ -211,6 +208,10 @@ Here are some of the most popular:
|
||||
<li><a href="._week43-bs024.html">25</a></li>
|
||||
<li><a href="._week43-bs025.html">26</a></li>
|
||||
<li class="active"><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs027.html">28</a></li>
|
||||
<li><a href="._week43-bs028.html">29</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs027.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
@@ -41,64 +41,66 @@ Automatically generated HTML file from DocOnce source
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -128,7 +130,7 @@ MathJax.Hub.Config({
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Dimensionality Reduction</a>
|
||||
<a class="navbar-brand" href="week43-bs.html">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</a>
|
||||
</div>
|
||||
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
@@ -136,35 +138,37 @@ MathJax.Hub.Config({
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs001.html#___sec0" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec1" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec2" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec3" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec4" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec5" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec6" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec8" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec9" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec10" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec11" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec12" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec13" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec14" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec15" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec16" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec17" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec18" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec19" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec20" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec21" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec22" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec23" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec24" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec25" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec26" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec27" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec28" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs002.html#___sec0" style="font-size: 80%;"><b>Recurrent Neural Networks</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs003.html#___sec1" style="font-size: 80%;"><b>Solving ODEs with Deep Learning</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs004.html#___sec2" style="font-size: 80%;"><b>Why should we think of reducing the dimensionality</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs005.html#___sec3" style="font-size: 80%;"><b>Basic ideas of the Principal Component Analysis (PCA)</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs006.html#___sec4" style="font-size: 80%;"><b>Introducing the Covariance and Correlation functions</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs007.html#___sec5" style="font-size: 80%;"><b>Correlation Function and Design/Feature Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs008.html#___sec6" style="font-size: 80%;"><b>Covariance Matrix Examples</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs009.html#___sec7" style="font-size: 80%;"><b>Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs010.html#___sec8" style="font-size: 80%;"><b>Correlation Matrix with Pandas</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs011.html#___sec9" style="font-size: 80%;"><b>Correlation Matrix with Pandas and the Franke function</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs012.html#___sec10" style="font-size: 80%;"><b>Rewriting the Covariance and/or Correlation Matrix</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs013.html#___sec11" style="font-size: 80%;"><b>Towards the PCA theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs014.html#___sec12" style="font-size: 80%;"><b>The Algorithm before the Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec13" style="font-size: 80%;"><b>Writing our own PCA code</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec14" style="font-size: 80%;"> Compute the sample mean and center the data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec15" style="font-size: 80%;"> Compute the sample covariance</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs015.html#___sec16" style="font-size: 80%;"> Diagonalize the sample covariance matrix to obtain the principal components</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs016.html#___sec17" style="font-size: 80%;"><b>Classical PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs017.html#___sec18" style="font-size: 80%;"><b>Proof of the PCA Theorem</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs018.html#___sec19" style="font-size: 80%;"><b>PCA Proof continued</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs019.html#___sec20" style="font-size: 80%;"><b>The final step</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs020.html#___sec21" style="font-size: 80%;"><b>Geometric Interpretation and link with Singular Value Decomposition</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs021.html#___sec22" style="font-size: 80%;"><b>Principal Component Analysis</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs022.html#___sec23" style="font-size: 80%;"><b>PCA and scikit-learn</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs023.html#___sec24" style="font-size: 80%;"><b>Back to the Cancer Data</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs024.html#___sec25" style="font-size: 80%;"><b>More on the PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs025.html#___sec26" style="font-size: 80%;"><b>Incremental PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs026.html#___sec27" style="font-size: 80%;"><b>Randomized PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs027.html#___sec28" style="font-size: 80%;"><b>Kernel PCA</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs028.html#___sec29" style="font-size: 80%;"><b>LLE</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week43-bs029.html#___sec30" style="font-size: 80%;"><b>Other techniques</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -183,7 +187,7 @@ MathJax.Hub.Config({
|
||||
|
||||
|
||||
<div class="jumbotron">
|
||||
<center><h1>Week 43: Dimensionality Reduction</h1></center> <!-- document title -->
|
||||
<center><h1>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</h1></center> <!-- document title -->
|
||||
|
||||
<p>
|
||||
<!-- author(s): Morten Hjorth-Jensen -->
|
||||
@@ -199,7 +203,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Sep 16, 2020</h4></center> <!-- date -->
|
||||
<center><h4>Oct 17, 2020</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
@@ -223,7 +227,7 @@ MathJax.Hub.Config({
|
||||
<li><a href="._week43-bs008.html">9</a></li>
|
||||
<li><a href="._week43-bs009.html">10</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._week43-bs026.html">27</a></li>
|
||||
<li><a href="._week43-bs029.html">30</a></li>
|
||||
<li><a href="._week43-bs001.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
|
||||
|
||||
@@ -132,7 +132,7 @@ MathJax.Hub.Config({
|
||||
|
||||
|
||||
|
||||
<center><h1 style="text-align: center;">Week 43: Dimensionality Reduction</h1></center> <!-- document title -->
|
||||
<center><h1 style="text-align: center;">Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</h1></center> <!-- document title -->
|
||||
|
||||
<p>
|
||||
<!-- author(s): Morten Hjorth-Jensen -->
|
||||
@@ -148,7 +148,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p> <br>
|
||||
<center><h4>Sep 16, 2020</h4></center> <!-- date -->
|
||||
<center><h4>Oct 17, 2020</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
@@ -159,7 +159,29 @@ MathJax.Hub.Config({
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec0">Why should we think of reducing the dimensionality </h2>
|
||||
|
||||
<ul>
|
||||
<p><li> Thursday: Wrapping up Recurrent Neural Networks and solving differential equations.</li>
|
||||
<p><li> Friday: Principal Component Analysis and Dimensionality Reduction</li>
|
||||
</ul>
|
||||
<p>
|
||||
|
||||
Reading suggestions for both days: "Aurelien Geron's chapters 8
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec0">Recurrent Neural Networks </h2>
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec1">Solving ODEs with Deep Learning </h2>
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec2">Why should we think of reducing the dimensionality </h2>
|
||||
|
||||
<p>
|
||||
In addition to the plot of the features, we study now also the covariance (and the correlation matrix).
|
||||
@@ -198,12 +220,12 @@ plt.show()
|
||||
correlation_matrix = cancerpd.corr().round(<span style="color: #B452CD">1</span>)
|
||||
<span style="color: #228B22"># use the heatmap function from seaborn to plot the correlation matrix</span>
|
||||
<span style="color: #228B22"># annot = True to print the values inside the square</span>
|
||||
sns.heatmap(data=correlation_matrix, annot=<span style="color: #658b00">True</span>)
|
||||
sns.heatmap(data=correlation_matrix, annot=<span style="color: #8B008B; font-weight: bold">True</span>)
|
||||
plt.show()
|
||||
|
||||
<span style="color: #228B22">#print eigvalues of correlation matrix</span>
|
||||
EigValues, EigVectors = np.linalg.eig(correlation_matrix)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(EigValues)
|
||||
<span style="color: #658b00">print</span>(EigValues)
|
||||
</pre></div>
|
||||
<p>
|
||||
In the above example we note two things. In the first plot we display
|
||||
@@ -243,7 +265,7 @@ applications.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec1">Basic ideas of the Principal Component Analysis (PCA) </h2>
|
||||
<h2 id="___sec3">Basic ideas of the Principal Component Analysis (PCA) </h2>
|
||||
|
||||
<p>
|
||||
The principal component analysis deals with the problem of fitting a
|
||||
@@ -266,7 +288,7 @@ We have a data set defined by a design/feature matrix \( \boldsymbol{X} \) (see
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec2">Introducing the Covariance and Correlation functions </h2>
|
||||
<h2 id="___sec4">Introducing the Covariance and Correlation functions </h2>
|
||||
|
||||
<p>
|
||||
Before we discuss the PCA theorem, we need to remind ourselves about
|
||||
@@ -339,7 +361,7 @@ In the above example this is the function we constructed using <b>pandas</b>.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec3">Correlation Function and Design/Feature Matrix </h2>
|
||||
<h2 id="___sec5">Correlation Function and Design/Feature Matrix </h2>
|
||||
|
||||
<p>
|
||||
In our derivation of the various regression algorithms like <b>Ordinary Least Squares</b> or <b>Ridge regression</b>
|
||||
@@ -410,7 +432,7 @@ $$
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec4">Covariance Matrix Examples </h2>
|
||||
<h2 id="___sec6">Covariance Matrix Examples </h2>
|
||||
|
||||
<p>
|
||||
The Numpy function <b>np.cov</b> calculates the covariance elements using
|
||||
@@ -445,18 +467,18 @@ covariance matrix through the <b>np.linalg.eig()</b> function.
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
||||
n = <span style="color: #B452CD">100</span>
|
||||
x = np.random.normal(size=n)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(np.mean(x))
|
||||
<span style="color: #658b00">print</span>(np.mean(x))
|
||||
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.normal(size=n)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(np.mean(y))
|
||||
<span style="color: #658b00">print</span>(np.mean(y))
|
||||
W = np.vstack((x, y))
|
||||
C = np.cov(W)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(C)
|
||||
<span style="color: #658b00">print</span>(C)
|
||||
</pre></div>
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec5">Correlation Matrix </h2>
|
||||
<h2 id="___sec7">Correlation Matrix </h2>
|
||||
|
||||
<p>
|
||||
The previous example can be converted into the correlation matrix by
|
||||
@@ -478,8 +500,8 @@ x = x - np.mean(x)
|
||||
y = y - np.mean(y)
|
||||
variance_x = np.sum(x<span style="color: #707a7c">@x</span>)/n
|
||||
variance_y = np.sum(y<span style="color: #707a7c">@y</span>)/n
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(variance_x)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(variance_y)
|
||||
<span style="color: #658b00">print</span>(variance_x)
|
||||
<span style="color: #658b00">print</span>(variance_y)
|
||||
cov_xy = np.sum(x<span style="color: #707a7c">@y</span>)/n
|
||||
cov_xx = np.sum(x<span style="color: #707a7c">@x</span>)/n
|
||||
cov_yy = np.sum(y<span style="color: #707a7c">@y</span>)/n
|
||||
@@ -488,7 +510,7 @@ C[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>]=
|
||||
C[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>]= cov_yy/variance_y
|
||||
C[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>]= cov_xy/np.sqrt(variance_y*variance_x)
|
||||
C[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>]= C[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>]
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(C)
|
||||
<span style="color: #658b00">print</span>(C)
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that the matrix elements along the diagonal are one as they
|
||||
@@ -501,7 +523,7 @@ The above procedure with <b>numpy</b> can be made more compact if we use <b>pand
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec6">Correlation Matrix with Pandas </h2>
|
||||
<h2 id="___sec8">Correlation Matrix with Pandas </h2>
|
||||
|
||||
<p>
|
||||
We whow here how we can set up the correlation matrix using <b>pandas</b>, as done in this simple code
|
||||
@@ -516,11 +538,11 @@ x = x - np.mean(x)
|
||||
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.normal(size=n)
|
||||
y = y - np.mean(y)
|
||||
X = (np.vstack((x, y))).T
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(X)
|
||||
<span style="color: #658b00">print</span>(X)
|
||||
Xpd = pd.DataFrame(X)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(Xpd)
|
||||
<span style="color: #658b00">print</span>(Xpd)
|
||||
correlation_matrix = Xpd.corr()
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(correlation_matrix)
|
||||
<span style="color: #658b00">print</span>(correlation_matrix)
|
||||
</pre></div>
|
||||
<p>
|
||||
We expand this model to the Franke function discussed above.
|
||||
@@ -528,7 +550,7 @@ We expand this model to the Franke function discussed above.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec7">Correlation Matrix with Pandas and the Franke function </h2>
|
||||
<h2 id="___sec9">Correlation Matrix with Pandas and the Franke function </h2>
|
||||
|
||||
<p>
|
||||
|
||||
@@ -575,7 +597,7 @@ Xpd = pd.DataFrame(X)
|
||||
<span style="color: #228B22"># subtract the mean values and set up the covariance matrix</span>
|
||||
Xpd = Xpd - Xpd.mean()
|
||||
covariance_matrix = Xpd.cov()
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(covariance_matrix)
|
||||
<span style="color: #658b00">print</span>(covariance_matrix)
|
||||
</pre></div>
|
||||
<p>
|
||||
We note here that the covariance is zero for the first rows and
|
||||
@@ -591,7 +613,7 @@ matrix without these elements.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec8">Rewriting the Covariance and/or Correlation Matrix </h2>
|
||||
<h2 id="___sec10">Rewriting the Covariance and/or Correlation Matrix </h2>
|
||||
|
||||
<p>
|
||||
We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as
|
||||
@@ -644,7 +666,7 @@ It is easy to generalize this to a matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\t
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec9">Towards the PCA theorem </h2>
|
||||
<h2 id="___sec11">Towards the PCA theorem </h2>
|
||||
|
||||
<p>
|
||||
We have that the covariance matrix (the correlation matrix involves a simple rescaling) is given as
|
||||
@@ -703,7 +725,7 @@ features/predictors.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec10">The Algorithm before theorem </h2>
|
||||
<h2 id="___sec12">The Algorithm before theorem </h2>
|
||||
|
||||
<p>
|
||||
Here's how we would proceed in setting up the algorithm for the PCA, see also discussion below here.
|
||||
@@ -736,7 +758,7 @@ $$
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec11">Writing our own PCA code </h2>
|
||||
<h2 id="___sec13">Writing our own PCA code </h2>
|
||||
|
||||
<p>
|
||||
We will use a simple example first with two-dimensional data
|
||||
@@ -771,7 +793,7 @@ X = np.random.multivariate_normal(mean, cov, n)
|
||||
<p>
|
||||
Now we are going to implement the PCA algorithm. We will break it down into various substeps.
|
||||
|
||||
<h3 id="___sec12">Compute the sample mean and center the data </h3>
|
||||
<h3 id="___sec14">Compute the sample mean and center the data </h3>
|
||||
|
||||
<p>
|
||||
The first step of PCA is to compute the sample mean of the data and use it to center the data. Recall that the sample mean is
|
||||
@@ -812,7 +834,7 @@ variance. The diagonal covariance matrix elements will then be one,
|
||||
while the non-diagonal ones need to be divided by \( 2\sqrt{2} \) for our
|
||||
specific case.
|
||||
|
||||
<h3 id="___sec13">Compute the sample covariance </h3>
|
||||
<h3 id="___sec15">Compute the sample covariance </h3>
|
||||
|
||||
<p>
|
||||
Now we are going to use the mean centered data to compute the sample covariance of the data by using the following equation
|
||||
@@ -829,8 +851,8 @@ We can write our own code or simply use either the functionaly of <b>numpy</b> o
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">print</span>(df.cov())
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(np.cov(X_centered.T))
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #658b00">print</span>(df.cov())
|
||||
<span style="color: #658b00">print</span>(np.cov(X_centered.T))
|
||||
</pre></div>
|
||||
<p>
|
||||
Note that the way we define the covariance matrix here has a factor \( n-1 \) instead of \( n \). This is included in the <b>cov()</b> function by <b>numpy</b> and <b>pandas</b>.
|
||||
@@ -846,8 +868,8 @@ Cov[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>]
|
||||
Cov[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>] = np.sum(x.T<span style="color: #707a7c">@x</span>)/(n-<span style="color: #B452CD">1.0</span>)
|
||||
Cov[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>] = np.sum(y.T<span style="color: #707a7c">@y</span>)/(n-<span style="color: #B452CD">1.0</span>)
|
||||
Cov[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>]= Cov[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>]
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Centered covariance using own code"</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(Cov)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Centered covariance using own code"</span>)
|
||||
<span style="color: #658b00">print</span>(Cov)
|
||||
plt.plot(x, y, <span style="color: #CD5555">'x'</span>)
|
||||
plt.axis(<span style="color: #CD5555">'equal'</span>)
|
||||
plt.show()
|
||||
@@ -856,7 +878,7 @@ plt.show()
|
||||
Depending on the number of points \( n \), we will get results that are close to the covariance values defined above.
|
||||
The plot shows how the data are clustered around a line with slope close to one. Is this expected?
|
||||
|
||||
<h3 id="___sec14">Diagonalize the sample covariance matrix to obtain the principal components </h3>
|
||||
<h3 id="___sec16">Diagonalize the sample covariance matrix to obtain the principal components </h3>
|
||||
|
||||
<p>
|
||||
Now we are ready to solve for the principal components! To do so we
|
||||
@@ -899,21 +921,21 @@ EigValues, EigVectors = np.linalg.eig(Cov)
|
||||
<span style="color: #228B22">#permute = EigValues.argsort()</span>
|
||||
<span style="color: #228B22">#EigValues = EigValues[permute]</span>
|
||||
<span style="color: #228B22">#EigVectors = EigVectors[:,permute]</span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Eigenvalues of Covariance matrix"</span>)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Eigenvalues of Covariance matrix"</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">2</span>):
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(EigValues[i])
|
||||
<span style="color: #658b00">print</span>(EigValues[i])
|
||||
FirstEigvector = EigVectors[:,<span style="color: #B452CD">0</span>]
|
||||
SecondEigvector = EigVectors[:,<span style="color: #B452CD">1</span>]
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"First eigenvector"</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(FirstEigvector)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Second eigenvector"</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(SecondEigvector)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"First eigenvector"</span>)
|
||||
<span style="color: #658b00">print</span>(FirstEigvector)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Second eigenvector"</span>)
|
||||
<span style="color: #658b00">print</span>(SecondEigvector)
|
||||
<span style="color: #228B22">#thereafter we do a PCA with Scikit-learn</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.decomposition</span> <span style="color: #8B008B; font-weight: bold">import</span> PCA
|
||||
pca = PCA(n_components = <span style="color: #B452CD">2</span>)
|
||||
X2Dsl = pca.fit_transform(X)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Eigenvector of largest eigenvalue"</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(pca.components_.T[:, <span style="color: #B452CD">0</span>])
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Eigenvector of largest eigenvalue"</span>)
|
||||
<span style="color: #658b00">print</span>(pca.components_.T[:, <span style="color: #B452CD">0</span>])
|
||||
</pre></div>
|
||||
<p>
|
||||
This code does not contain all the above elements, but it shows how we can use <b>Scikit-Learn</b> to extract the eigenvector which corresponds to the largest eigenvalue. Try to address the questions we pose before the above code. Try also to change the values of the covariance matrix by making one of the diagonal elements much larger than the other. What do you observe then?
|
||||
@@ -921,7 +943,7 @@ This code does not contain all the above elements, but it shows how we can use <
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec15">Classical PCA Theorem </h2>
|
||||
<h2 id="___sec17">Classical PCA Theorem </h2>
|
||||
|
||||
<p>
|
||||
We assume now that we have a design matrix \( \boldsymbol{X} \) which has been
|
||||
@@ -956,7 +978,7 @@ The proof which follows will be updated by mid January 2020.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec16">Proof of the PCA Theorem </h2>
|
||||
<h2 id="___sec18">Proof of the PCA Theorem </h2>
|
||||
|
||||
<p>
|
||||
To show the PCA theorem let us start with the assumption that there is one vector \( \boldsymbol{w}_0 \) which corresponds to a solution which minimized the reconstruction error \( J \). This is an orthogonal vector. It means that we now approximate the reconstruction error in terms of \( \boldsymbol{w}_0 \) and \( \boldsymbol{z}_0 \) as
|
||||
@@ -985,7 +1007,7 @@ where the vectors on the rhs are known.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec17">PCA Proof continued </h2>
|
||||
<h2 id="___sec19">PCA Proof continued </h2>
|
||||
|
||||
<p>
|
||||
We have now found the unknown parameters \( z_{i0} \). These correspond to the projected coordinates and we can write
|
||||
@@ -1036,7 +1058,7 @@ of the projected data.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec18">The final step </h2>
|
||||
<h2 id="___sec20">The final step </h2>
|
||||
|
||||
<p>
|
||||
We could trivially maximize the variance of the projection (and
|
||||
@@ -1098,7 +1120,7 @@ Additional part of the proof for the other eigenvectors will be added by mid Jan
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec19">Geometric Interpretation and link with Singular Value Decomposition </h2>
|
||||
<h2 id="___sec21">Geometric Interpretation and link with Singular Value Decomposition </h2>
|
||||
|
||||
<p>
|
||||
This material will be added by mid January 2020.
|
||||
@@ -1106,7 +1128,7 @@ This material will be added by mid January 2020.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec20">Principal Component Analysis </h2>
|
||||
<h2 id="___sec22">Principal Component Analysis </h2>
|
||||
|
||||
<p>
|
||||
Principal Component Analysis (PCA) is by far the most popular dimensionality reduction algorithm.
|
||||
@@ -1134,14 +1156,14 @@ display(df)
|
||||
<span style="color: #228B22"># we center it ourselves</span>
|
||||
X_centered = X - X.mean(axis=<span style="color: #B452CD">0</span>)
|
||||
<span style="color: #228B22"># Then check the difference between pandas and our own set up</span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(X_centered-df)
|
||||
<span style="color: #658b00">print</span>(X_centered-df)
|
||||
<span style="color: #228B22">#Now we do an SVD</span>
|
||||
U, s, V = np.linalg.svd(X_centered)
|
||||
c1 = V.T[:, <span style="color: #B452CD">0</span>]
|
||||
c2 = V.T[:, <span style="color: #B452CD">1</span>]
|
||||
W2 = V.T[:, :<span style="color: #B452CD">2</span>]
|
||||
X2D = X_centered.dot(W2)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(X2D)
|
||||
<span style="color: #658b00">print</span>(X2D)
|
||||
</pre></div>
|
||||
<p>
|
||||
PCA assumes that the dataset is centered around the origin. Scikit-Learn’s PCA classes take care of centering
|
||||
@@ -1162,7 +1184,7 @@ X2D = X_centered.dot(W2)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec21">PCA and scikit-learn </h2>
|
||||
<h2 id="___sec23">PCA and scikit-learn </h2>
|
||||
|
||||
<p>
|
||||
Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
|
||||
@@ -1175,7 +1197,7 @@ that it automatically takes care of centering the data):
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.decomposition</span> <span style="color: #8B008B; font-weight: bold">import</span> PCA
|
||||
pca = PCA(n_components = <span style="color: #B452CD">2</span>)
|
||||
X2D = pca.fit_transform(X)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(X2D)
|
||||
<span style="color: #658b00">print</span>(X2D)
|
||||
</pre></div>
|
||||
<p>
|
||||
After fitting the PCA transformer to the dataset, you can access the principal components using the
|
||||
@@ -1194,7 +1216,7 @@ variance that lies along the axis of each principal component.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec22">Back to the Cancer Data </h2>
|
||||
<h2 id="___sec24">Back to the Cancer Data </h2>
|
||||
We can now repeat the above but applied to real data, in this case our breast cancer data.
|
||||
Here we compute performance scores on the training data using logistic regression.
|
||||
<p>
|
||||
@@ -1211,7 +1233,7 @@ X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,ra
|
||||
|
||||
logreg = LogisticRegression()
|
||||
logreg.fit(X_train, y_train)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Train set accuracy from Logistic Regression: {:.2f}"</span>.format(logreg.score(X_train,y_train)))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Train set accuracy from Logistic Regression: {:.2f}"</span>.format(logreg.score(X_train,y_train)))
|
||||
<span style="color: #228B22"># We scale the data</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler
|
||||
scaler = StandardScaler()
|
||||
@@ -1220,14 +1242,14 @@ X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
<span style="color: #228B22"># Then perform again a log reg fit</span>
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Train set accuracy scaled data: {:.2f}"</span>.format(logreg.score(X_train_scaled,y_train)))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Train set accuracy scaled data: {:.2f}"</span>.format(logreg.score(X_train_scaled,y_train)))
|
||||
<span style="color: #228B22">#thereafter we do a PCA with Scikit-learn</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.decomposition</span> <span style="color: #8B008B; font-weight: bold">import</span> PCA
|
||||
pca = PCA(n_components = <span style="color: #B452CD">2</span>)
|
||||
X2D_train = pca.fit_transform(X_train_scaled)
|
||||
<span style="color: #228B22"># and finally compute the log reg fit and the score on the training data </span>
|
||||
logreg.fit(X2D_train,y_train)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Train set accuracy scaled and PCA data: {:.2f}"</span>.format(logreg.score(X2D_train,y_train)))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Train set accuracy scaled and PCA data: {:.2f}"</span>.format(logreg.score(X2D_train,y_train)))
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that our training data after the PCA decomposition has a performance similar to the non-scaled data.
|
||||
@@ -1235,7 +1257,7 @@ We see that our training data after the PCA decomposition has a performance simi
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec23">More on the PCA </h2>
|
||||
<h2 id="___sec25">More on the PCA </h2>
|
||||
|
||||
<p>
|
||||
Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to
|
||||
@@ -1266,7 +1288,7 @@ X_reduced = pca.fit_transform(X)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec24">Incremental PCA </h2>
|
||||
<h2 id="___sec26">Incremental PCA </h2>
|
||||
|
||||
<p>
|
||||
One problem with the preceding implementation of PCA is that it requires the whole training set to fit in
|
||||
@@ -1278,7 +1300,7 @@ instances arrive).
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec25">Randomized PCA </h2>
|
||||
<h2 id="___sec27">Randomized PCA </h2>
|
||||
|
||||
<p>
|
||||
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
|
||||
@@ -1289,7 +1311,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec26">Kernel PCA </h2>
|
||||
<h2 id="___sec28">Kernel PCA </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -1315,7 +1337,7 @@ X_reduced = rbf_pca.fit_transform(X)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec27">LLE </h2>
|
||||
<h2 id="___sec29">LLE </h2>
|
||||
|
||||
<p>
|
||||
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
|
||||
@@ -1327,7 +1349,7 @@ these local relationships are best preserved (more details shortly).
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec28">Other techniques </h2>
|
||||
<h2 id="___sec30">Other techniques </h2>
|
||||
|
||||
<p>
|
||||
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
|
||||
|
||||
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
|
||||
<link href="https://cdn.rawgit.com/hplgit/doconce/master/bundled/html_styles/style_solarized_box/css/solarized_light_code.css" rel="stylesheet" type="text/css" title="light"/>
|
||||
@@ -61,64 +61,66 @@ div { text-align: justify; text-justify: inter-word; }
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -144,7 +146,7 @@ MathJax.Hub.Config({
|
||||
|
||||
|
||||
|
||||
<center><h1>Week 43: Dimensionality Reduction</h1></center> <!-- document title -->
|
||||
<center><h1>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</h1></center> <!-- document title -->
|
||||
|
||||
<p>
|
||||
<!-- author(s): Morten Hjorth-Jensen -->
|
||||
@@ -160,12 +162,32 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Sep 16, 2020</h4></center> <!-- date -->
|
||||
<center><h4>Oct 17, 2020</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec0">Why should we think of reducing the dimensionality </h2>
|
||||
<ul>
|
||||
<li> Thursday: Wrapping up Recurrent Neural Networks and solving differential equations.</li>
|
||||
<li> Friday: Principal Component Analysis and Dimensionality Reduction</li>
|
||||
</ul>
|
||||
|
||||
Reading suggestions for both days: "Aurelien Geron's chapters 8
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec0">Recurrent Neural Networks </h2>
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec1">Solving ODEs with Deep Learning </h2>
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec2">Why should we think of reducing the dimensionality </h2>
|
||||
|
||||
<p>
|
||||
In addition to the plot of the features, we study now also the covariance (and the correlation matrix).
|
||||
@@ -204,12 +226,12 @@ plt.show()
|
||||
correlation_matrix = cancerpd.corr().round(<span style="color: #B452CD">1</span>)
|
||||
<span style="color: #228B22"># use the heatmap function from seaborn to plot the correlation matrix</span>
|
||||
<span style="color: #228B22"># annot = True to print the values inside the square</span>
|
||||
sns.heatmap(data=correlation_matrix, annot=<span style="color: #658b00">True</span>)
|
||||
sns.heatmap(data=correlation_matrix, annot=<span style="color: #8B008B; font-weight: bold">True</span>)
|
||||
plt.show()
|
||||
|
||||
<span style="color: #228B22">#print eigvalues of correlation matrix</span>
|
||||
EigValues, EigVectors = np.linalg.eig(correlation_matrix)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(EigValues)
|
||||
<span style="color: #658b00">print</span>(EigValues)
|
||||
</pre></div>
|
||||
<p>
|
||||
In the above example we note two things. In the first plot we display
|
||||
@@ -249,7 +271,7 @@ applications.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec1">Basic ideas of the Principal Component Analysis (PCA) </h2>
|
||||
<h2 id="___sec3">Basic ideas of the Principal Component Analysis (PCA) </h2>
|
||||
|
||||
<p>
|
||||
The principal component analysis deals with the problem of fitting a
|
||||
@@ -271,7 +293,7 @@ We have a data set defined by a design/feature matrix \( \boldsymbol{X} \) (see
|
||||
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec2">Introducing the Covariance and Correlation functions </h2>
|
||||
<h2 id="___sec4">Introducing the Covariance and Correlation functions </h2>
|
||||
|
||||
<p>
|
||||
Before we discuss the PCA theorem, we need to remind ourselves about
|
||||
@@ -332,7 +354,7 @@ In the above example this is the function we constructed using <b>pandas</b>.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec3">Correlation Function and Design/Feature Matrix </h2>
|
||||
<h2 id="___sec5">Correlation Function and Design/Feature Matrix </h2>
|
||||
|
||||
<p>
|
||||
In our derivation of the various regression algorithms like <b>Ordinary Least Squares</b> or <b>Ridge regression</b>
|
||||
@@ -393,7 +415,7 @@ $$
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec4">Covariance Matrix Examples </h2>
|
||||
<h2 id="___sec6">Covariance Matrix Examples </h2>
|
||||
|
||||
<p>
|
||||
The Numpy function <b>np.cov</b> calculates the covariance elements using
|
||||
@@ -426,17 +448,17 @@ covariance matrix through the <b>np.linalg.eig()</b> function.
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
||||
n = <span style="color: #B452CD">100</span>
|
||||
x = np.random.normal(size=n)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(np.mean(x))
|
||||
<span style="color: #658b00">print</span>(np.mean(x))
|
||||
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.normal(size=n)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(np.mean(y))
|
||||
<span style="color: #658b00">print</span>(np.mean(y))
|
||||
W = np.vstack((x, y))
|
||||
C = np.cov(W)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(C)
|
||||
<span style="color: #658b00">print</span>(C)
|
||||
</pre></div>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec5">Correlation Matrix </h2>
|
||||
<h2 id="___sec7">Correlation Matrix </h2>
|
||||
|
||||
<p>
|
||||
The previous example can be converted into the correlation matrix by
|
||||
@@ -458,8 +480,8 @@ x = x - np.mean(x)
|
||||
y = y - np.mean(y)
|
||||
variance_x = np.sum(x<span style="color: #707a7c">@x</span>)/n
|
||||
variance_y = np.sum(y<span style="color: #707a7c">@y</span>)/n
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(variance_x)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(variance_y)
|
||||
<span style="color: #658b00">print</span>(variance_x)
|
||||
<span style="color: #658b00">print</span>(variance_y)
|
||||
cov_xy = np.sum(x<span style="color: #707a7c">@y</span>)/n
|
||||
cov_xx = np.sum(x<span style="color: #707a7c">@x</span>)/n
|
||||
cov_yy = np.sum(y<span style="color: #707a7c">@y</span>)/n
|
||||
@@ -468,7 +490,7 @@ C[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>]=
|
||||
C[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>]= cov_yy/variance_y
|
||||
C[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>]= cov_xy/np.sqrt(variance_y*variance_x)
|
||||
C[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>]= C[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>]
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(C)
|
||||
<span style="color: #658b00">print</span>(C)
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that the matrix elements along the diagonal are one as they
|
||||
@@ -481,7 +503,7 @@ The above procedure with <b>numpy</b> can be made more compact if we use <b>pand
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec6">Correlation Matrix with Pandas </h2>
|
||||
<h2 id="___sec8">Correlation Matrix with Pandas </h2>
|
||||
|
||||
<p>
|
||||
We whow here how we can set up the correlation matrix using <b>pandas</b>, as done in this simple code
|
||||
@@ -496,11 +518,11 @@ x = x - np.mean(x)
|
||||
y = <span style="color: #B452CD">4</span>+<span style="color: #B452CD">3</span>*x+np.random.normal(size=n)
|
||||
y = y - np.mean(y)
|
||||
X = (np.vstack((x, y))).T
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(X)
|
||||
<span style="color: #658b00">print</span>(X)
|
||||
Xpd = pd.DataFrame(X)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(Xpd)
|
||||
<span style="color: #658b00">print</span>(Xpd)
|
||||
correlation_matrix = Xpd.corr()
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(correlation_matrix)
|
||||
<span style="color: #658b00">print</span>(correlation_matrix)
|
||||
</pre></div>
|
||||
<p>
|
||||
We expand this model to the Franke function discussed above.
|
||||
@@ -508,7 +530,7 @@ We expand this model to the Franke function discussed above.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec7">Correlation Matrix with Pandas and the Franke function </h2>
|
||||
<h2 id="___sec9">Correlation Matrix with Pandas and the Franke function </h2>
|
||||
|
||||
<p>
|
||||
|
||||
@@ -555,7 +577,7 @@ Xpd = pd.DataFrame(X)
|
||||
<span style="color: #228B22"># subtract the mean values and set up the covariance matrix</span>
|
||||
Xpd = Xpd - Xpd.mean()
|
||||
covariance_matrix = Xpd.cov()
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(covariance_matrix)
|
||||
<span style="color: #658b00">print</span>(covariance_matrix)
|
||||
</pre></div>
|
||||
<p>
|
||||
We note here that the covariance is zero for the first rows and
|
||||
@@ -571,7 +593,7 @@ matrix without these elements.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec8">Rewriting the Covariance and/or Correlation Matrix </h2>
|
||||
<h2 id="___sec10">Rewriting the Covariance and/or Correlation Matrix </h2>
|
||||
|
||||
<p>
|
||||
We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as
|
||||
@@ -614,7 +636,7 @@ It is easy to generalize this to a matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\t
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec9">Towards the PCA theorem </h2>
|
||||
<h2 id="___sec11">Towards the PCA theorem </h2>
|
||||
|
||||
<p>
|
||||
We have that the covariance matrix (the correlation matrix involves a simple rescaling) is given as
|
||||
@@ -665,7 +687,7 @@ features/predictors.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec10">The Algorithm before theorem </h2>
|
||||
<h2 id="___sec12">The Algorithm before theorem </h2>
|
||||
|
||||
<p>
|
||||
Here's how we would proceed in setting up the algorithm for the PCA, see also discussion below here.
|
||||
@@ -696,7 +718,7 @@ $$
|
||||
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec11">Writing our own PCA code </h2>
|
||||
<h2 id="___sec13">Writing our own PCA code </h2>
|
||||
|
||||
<p>
|
||||
We will use a simple example first with two-dimensional data
|
||||
@@ -729,7 +751,7 @@ X = np.random.multivariate_normal(mean, cov, n)
|
||||
<p>
|
||||
Now we are going to implement the PCA algorithm. We will break it down into various substeps.
|
||||
|
||||
<h3 id="___sec12">Compute the sample mean and center the data </h3>
|
||||
<h3 id="___sec14">Compute the sample mean and center the data </h3>
|
||||
|
||||
<p>
|
||||
The first step of PCA is to compute the sample mean of the data and use it to center the data. Recall that the sample mean is
|
||||
@@ -766,7 +788,7 @@ variance. The diagonal covariance matrix elements will then be one,
|
||||
while the non-diagonal ones need to be divided by \( 2\sqrt{2} \) for our
|
||||
specific case.
|
||||
|
||||
<h3 id="___sec13">Compute the sample covariance </h3>
|
||||
<h3 id="___sec15">Compute the sample covariance </h3>
|
||||
|
||||
<p>
|
||||
Now we are going to use the mean centered data to compute the sample covariance of the data by using the following equation
|
||||
@@ -781,8 +803,8 @@ We can write our own code or simply use either the functionaly of <b>numpy</b> o
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">print</span>(df.cov())
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(np.cov(X_centered.T))
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #658b00">print</span>(df.cov())
|
||||
<span style="color: #658b00">print</span>(np.cov(X_centered.T))
|
||||
</pre></div>
|
||||
<p>
|
||||
Note that the way we define the covariance matrix here has a factor \( n-1 \) instead of \( n \). This is included in the <b>cov()</b> function by <b>numpy</b> and <b>pandas</b>.
|
||||
@@ -798,8 +820,8 @@ Cov[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>]
|
||||
Cov[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">0</span>] = np.sum(x.T<span style="color: #707a7c">@x</span>)/(n-<span style="color: #B452CD">1.0</span>)
|
||||
Cov[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">1</span>] = np.sum(y.T<span style="color: #707a7c">@y</span>)/(n-<span style="color: #B452CD">1.0</span>)
|
||||
Cov[<span style="color: #B452CD">1</span>,<span style="color: #B452CD">0</span>]= Cov[<span style="color: #B452CD">0</span>,<span style="color: #B452CD">1</span>]
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Centered covariance using own code"</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(Cov)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Centered covariance using own code"</span>)
|
||||
<span style="color: #658b00">print</span>(Cov)
|
||||
plt.plot(x, y, <span style="color: #CD5555">'x'</span>)
|
||||
plt.axis(<span style="color: #CD5555">'equal'</span>)
|
||||
plt.show()
|
||||
@@ -808,7 +830,7 @@ plt.show()
|
||||
Depending on the number of points \( n \), we will get results that are close to the covariance values defined above.
|
||||
The plot shows how the data are clustered around a line with slope close to one. Is this expected?
|
||||
|
||||
<h3 id="___sec14">Diagonalize the sample covariance matrix to obtain the principal components </h3>
|
||||
<h3 id="___sec16">Diagonalize the sample covariance matrix to obtain the principal components </h3>
|
||||
|
||||
<p>
|
||||
Now we are ready to solve for the principal components! To do so we
|
||||
@@ -850,21 +872,21 @@ EigValues, EigVectors = np.linalg.eig(Cov)
|
||||
<span style="color: #228B22">#permute = EigValues.argsort()</span>
|
||||
<span style="color: #228B22">#EigValues = EigValues[permute]</span>
|
||||
<span style="color: #228B22">#EigVectors = EigVectors[:,permute]</span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Eigenvalues of Covariance matrix"</span>)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Eigenvalues of Covariance matrix"</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">2</span>):
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(EigValues[i])
|
||||
<span style="color: #658b00">print</span>(EigValues[i])
|
||||
FirstEigvector = EigVectors[:,<span style="color: #B452CD">0</span>]
|
||||
SecondEigvector = EigVectors[:,<span style="color: #B452CD">1</span>]
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"First eigenvector"</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(FirstEigvector)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Second eigenvector"</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(SecondEigvector)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"First eigenvector"</span>)
|
||||
<span style="color: #658b00">print</span>(FirstEigvector)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Second eigenvector"</span>)
|
||||
<span style="color: #658b00">print</span>(SecondEigvector)
|
||||
<span style="color: #228B22">#thereafter we do a PCA with Scikit-learn</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.decomposition</span> <span style="color: #8B008B; font-weight: bold">import</span> PCA
|
||||
pca = PCA(n_components = <span style="color: #B452CD">2</span>)
|
||||
X2Dsl = pca.fit_transform(X)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Eigenvector of largest eigenvalue"</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(pca.components_.T[:, <span style="color: #B452CD">0</span>])
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Eigenvector of largest eigenvalue"</span>)
|
||||
<span style="color: #658b00">print</span>(pca.components_.T[:, <span style="color: #B452CD">0</span>])
|
||||
</pre></div>
|
||||
<p>
|
||||
This code does not contain all the above elements, but it shows how we can use <b>Scikit-Learn</b> to extract the eigenvector which corresponds to the largest eigenvalue. Try to address the questions we pose before the above code. Try also to change the values of the covariance matrix by making one of the diagonal elements much larger than the other. What do you observe then?
|
||||
@@ -872,7 +894,7 @@ This code does not contain all the above elements, but it shows how we can use <
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec15">Classical PCA Theorem </h2>
|
||||
<h2 id="___sec17">Classical PCA Theorem </h2>
|
||||
|
||||
<p>
|
||||
We assume now that we have a design matrix \( \boldsymbol{X} \) which has been
|
||||
@@ -905,7 +927,7 @@ The proof which follows will be updated by mid January 2020.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec16">Proof of the PCA Theorem </h2>
|
||||
<h2 id="___sec18">Proof of the PCA Theorem </h2>
|
||||
|
||||
<p>
|
||||
To show the PCA theorem let us start with the assumption that there is one vector \( \boldsymbol{w}_0 \) which corresponds to a solution which minimized the reconstruction error \( J \). This is an orthogonal vector. It means that we now approximate the reconstruction error in terms of \( \boldsymbol{w}_0 \) and \( \boldsymbol{z}_0 \) as
|
||||
@@ -928,7 +950,7 @@ where the vectors on the rhs are known.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec17">PCA Proof continued </h2>
|
||||
<h2 id="___sec19">PCA Proof continued </h2>
|
||||
|
||||
<p>
|
||||
We have now found the unknown parameters \( z_{i0} \). These correspond to the projected coordinates and we can write
|
||||
@@ -969,7 +991,7 @@ of the projected data.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec18">The final step </h2>
|
||||
<h2 id="___sec20">The final step </h2>
|
||||
|
||||
<p>
|
||||
We could trivially maximize the variance of the projection (and
|
||||
@@ -1023,7 +1045,7 @@ Additional part of the proof for the other eigenvectors will be added by mid Jan
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec19">Geometric Interpretation and link with Singular Value Decomposition </h2>
|
||||
<h2 id="___sec21">Geometric Interpretation and link with Singular Value Decomposition </h2>
|
||||
|
||||
<p>
|
||||
This material will be added by mid January 2020.
|
||||
@@ -1031,7 +1053,7 @@ This material will be added by mid January 2020.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec20">Principal Component Analysis </h2>
|
||||
<h2 id="___sec22">Principal Component Analysis </h2>
|
||||
|
||||
<p>
|
||||
Principal Component Analysis (PCA) is by far the most popular dimensionality reduction algorithm.
|
||||
@@ -1059,14 +1081,14 @@ display(df)
|
||||
<span style="color: #228B22"># we center it ourselves</span>
|
||||
X_centered = X - X.mean(axis=<span style="color: #B452CD">0</span>)
|
||||
<span style="color: #228B22"># Then check the difference between pandas and our own set up</span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(X_centered-df)
|
||||
<span style="color: #658b00">print</span>(X_centered-df)
|
||||
<span style="color: #228B22">#Now we do an SVD</span>
|
||||
U, s, V = np.linalg.svd(X_centered)
|
||||
c1 = V.T[:, <span style="color: #B452CD">0</span>]
|
||||
c2 = V.T[:, <span style="color: #B452CD">1</span>]
|
||||
W2 = V.T[:, :<span style="color: #B452CD">2</span>]
|
||||
X2D = X_centered.dot(W2)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(X2D)
|
||||
<span style="color: #658b00">print</span>(X2D)
|
||||
</pre></div>
|
||||
<p>
|
||||
PCA assumes that the dataset is centered around the origin. Scikit-Learn’s PCA classes take care of centering
|
||||
@@ -1086,7 +1108,7 @@ X2D = X_centered.dot(W2)
|
||||
<p>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec21">PCA and scikit-learn </h2>
|
||||
<h2 id="___sec23">PCA and scikit-learn </h2>
|
||||
|
||||
<p>
|
||||
Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
|
||||
@@ -1099,7 +1121,7 @@ that it automatically takes care of centering the data):
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.decomposition</span> <span style="color: #8B008B; font-weight: bold">import</span> PCA
|
||||
pca = PCA(n_components = <span style="color: #B452CD">2</span>)
|
||||
X2D = pca.fit_transform(X)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(X2D)
|
||||
<span style="color: #658b00">print</span>(X2D)
|
||||
</pre></div>
|
||||
<p>
|
||||
After fitting the PCA transformer to the dataset, you can access the principal components using the
|
||||
@@ -1118,7 +1140,7 @@ variance that lies along the axis of each principal component.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec22">Back to the Cancer Data </h2>
|
||||
<h2 id="___sec24">Back to the Cancer Data </h2>
|
||||
We can now repeat the above but applied to real data, in this case our breast cancer data.
|
||||
Here we compute performance scores on the training data using logistic regression.
|
||||
<p>
|
||||
@@ -1135,7 +1157,7 @@ X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,ra
|
||||
|
||||
logreg = LogisticRegression()
|
||||
logreg.fit(X_train, y_train)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Train set accuracy from Logistic Regression: {:.2f}"</span>.format(logreg.score(X_train,y_train)))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Train set accuracy from Logistic Regression: {:.2f}"</span>.format(logreg.score(X_train,y_train)))
|
||||
<span style="color: #228B22"># We scale the data</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler
|
||||
scaler = StandardScaler()
|
||||
@@ -1144,14 +1166,14 @@ X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
<span style="color: #228B22"># Then perform again a log reg fit</span>
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Train set accuracy scaled data: {:.2f}"</span>.format(logreg.score(X_train_scaled,y_train)))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Train set accuracy scaled data: {:.2f}"</span>.format(logreg.score(X_train_scaled,y_train)))
|
||||
<span style="color: #228B22">#thereafter we do a PCA with Scikit-learn</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.decomposition</span> <span style="color: #8B008B; font-weight: bold">import</span> PCA
|
||||
pca = PCA(n_components = <span style="color: #B452CD">2</span>)
|
||||
X2D_train = pca.fit_transform(X_train_scaled)
|
||||
<span style="color: #228B22"># and finally compute the log reg fit and the score on the training data </span>
|
||||
logreg.fit(X2D_train,y_train)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Train set accuracy scaled and PCA data: {:.2f}"</span>.format(logreg.score(X2D_train,y_train)))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Train set accuracy scaled and PCA data: {:.2f}"</span>.format(logreg.score(X2D_train,y_train)))
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that our training data after the PCA decomposition has a performance similar to the non-scaled data.
|
||||
@@ -1159,7 +1181,7 @@ We see that our training data after the PCA decomposition has a performance simi
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec23">More on the PCA </h2>
|
||||
<h2 id="___sec25">More on the PCA </h2>
|
||||
|
||||
<p>
|
||||
Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to
|
||||
@@ -1189,7 +1211,7 @@ X_reduced = pca.fit_transform(X)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec24">Incremental PCA </h2>
|
||||
<h2 id="___sec26">Incremental PCA </h2>
|
||||
|
||||
<p>
|
||||
One problem with the preceding implementation of PCA is that it requires the whole training set to fit in
|
||||
@@ -1201,7 +1223,7 @@ instances arrive).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec25">Randomized PCA </h2>
|
||||
<h2 id="___sec27">Randomized PCA </h2>
|
||||
|
||||
<p>
|
||||
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
|
||||
@@ -1212,7 +1234,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec26">Kernel PCA </h2>
|
||||
<h2 id="___sec28">Kernel PCA </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -1241,7 +1263,7 @@ X_reduced = rbf_pca.fit_transform(X)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec27">LLE </h2>
|
||||
<h2 id="___sec29">LLE </h2>
|
||||
|
||||
<p>
|
||||
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
|
||||
@@ -1253,7 +1275,7 @@ these local relationships are best preserved (more details shortly).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec28">Other techniques </h2>
|
||||
<h2 id="___sec30">Other techniques </h2>
|
||||
|
||||
<p>
|
||||
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
|
||||
|
||||
+120
-98
@@ -7,9 +7,9 @@ Automatically generated HTML file from DocOnce source
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Week 43: Dimensionality Reduction">
|
||||
<meta name="description" content="Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods">
|
||||
|
||||
<title>Week 43: Dimensionality Reduction</title>
|
||||
<title>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</title>
|
||||
|
||||
|
||||
<style type="text/css">
|
||||
@@ -66,64 +66,66 @@ div { text-align: justify; text-justify: inter-word; }
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec0'),
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec1'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
'sections': [('Recurrent Neural Networks', 2, None, '___sec0'),
|
||||
('Solving ODEs with Deep Learning', 2, None, '___sec1'),
|
||||
('Why should we think of reducing the dimensionality',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
('Basic ideas of the Principal Component Analysis (PCA)',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec4'),
|
||||
('Correlation Matrix', 2, None, '___sec5'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec6'),
|
||||
('Introducing the Covariance and Correlation functions',
|
||||
2,
|
||||
None,
|
||||
'___sec4'),
|
||||
('Correlation Function and Design/Feature Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec5'),
|
||||
('Covariance Matrix Examples', 2, None, '___sec6'),
|
||||
('Correlation Matrix', 2, None, '___sec7'),
|
||||
('Correlation Matrix with Pandas', 2, None, '___sec8'),
|
||||
('Correlation Matrix with Pandas and the Franke function',
|
||||
2,
|
||||
None,
|
||||
'___sec7'),
|
||||
'___sec9'),
|
||||
('Rewriting the Covariance and/or Correlation Matrix',
|
||||
2,
|
||||
None,
|
||||
'___sec8'),
|
||||
('Towards the PCA theorem', 2, None, '___sec9'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec10'),
|
||||
('Writing our own PCA code', 2, None, '___sec11'),
|
||||
'___sec10'),
|
||||
('Towards the PCA theorem', 2, None, '___sec11'),
|
||||
('The Algorithm before the Theorem', 2, None, '___sec12'),
|
||||
('Writing our own PCA code', 2, None, '___sec13'),
|
||||
('Compute the sample mean and center the data',
|
||||
3,
|
||||
None,
|
||||
'___sec12'),
|
||||
('Compute the sample covariance', 3, None, '___sec13'),
|
||||
'___sec14'),
|
||||
('Compute the sample covariance', 3, None, '___sec15'),
|
||||
('Diagonalize the sample covariance matrix to obtain the '
|
||||
'principal components',
|
||||
3,
|
||||
None,
|
||||
'___sec14'),
|
||||
('Classical PCA Theorem', 2, None, '___sec15'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec16'),
|
||||
('PCA Proof continued', 2, None, '___sec17'),
|
||||
('The final step', 2, None, '___sec18'),
|
||||
'___sec16'),
|
||||
('Classical PCA Theorem', 2, None, '___sec17'),
|
||||
('Proof of the PCA Theorem', 2, None, '___sec18'),
|
||||
('PCA Proof continued', 2, None, '___sec19'),
|
||||
('The final step', 2, None, '___sec20'),
|
||||
('Geometric Interpretation and link with Singular Value '
|
||||
'Decomposition',
|
||||
2,
|
||||
None,
|
||||
'___sec19'),
|
||||
('Principal Component Analysis', 2, None, '___sec20'),
|
||||
('PCA and scikit-learn', 2, None, '___sec21'),
|
||||
('Back to the Cancer Data', 2, None, '___sec22'),
|
||||
('More on the PCA', 2, None, '___sec23'),
|
||||
('Incremental PCA', 2, None, '___sec24'),
|
||||
('Randomized PCA', 2, None, '___sec25'),
|
||||
('Kernel PCA', 2, None, '___sec26'),
|
||||
('LLE', 2, None, '___sec27'),
|
||||
('Other techniques', 2, None, '___sec28')]}
|
||||
'___sec21'),
|
||||
('Principal Component Analysis', 2, None, '___sec22'),
|
||||
('PCA and scikit-learn', 2, None, '___sec23'),
|
||||
('Back to the Cancer Data', 2, None, '___sec24'),
|
||||
('More on the PCA', 2, None, '___sec25'),
|
||||
('Incremental PCA', 2, None, '___sec26'),
|
||||
('Randomized PCA', 2, None, '___sec27'),
|
||||
('Kernel PCA', 2, None, '___sec28'),
|
||||
('LLE', 2, None, '___sec29'),
|
||||
('Other techniques', 2, None, '___sec30')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -149,7 +151,7 @@ MathJax.Hub.Config({
|
||||
|
||||
|
||||
|
||||
<center><h1>Week 43: Dimensionality Reduction</h1></center> <!-- document title -->
|
||||
<center><h1>Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods</h1></center> <!-- document title -->
|
||||
|
||||
<p>
|
||||
<!-- author(s): Morten Hjorth-Jensen -->
|
||||
@@ -165,12 +167,32 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Sep 16, 2020</h4></center> <!-- date -->
|
||||
<center><h4>Oct 17, 2020</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec0">Why should we think of reducing the dimensionality </h2>
|
||||
<ul>
|
||||
<li> Thursday: Wrapping up Recurrent Neural Networks and solving differential equations.</li>
|
||||
<li> Friday: Principal Component Analysis and Dimensionality Reduction</li>
|
||||
</ul>
|
||||
|
||||
Reading suggestions for both days: "Aurelien Geron's chapters 8
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec0">Recurrent Neural Networks </h2>
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec1">Solving ODEs with Deep Learning </h2>
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec2">Why should we think of reducing the dimensionality </h2>
|
||||
|
||||
<p>
|
||||
In addition to the plot of the features, we study now also the covariance (and the correlation matrix).
|
||||
@@ -209,12 +231,12 @@ plt<span style="color: #666666">.</span>show()
|
||||
correlation_matrix <span style="color: #666666">=</span> cancerpd<span style="color: #666666">.</span>corr()<span style="color: #666666">.</span>round(<span style="color: #666666">1</span>)
|
||||
<span style="color: #408080; font-style: italic"># use the heatmap function from seaborn to plot the correlation matrix</span>
|
||||
<span style="color: #408080; font-style: italic"># annot = True to print the values inside the square</span>
|
||||
sns<span style="color: #666666">.</span>heatmap(data<span style="color: #666666">=</span>correlation_matrix, annot<span style="color: #666666">=</span><span style="color: #008000">True</span>)
|
||||
sns<span style="color: #666666">.</span>heatmap(data<span style="color: #666666">=</span>correlation_matrix, annot<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic">#print eigvalues of correlation matrix</span>
|
||||
EigValues, EigVectors <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>eig(correlation_matrix)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(EigValues)
|
||||
<span style="color: #008000">print</span>(EigValues)
|
||||
</pre></div>
|
||||
<p>
|
||||
In the above example we note two things. In the first plot we display
|
||||
@@ -254,7 +276,7 @@ applications.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec1">Basic ideas of the Principal Component Analysis (PCA) </h2>
|
||||
<h2 id="___sec3">Basic ideas of the Principal Component Analysis (PCA) </h2>
|
||||
|
||||
<p>
|
||||
The principal component analysis deals with the problem of fitting a
|
||||
@@ -276,7 +298,7 @@ We have a data set defined by a design/feature matrix \( \boldsymbol{X} \) (see
|
||||
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec2">Introducing the Covariance and Correlation functions </h2>
|
||||
<h2 id="___sec4">Introducing the Covariance and Correlation functions </h2>
|
||||
|
||||
<p>
|
||||
Before we discuss the PCA theorem, we need to remind ourselves about
|
||||
@@ -337,7 +359,7 @@ In the above example this is the function we constructed using <b>pandas</b>.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec3">Correlation Function and Design/Feature Matrix </h2>
|
||||
<h2 id="___sec5">Correlation Function and Design/Feature Matrix </h2>
|
||||
|
||||
<p>
|
||||
In our derivation of the various regression algorithms like <b>Ordinary Least Squares</b> or <b>Ridge regression</b>
|
||||
@@ -398,7 +420,7 @@ $$
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec4">Covariance Matrix Examples </h2>
|
||||
<h2 id="___sec6">Covariance Matrix Examples </h2>
|
||||
|
||||
<p>
|
||||
The Numpy function <b>np.cov</b> calculates the covariance elements using
|
||||
@@ -431,17 +453,17 @@ covariance matrix through the <b>np.linalg.eig()</b> function.
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
n <span style="color: #666666">=</span> <span style="color: #666666">100</span>
|
||||
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(np<span style="color: #666666">.</span>mean(x))
|
||||
<span style="color: #008000">print</span>(np<span style="color: #666666">.</span>mean(x))
|
||||
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(np<span style="color: #666666">.</span>mean(y))
|
||||
<span style="color: #008000">print</span>(np<span style="color: #666666">.</span>mean(y))
|
||||
W <span style="color: #666666">=</span> np<span style="color: #666666">.</span>vstack((x, y))
|
||||
C <span style="color: #666666">=</span> np<span style="color: #666666">.</span>cov(W)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(C)
|
||||
<span style="color: #008000">print</span>(C)
|
||||
</pre></div>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec5">Correlation Matrix </h2>
|
||||
<h2 id="___sec7">Correlation Matrix </h2>
|
||||
|
||||
<p>
|
||||
The previous example can be converted into the correlation matrix by
|
||||
@@ -463,8 +485,8 @@ x <span style="color: #666666">=</span> x <span style="color: #666666">-</span>
|
||||
y <span style="color: #666666">=</span> y <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(y)
|
||||
variance_x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #AA22FF">@x</span>)<span style="color: #666666">/</span>n
|
||||
variance_y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(y<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>n
|
||||
<span style="color: #008000; font-weight: bold">print</span>(variance_x)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(variance_y)
|
||||
<span style="color: #008000">print</span>(variance_x)
|
||||
<span style="color: #008000">print</span>(variance_y)
|
||||
cov_xy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>n
|
||||
cov_xx <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #AA22FF">@x</span>)<span style="color: #666666">/</span>n
|
||||
cov_yy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(y<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>n
|
||||
@@ -473,7 +495,7 @@ C[<span style="color: #666666">0</span>,<span style="color: #666666">0</span>]<s
|
||||
C[<span style="color: #666666">1</span>,<span style="color: #666666">1</span>]<span style="color: #666666">=</span> cov_yy<span style="color: #666666">/</span>variance_y
|
||||
C[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>]<span style="color: #666666">=</span> cov_xy<span style="color: #666666">/</span>np<span style="color: #666666">.</span>sqrt(variance_y<span style="color: #666666">*</span>variance_x)
|
||||
C[<span style="color: #666666">1</span>,<span style="color: #666666">0</span>]<span style="color: #666666">=</span> C[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>]
|
||||
<span style="color: #008000; font-weight: bold">print</span>(C)
|
||||
<span style="color: #008000">print</span>(C)
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that the matrix elements along the diagonal are one as they
|
||||
@@ -486,7 +508,7 @@ The above procedure with <b>numpy</b> can be made more compact if we use <b>pand
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec6">Correlation Matrix with Pandas </h2>
|
||||
<h2 id="___sec8">Correlation Matrix with Pandas </h2>
|
||||
|
||||
<p>
|
||||
We whow here how we can set up the correlation matrix using <b>pandas</b>, as done in this simple code
|
||||
@@ -501,11 +523,11 @@ x <span style="color: #666666">=</span> x <span style="color: #666666">-</span>
|
||||
y <span style="color: #666666">=</span> <span style="color: #666666">4+3*</span>x<span style="color: #666666">+</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>normal(size<span style="color: #666666">=</span>n)
|
||||
y <span style="color: #666666">=</span> y <span style="color: #666666">-</span> np<span style="color: #666666">.</span>mean(y)
|
||||
X <span style="color: #666666">=</span> (np<span style="color: #666666">.</span>vstack((x, y)))<span style="color: #666666">.</span>T
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X)
|
||||
<span style="color: #008000">print</span>(X)
|
||||
Xpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(X)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(Xpd)
|
||||
<span style="color: #008000">print</span>(Xpd)
|
||||
correlation_matrix <span style="color: #666666">=</span> Xpd<span style="color: #666666">.</span>corr()
|
||||
<span style="color: #008000; font-weight: bold">print</span>(correlation_matrix)
|
||||
<span style="color: #008000">print</span>(correlation_matrix)
|
||||
</pre></div>
|
||||
<p>
|
||||
We expand this model to the Franke function discussed above.
|
||||
@@ -513,7 +535,7 @@ We expand this model to the Franke function discussed above.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec7">Correlation Matrix with Pandas and the Franke function </h2>
|
||||
<h2 id="___sec9">Correlation Matrix with Pandas and the Franke function </h2>
|
||||
|
||||
<p>
|
||||
|
||||
@@ -560,7 +582,7 @@ Xpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span
|
||||
<span style="color: #408080; font-style: italic"># subtract the mean values and set up the covariance matrix</span>
|
||||
Xpd <span style="color: #666666">=</span> Xpd <span style="color: #666666">-</span> Xpd<span style="color: #666666">.</span>mean()
|
||||
covariance_matrix <span style="color: #666666">=</span> Xpd<span style="color: #666666">.</span>cov()
|
||||
<span style="color: #008000; font-weight: bold">print</span>(covariance_matrix)
|
||||
<span style="color: #008000">print</span>(covariance_matrix)
|
||||
</pre></div>
|
||||
<p>
|
||||
We note here that the covariance is zero for the first rows and
|
||||
@@ -576,7 +598,7 @@ matrix without these elements.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec8">Rewriting the Covariance and/or Correlation Matrix </h2>
|
||||
<h2 id="___sec10">Rewriting the Covariance and/or Correlation Matrix </h2>
|
||||
|
||||
<p>
|
||||
We can rewrite the covariance matrix in a more compact form in terms of the design/feature matrix \( \boldsymbol{X} \) as
|
||||
@@ -619,7 +641,7 @@ It is easy to generalize this to a matrix \( \boldsymbol{X}\in {\mathbb{R}}^{n\t
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec9">Towards the PCA theorem </h2>
|
||||
<h2 id="___sec11">Towards the PCA theorem </h2>
|
||||
|
||||
<p>
|
||||
We have that the covariance matrix (the correlation matrix involves a simple rescaling) is given as
|
||||
@@ -670,7 +692,7 @@ features/predictors.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec10">The Algorithm before theorem </h2>
|
||||
<h2 id="___sec12">The Algorithm before theorem </h2>
|
||||
|
||||
<p>
|
||||
Here's how we would proceed in setting up the algorithm for the PCA, see also discussion below here.
|
||||
@@ -701,7 +723,7 @@ $$
|
||||
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec11">Writing our own PCA code </h2>
|
||||
<h2 id="___sec13">Writing our own PCA code </h2>
|
||||
|
||||
<p>
|
||||
We will use a simple example first with two-dimensional data
|
||||
@@ -734,7 +756,7 @@ X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>r
|
||||
<p>
|
||||
Now we are going to implement the PCA algorithm. We will break it down into various substeps.
|
||||
|
||||
<h3 id="___sec12">Compute the sample mean and center the data </h3>
|
||||
<h3 id="___sec14">Compute the sample mean and center the data </h3>
|
||||
|
||||
<p>
|
||||
The first step of PCA is to compute the sample mean of the data and use it to center the data. Recall that the sample mean is
|
||||
@@ -771,7 +793,7 @@ variance. The diagonal covariance matrix elements will then be one,
|
||||
while the non-diagonal ones need to be divided by \( 2\sqrt{2} \) for our
|
||||
specific case.
|
||||
|
||||
<h3 id="___sec13">Compute the sample covariance </h3>
|
||||
<h3 id="___sec15">Compute the sample covariance </h3>
|
||||
|
||||
<p>
|
||||
Now we are going to use the mean centered data to compute the sample covariance of the data by using the following equation
|
||||
@@ -786,8 +808,8 @@ We can write our own code or simply use either the functionaly of <b>numpy</b> o
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">print</span>(df<span style="color: #666666">.</span>cov())
|
||||
<span style="color: #008000; font-weight: bold">print</span>(np<span style="color: #666666">.</span>cov(X_centered<span style="color: #666666">.</span>T))
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000">print</span>(df<span style="color: #666666">.</span>cov())
|
||||
<span style="color: #008000">print</span>(np<span style="color: #666666">.</span>cov(X_centered<span style="color: #666666">.</span>T))
|
||||
</pre></div>
|
||||
<p>
|
||||
Note that the way we define the covariance matrix here has a factor \( n-1 \) instead of \( n \). This is included in the <b>cov()</b> function by <b>numpy</b> and <b>pandas</b>.
|
||||
@@ -803,8 +825,8 @@ Cov[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>]
|
||||
Cov[<span style="color: #666666">0</span>,<span style="color: #666666">0</span>] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(x<span style="color: #666666">.</span>T<span style="color: #AA22FF">@x</span>)<span style="color: #666666">/</span>(n<span style="color: #666666">-1.0</span>)
|
||||
Cov[<span style="color: #666666">1</span>,<span style="color: #666666">1</span>] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(y<span style="color: #666666">.</span>T<span style="color: #AA22FF">@y</span>)<span style="color: #666666">/</span>(n<span style="color: #666666">-1.0</span>)
|
||||
Cov[<span style="color: #666666">1</span>,<span style="color: #666666">0</span>]<span style="color: #666666">=</span> Cov[<span style="color: #666666">0</span>,<span style="color: #666666">1</span>]
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Centered covariance using own code"</span>)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(Cov)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Centered covariance using own code"</span>)
|
||||
<span style="color: #008000">print</span>(Cov)
|
||||
plt<span style="color: #666666">.</span>plot(x, y, <span style="color: #BA2121">'x'</span>)
|
||||
plt<span style="color: #666666">.</span>axis(<span style="color: #BA2121">'equal'</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
@@ -813,7 +835,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
Depending on the number of points \( n \), we will get results that are close to the covariance values defined above.
|
||||
The plot shows how the data are clustered around a line with slope close to one. Is this expected?
|
||||
|
||||
<h3 id="___sec14">Diagonalize the sample covariance matrix to obtain the principal components </h3>
|
||||
<h3 id="___sec16">Diagonalize the sample covariance matrix to obtain the principal components </h3>
|
||||
|
||||
<p>
|
||||
Now we are ready to solve for the principal components! To do so we
|
||||
@@ -855,21 +877,21 @@ EigValues, EigVectors <span style="color: #666666">=</span> np<span style="color
|
||||
<span style="color: #408080; font-style: italic">#permute = EigValues.argsort()</span>
|
||||
<span style="color: #408080; font-style: italic">#EigValues = EigValues[permute]</span>
|
||||
<span style="color: #408080; font-style: italic">#EigVectors = EigVectors[:,permute]</span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Eigenvalues of Covariance matrix"</span>)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Eigenvalues of Covariance matrix"</span>)
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">2</span>):
|
||||
<span style="color: #008000; font-weight: bold">print</span>(EigValues[i])
|
||||
<span style="color: #008000">print</span>(EigValues[i])
|
||||
FirstEigvector <span style="color: #666666">=</span> EigVectors[:,<span style="color: #666666">0</span>]
|
||||
SecondEigvector <span style="color: #666666">=</span> EigVectors[:,<span style="color: #666666">1</span>]
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"First eigenvector"</span>)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(FirstEigvector)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Second eigenvector"</span>)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(SecondEigvector)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"First eigenvector"</span>)
|
||||
<span style="color: #008000">print</span>(FirstEigvector)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Second eigenvector"</span>)
|
||||
<span style="color: #008000">print</span>(SecondEigvector)
|
||||
<span style="color: #408080; font-style: italic">#thereafter we do a PCA with Scikit-learn</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.decomposition</span> <span style="color: #008000; font-weight: bold">import</span> PCA
|
||||
pca <span style="color: #666666">=</span> PCA(n_components <span style="color: #666666">=</span> <span style="color: #666666">2</span>)
|
||||
X2Dsl <span style="color: #666666">=</span> pca<span style="color: #666666">.</span>fit_transform(X)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Eigenvector of largest eigenvalue"</span>)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(pca<span style="color: #666666">.</span>components_<span style="color: #666666">.</span>T[:, <span style="color: #666666">0</span>])
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Eigenvector of largest eigenvalue"</span>)
|
||||
<span style="color: #008000">print</span>(pca<span style="color: #666666">.</span>components_<span style="color: #666666">.</span>T[:, <span style="color: #666666">0</span>])
|
||||
</pre></div>
|
||||
<p>
|
||||
This code does not contain all the above elements, but it shows how we can use <b>Scikit-Learn</b> to extract the eigenvector which corresponds to the largest eigenvalue. Try to address the questions we pose before the above code. Try also to change the values of the covariance matrix by making one of the diagonal elements much larger than the other. What do you observe then?
|
||||
@@ -877,7 +899,7 @@ This code does not contain all the above elements, but it shows how we can use <
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec15">Classical PCA Theorem </h2>
|
||||
<h2 id="___sec17">Classical PCA Theorem </h2>
|
||||
|
||||
<p>
|
||||
We assume now that we have a design matrix \( \boldsymbol{X} \) which has been
|
||||
@@ -910,7 +932,7 @@ The proof which follows will be updated by mid January 2020.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec16">Proof of the PCA Theorem </h2>
|
||||
<h2 id="___sec18">Proof of the PCA Theorem </h2>
|
||||
|
||||
<p>
|
||||
To show the PCA theorem let us start with the assumption that there is one vector \( \boldsymbol{w}_0 \) which corresponds to a solution which minimized the reconstruction error \( J \). This is an orthogonal vector. It means that we now approximate the reconstruction error in terms of \( \boldsymbol{w}_0 \) and \( \boldsymbol{z}_0 \) as
|
||||
@@ -933,7 +955,7 @@ where the vectors on the rhs are known.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec17">PCA Proof continued </h2>
|
||||
<h2 id="___sec19">PCA Proof continued </h2>
|
||||
|
||||
<p>
|
||||
We have now found the unknown parameters \( z_{i0} \). These correspond to the projected coordinates and we can write
|
||||
@@ -974,7 +996,7 @@ of the projected data.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec18">The final step </h2>
|
||||
<h2 id="___sec20">The final step </h2>
|
||||
|
||||
<p>
|
||||
We could trivially maximize the variance of the projection (and
|
||||
@@ -1028,7 +1050,7 @@ Additional part of the proof for the other eigenvectors will be added by mid Jan
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec19">Geometric Interpretation and link with Singular Value Decomposition </h2>
|
||||
<h2 id="___sec21">Geometric Interpretation and link with Singular Value Decomposition </h2>
|
||||
|
||||
<p>
|
||||
This material will be added by mid January 2020.
|
||||
@@ -1036,7 +1058,7 @@ This material will be added by mid January 2020.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec20">Principal Component Analysis </h2>
|
||||
<h2 id="___sec22">Principal Component Analysis </h2>
|
||||
|
||||
<p>
|
||||
Principal Component Analysis (PCA) is by far the most popular dimensionality reduction algorithm.
|
||||
@@ -1064,14 +1086,14 @@ display(df)
|
||||
<span style="color: #408080; font-style: italic"># we center it ourselves</span>
|
||||
X_centered <span style="color: #666666">=</span> X <span style="color: #666666">-</span> X<span style="color: #666666">.</span>mean(axis<span style="color: #666666">=0</span>)
|
||||
<span style="color: #408080; font-style: italic"># Then check the difference between pandas and our own set up</span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X_centered<span style="color: #666666">-</span>df)
|
||||
<span style="color: #008000">print</span>(X_centered<span style="color: #666666">-</span>df)
|
||||
<span style="color: #408080; font-style: italic">#Now we do an SVD</span>
|
||||
U, s, V <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>svd(X_centered)
|
||||
c1 <span style="color: #666666">=</span> V<span style="color: #666666">.</span>T[:, <span style="color: #666666">0</span>]
|
||||
c2 <span style="color: #666666">=</span> V<span style="color: #666666">.</span>T[:, <span style="color: #666666">1</span>]
|
||||
W2 <span style="color: #666666">=</span> V<span style="color: #666666">.</span>T[:, :<span style="color: #666666">2</span>]
|
||||
X2D <span style="color: #666666">=</span> X_centered<span style="color: #666666">.</span>dot(W2)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X2D)
|
||||
<span style="color: #008000">print</span>(X2D)
|
||||
</pre></div>
|
||||
<p>
|
||||
PCA assumes that the dataset is centered around the origin. Scikit-Learn’s PCA classes take care of centering
|
||||
@@ -1091,7 +1113,7 @@ X2D <span style="color: #666666">=</span> X_centered<span style="color: #666666"
|
||||
<p>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec21">PCA and scikit-learn </h2>
|
||||
<h2 id="___sec23">PCA and scikit-learn </h2>
|
||||
|
||||
<p>
|
||||
Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
|
||||
@@ -1104,7 +1126,7 @@ that it automatically takes care of centering the data):
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.decomposition</span> <span style="color: #008000; font-weight: bold">import</span> PCA
|
||||
pca <span style="color: #666666">=</span> PCA(n_components <span style="color: #666666">=</span> <span style="color: #666666">2</span>)
|
||||
X2D <span style="color: #666666">=</span> pca<span style="color: #666666">.</span>fit_transform(X)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X2D)
|
||||
<span style="color: #008000">print</span>(X2D)
|
||||
</pre></div>
|
||||
<p>
|
||||
After fitting the PCA transformer to the dataset, you can access the principal components using the
|
||||
@@ -1123,7 +1145,7 @@ variance that lies along the axis of each principal component.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec22">Back to the Cancer Data </h2>
|
||||
<h2 id="___sec24">Back to the Cancer Data </h2>
|
||||
We can now repeat the above but applied to real data, in this case our breast cancer data.
|
||||
Here we compute performance scores on the training data using logistic regression.
|
||||
<p>
|
||||
@@ -1140,7 +1162,7 @@ X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_tes
|
||||
|
||||
logreg <span style="color: #666666">=</span> LogisticRegression()
|
||||
logreg<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Train set accuracy from Logistic Regression: {:.2f}"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_train,y_train)))
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Train set accuracy from Logistic Regression: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_train,y_train)))
|
||||
<span style="color: #408080; font-style: italic"># We scale the data</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
|
||||
scaler <span style="color: #666666">=</span> StandardScaler()
|
||||
@@ -1149,14 +1171,14 @@ X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #
|
||||
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
|
||||
<span style="color: #408080; font-style: italic"># Then perform again a log reg fit</span>
|
||||
logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Train set accuracy scaled data: {:.2f}"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_train_scaled,y_train)))
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Train set accuracy scaled data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_train_scaled,y_train)))
|
||||
<span style="color: #408080; font-style: italic">#thereafter we do a PCA with Scikit-learn</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.decomposition</span> <span style="color: #008000; font-weight: bold">import</span> PCA
|
||||
pca <span style="color: #666666">=</span> PCA(n_components <span style="color: #666666">=</span> <span style="color: #666666">2</span>)
|
||||
X2D_train <span style="color: #666666">=</span> pca<span style="color: #666666">.</span>fit_transform(X_train_scaled)
|
||||
<span style="color: #408080; font-style: italic"># and finally compute the log reg fit and the score on the training data </span>
|
||||
logreg<span style="color: #666666">.</span>fit(X2D_train,y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Train set accuracy scaled and PCA data: {:.2f}"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X2D_train,y_train)))
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Train set accuracy scaled and PCA data: </span><span style="color: #BB6688; font-weight: bold">{:.2f}</span><span style="color: #BA2121">"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X2D_train,y_train)))
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that our training data after the PCA decomposition has a performance similar to the non-scaled data.
|
||||
@@ -1164,7 +1186,7 @@ We see that our training data after the PCA decomposition has a performance simi
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec23">More on the PCA </h2>
|
||||
<h2 id="___sec25">More on the PCA </h2>
|
||||
|
||||
<p>
|
||||
Instead of arbitrarily choosing the number of dimensions to reduce down to, it is generally preferable to
|
||||
@@ -1194,7 +1216,7 @@ X_reduced <span style="color: #666666">=</span> pca<span style="color: #666666">
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec24">Incremental PCA </h2>
|
||||
<h2 id="___sec26">Incremental PCA </h2>
|
||||
|
||||
<p>
|
||||
One problem with the preceding implementation of PCA is that it requires the whole training set to fit in
|
||||
@@ -1206,7 +1228,7 @@ instances arrive).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec25">Randomized PCA </h2>
|
||||
<h2 id="___sec27">Randomized PCA </h2>
|
||||
|
||||
<p>
|
||||
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
|
||||
@@ -1217,7 +1239,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec26">Kernel PCA </h2>
|
||||
<h2 id="___sec28">Kernel PCA </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -1246,7 +1268,7 @@ X_reduced <span style="color: #666666">=</span> rbf_pca<span style="color: #6666
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec27">LLE </h2>
|
||||
<h2 id="___sec29">LLE </h2>
|
||||
|
||||
<p>
|
||||
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
|
||||
@@ -1258,7 +1280,7 @@ these local relationships are best preserved (more details shortly).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec28">Other techniques </h2>
|
||||
<h2 id="___sec30">Other techniques </h2>
|
||||
|
||||
<p>
|
||||
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
|
||||
|
||||
Binary file not shown.
@@ -4,19 +4,31 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<!-- dom:TITLE: Week 43: Dimensionality Reduction -->\n",
|
||||
"# Week 43: Dimensionality Reduction\n",
|
||||
"<!-- dom:TITLE: Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods -->\n",
|
||||
"# Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods\n",
|
||||
"<!-- dom:AUTHOR: Morten Hjorth-Jensen at Department of Physics, University of Oslo & Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University -->\n",
|
||||
"<!-- Author: --> \n",
|
||||
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
|
||||
"\n",
|
||||
"Date: **Sep 16, 2020**\n",
|
||||
"Date: **Oct 17, 2020**\n",
|
||||
"\n",
|
||||
"Copyright 1999-2020, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"* Thursday: Wrapping up Recurrent Neural Networks and solving differential equations. \n",
|
||||
"\n",
|
||||
"* Friday: Principal Component Analysis and Dimensionality Reduction\n",
|
||||
"\n",
|
||||
"Reading suggestions for both days: \"Aurelien Geron's chapters 8 \n",
|
||||
"\n",
|
||||
"## Recurrent Neural Networks\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Solving ODEs with Deep Learning\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Why should we think of reducing the dimensionality\n",
|
||||
"\n",
|
||||
"In addition to the plot of the features, we study now also the covariance (and the correlation matrix).\n",
|
||||
@@ -1634,5 +1646,5 @@
|
||||
],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
|
||||
@@ -1,7 +1,21 @@
|
||||
TITLE: Week 43: Dimensionality Reduction
|
||||
TITLE: Week 43: Solving Differential Equations with Deep Learning and Dimensionality Reduction methods
|
||||
AUTHOR: Morten Hjorth-Jensen {copyright, 1999-present|CC BY-NC} at Department of Physics, University of Oslo & Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University
|
||||
DATE: today
|
||||
|
||||
!split
|
||||
|
||||
* Thursday: Wrapping up Recurrent Neural Networks and solving differential equations.
|
||||
* Friday: Principal Component Analysis and Dimensionality Reduction
|
||||
|
||||
Reading suggestions for both days: "Aurelien Geron's chapters 8
|
||||
|
||||
!split
|
||||
===== Recurrent Neural Networks =====
|
||||
|
||||
|
||||
!split
|
||||
===== Solving ODEs with Deep Learning =====
|
||||
|
||||
|
||||
!split
|
||||
===== Why should we think of reducing the dimensionality =====
|
||||
|
||||
Reference in New Issue
Block a user