dim red with new examples
This commit is contained in:
@@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source
|
||||
None,
|
||||
'___sec0'),
|
||||
('Preprocessing our data', 2, None, '___sec1'),
|
||||
('Simple preprocessing examples', 2, None, '___sec2'),
|
||||
('Principal Component Analysis', 2, None, '___sec3'),
|
||||
('PCA and scikit-learn', 2, None, '___sec4'),
|
||||
('More on the PCA', 2, None, '___sec5'),
|
||||
('Incremental PCA', 2, None, '___sec6'),
|
||||
('Randomized PCA', 2, None, '___sec7'),
|
||||
('Kernel PCA', 2, None, '___sec8'),
|
||||
('LLE', 2, None, '___sec9'),
|
||||
('Other techniques', 2, None, '___sec10')]}
|
||||
('Simple preprocessing examples, Franke function and regression',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Simple preprocessing examples, breast cancer data and '
|
||||
'classification',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Principal Component Analysis', 2, None, '___sec4'),
|
||||
('PCA and scikit-learn', 2, None, '___sec5'),
|
||||
('More on the PCA', 2, None, '___sec6'),
|
||||
('Incremental PCA', 2, None, '___sec7'),
|
||||
('Randomized PCA', 2, None, '___sec8'),
|
||||
('Kernel PCA', 2, None, '___sec9'),
|
||||
('LLE', 2, None, '___sec10'),
|
||||
('Other techniques', 2, None, '___sec11')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -94,15 +102,16 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs001.html#___sec0" style="font-size: 80%;">Reducing the number of degrees of freedom, overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs002.html#___sec1" style="font-size: 80%;">Preprocessing our data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Other techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples, Franke function and regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">Other techniques</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -137,7 +146,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>Oct 12, 2019</h4></center> <!-- date -->
|
||||
<center><h4>Oct 14, 2019</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
@@ -161,7 +170,7 @@ MathJax.Hub.Config({
|
||||
<li><a href="._DimRed-bs008.html">9</a></li>
|
||||
<li><a href="._DimRed-bs009.html">10</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DimRed-bs011.html">12</a></li>
|
||||
<li><a href="._DimRed-bs012.html">13</a></li>
|
||||
<li><a href="._DimRed-bs001.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source
|
||||
None,
|
||||
'___sec0'),
|
||||
('Preprocessing our data', 2, None, '___sec1'),
|
||||
('Simple preprocessing examples', 2, None, '___sec2'),
|
||||
('Principal Component Analysis', 2, None, '___sec3'),
|
||||
('PCA and scikit-learn', 2, None, '___sec4'),
|
||||
('More on the PCA', 2, None, '___sec5'),
|
||||
('Incremental PCA', 2, None, '___sec6'),
|
||||
('Randomized PCA', 2, None, '___sec7'),
|
||||
('Kernel PCA', 2, None, '___sec8'),
|
||||
('LLE', 2, None, '___sec9'),
|
||||
('Other techniques', 2, None, '___sec10')]}
|
||||
('Simple preprocessing examples, Franke function and regression',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Simple preprocessing examples, breast cancer data and '
|
||||
'classification',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Principal Component Analysis', 2, None, '___sec4'),
|
||||
('PCA and scikit-learn', 2, None, '___sec5'),
|
||||
('More on the PCA', 2, None, '___sec6'),
|
||||
('Incremental PCA', 2, None, '___sec7'),
|
||||
('Randomized PCA', 2, None, '___sec8'),
|
||||
('Kernel PCA', 2, None, '___sec9'),
|
||||
('LLE', 2, None, '___sec10'),
|
||||
('Other techniques', 2, None, '___sec11')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -94,15 +102,16 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="#___sec0" style="font-size: 80%;">Reducing the number of degrees of freedom, overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs002.html#___sec1" style="font-size: 80%;">Preprocessing our data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Other techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples, Franke function and regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">Other techniques</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -156,7 +165,7 @@ reduction techniques: the principal component analysis PCA, Kernel PCA, and Loca
|
||||
<li><a href="._DimRed-bs009.html">10</a></li>
|
||||
<li><a href="._DimRed-bs010.html">11</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DimRed-bs011.html">12</a></li>
|
||||
<li><a href="._DimRed-bs012.html">13</a></li>
|
||||
<li><a href="._DimRed-bs002.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source
|
||||
None,
|
||||
'___sec0'),
|
||||
('Preprocessing our data', 2, None, '___sec1'),
|
||||
('Simple preprocessing examples', 2, None, '___sec2'),
|
||||
('Principal Component Analysis', 2, None, '___sec3'),
|
||||
('PCA and scikit-learn', 2, None, '___sec4'),
|
||||
('More on the PCA', 2, None, '___sec5'),
|
||||
('Incremental PCA', 2, None, '___sec6'),
|
||||
('Randomized PCA', 2, None, '___sec7'),
|
||||
('Kernel PCA', 2, None, '___sec8'),
|
||||
('LLE', 2, None, '___sec9'),
|
||||
('Other techniques', 2, None, '___sec10')]}
|
||||
('Simple preprocessing examples, Franke function and regression',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Simple preprocessing examples, breast cancer data and '
|
||||
'classification',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Principal Component Analysis', 2, None, '___sec4'),
|
||||
('PCA and scikit-learn', 2, None, '___sec5'),
|
||||
('More on the PCA', 2, None, '___sec6'),
|
||||
('Incremental PCA', 2, None, '___sec7'),
|
||||
('Randomized PCA', 2, None, '___sec8'),
|
||||
('Kernel PCA', 2, None, '___sec9'),
|
||||
('LLE', 2, None, '___sec10'),
|
||||
('Other techniques', 2, None, '___sec11')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -94,15 +102,16 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs001.html#___sec0" style="font-size: 80%;">Reducing the number of degrees of freedom, overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec1" style="font-size: 80%;">Preprocessing our data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Other techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples, Franke function and regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">Other techniques</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -157,6 +166,8 @@ This scaling has the drawback that it does not ensure that we have a particular
|
||||
<li><a href="._DimRed-bs009.html">10</a></li>
|
||||
<li><a href="._DimRed-bs010.html">11</a></li>
|
||||
<li><a href="._DimRed-bs011.html">12</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DimRed-bs012.html">13</a></li>
|
||||
<li><a href="._DimRed-bs003.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source
|
||||
None,
|
||||
'___sec0'),
|
||||
('Preprocessing our data', 2, None, '___sec1'),
|
||||
('Simple preprocessing examples', 2, None, '___sec2'),
|
||||
('Principal Component Analysis', 2, None, '___sec3'),
|
||||
('PCA and scikit-learn', 2, None, '___sec4'),
|
||||
('More on the PCA', 2, None, '___sec5'),
|
||||
('Incremental PCA', 2, None, '___sec6'),
|
||||
('Randomized PCA', 2, None, '___sec7'),
|
||||
('Kernel PCA', 2, None, '___sec8'),
|
||||
('LLE', 2, None, '___sec9'),
|
||||
('Other techniques', 2, None, '___sec10')]}
|
||||
('Simple preprocessing examples, Franke function and regression',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Simple preprocessing examples, breast cancer data and '
|
||||
'classification',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Principal Component Analysis', 2, None, '___sec4'),
|
||||
('PCA and scikit-learn', 2, None, '___sec5'),
|
||||
('More on the PCA', 2, None, '___sec6'),
|
||||
('Incremental PCA', 2, None, '___sec7'),
|
||||
('Randomized PCA', 2, None, '___sec8'),
|
||||
('Kernel PCA', 2, None, '___sec9'),
|
||||
('LLE', 2, None, '___sec10'),
|
||||
('Other techniques', 2, None, '___sec11')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -94,15 +102,16 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs001.html#___sec0" style="font-size: 80%;">Reducing the number of degrees of freedom, overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs002.html#___sec1" style="font-size: 80%;">Preprocessing our data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec2" style="font-size: 80%;">Simple preprocessing examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Other techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec2" style="font-size: 80%;">Simple preprocessing examples, Franke function and regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">Other techniques</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -118,33 +127,90 @@ MathJax.Hub.Config({
|
||||
<a name="part0003"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec2" class="anchor">Simple preprocessing examples </h2>
|
||||
|
||||
<p>
|
||||
We show here how we can use a simple regression case (our nuclear binding energies discussed earlier).
|
||||
Rescaling our data with different
|
||||
<h2 id="___sec2" class="anchor">Simple preprocessing examples, Franke function and regression </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>
|
||||
<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">os</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.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
|
||||
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: #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">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skl</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> mean_squared_error
|
||||
<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.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> MinMaxScaler, StandardScaler, Normalizer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVR
|
||||
|
||||
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>)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X_train<span style="color: #666666">.</span>shape)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X_test<span style="color: #666666">.</span>shape)
|
||||
<span style="color: #408080; font-style: italic"># Where to save the figures and data files</span>
|
||||
PROJECT_ROOT_DIR <span style="color: #666666">=</span> <span style="color: #BA2121">"Results"</span>
|
||||
FIGURE_ID <span style="color: #666666">=</span> <span style="color: #BA2121">"Results/FigureFiles"</span>
|
||||
DATA_ID <span style="color: #666666">=</span> <span style="color: #BA2121">"DataFiles/"</span>
|
||||
|
||||
svm <span style="color: #666666">=</span> SVC(C<span style="color: #666666">=100</span>)
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(PROJECT_ROOT_DIR):
|
||||
os<span style="color: #666666">.</span>mkdir(PROJECT_ROOT_DIR)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(FIGURE_ID):
|
||||
os<span style="color: #666666">.</span>makedirs(FIGURE_ID)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(DATA_ID):
|
||||
os<span style="color: #666666">.</span>makedirs(DATA_ID)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">image_path</span>(fig_id):
|
||||
<span style="color: #008000; font-weight: bold">return</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(FIGURE_ID, fig_id)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">data_path</span>(dat_id):
|
||||
<span style="color: #008000; font-weight: bold">return</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(DATA_ID, dat_id)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">save_fig</span>(fig_id):
|
||||
plt<span style="color: #666666">.</span>savefig(image_path(fig_id) <span style="color: #666666">+</span> <span style="color: #BA2121">".png"</span>, format<span style="color: #666666">=</span><span style="color: #BA2121">'png'</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">5</span>
|
||||
N <span style="color: #666666">=</span> <span style="color: #666666">1000</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)
|
||||
<span style="color: #408080; font-style: italic"># split in training and test data</span>
|
||||
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X,z,test_size<span style="color: #666666">=0.2</span>)
|
||||
|
||||
|
||||
svm <span style="color: #666666">=</span> SVR(gamma<span style="color: #666666">=</span><span style="color: #BA2121">'auto'</span>,C<span style="color: #666666">=10.0</span>)
|
||||
svm<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Test set accuracy: {:.2f}"</span><span style="color: #666666">.</span>format(svm<span style="color: #666666">.</span>score(X_test,y_test)))
|
||||
|
||||
<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> MinMaxScaler, StandardScaler
|
||||
<span style="color: #408080; font-style: italic"># The mean squared error and R2 score</span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"MSE before scaling: {:.2f}"</span><span style="color: #666666">.</span>format(mean_squared_error(svm<span style="color: #666666">.</span>predict(X_test), y_test)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"R2 score before scaling {:.2f}"</span><span style="color: #666666">.</span>format(svm<span style="color: #666666">.</span>score(X_test,y_test)))
|
||||
|
||||
scaler <span style="color: #666666">=</span> MinMaxScaler()
|
||||
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)
|
||||
@@ -152,20 +218,14 @@ X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #6
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature min values before scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train<span style="color: #666666">.</span>min(axis<span style="color: #666666">=0</span>)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature max values before scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train<span style="color: #666666">.</span>max(axis<span style="color: #666666">=0</span>)))
|
||||
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature min values before scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train_scaled<span style="color: #666666">.</span>min(axis<span style="color: #666666">=0</span>)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature max values before scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train_scaled<span style="color: #666666">.</span>max(axis<span style="color: #666666">=0</span>)))
|
||||
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature min values after scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train_scaled<span style="color: #666666">.</span>min(axis<span style="color: #666666">=0</span>)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature max values after scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train_scaled<span style="color: #666666">.</span>max(axis<span style="color: #666666">=0</span>)))
|
||||
|
||||
svm <span style="color: #666666">=</span> SVR(gamma<span style="color: #666666">=</span><span style="color: #BA2121">'auto'</span>,C<span style="color: #666666">=10.0</span>)
|
||||
svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Test set accuracy scaled data: {:.2f}"</span><span style="color: #666666">.</span>format(svm<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
|
||||
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)
|
||||
|
||||
svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Test set accuracy scaled data: {:.2f}"</span><span style="color: #666666">.</span>format(svm<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"MSE after scaling: {:.2f}"</span><span style="color: #666666">.</span>format(mean_squared_error(svm<span style="color: #666666">.</span>predict(X_test_scaled), y_test)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"R2 score for scaled data: {:.2f}"</span><span style="color: #666666">.</span>format(svm<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
@@ -184,6 +244,7 @@ svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<li><a href="._DimRed-bs009.html">10</a></li>
|
||||
<li><a href="._DimRed-bs010.html">11</a></li>
|
||||
<li><a href="._DimRed-bs011.html">12</a></li>
|
||||
<li><a href="._DimRed-bs012.html">13</a></li>
|
||||
<li><a href="._DimRed-bs004.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source
|
||||
None,
|
||||
'___sec0'),
|
||||
('Preprocessing our data', 2, None, '___sec1'),
|
||||
('Simple preprocessing examples', 2, None, '___sec2'),
|
||||
('Principal Component Analysis', 2, None, '___sec3'),
|
||||
('PCA and scikit-learn', 2, None, '___sec4'),
|
||||
('More on the PCA', 2, None, '___sec5'),
|
||||
('Incremental PCA', 2, None, '___sec6'),
|
||||
('Randomized PCA', 2, None, '___sec7'),
|
||||
('Kernel PCA', 2, None, '___sec8'),
|
||||
('LLE', 2, None, '___sec9'),
|
||||
('Other techniques', 2, None, '___sec10')]}
|
||||
('Simple preprocessing examples, Franke function and regression',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Simple preprocessing examples, breast cancer data and '
|
||||
'classification',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Principal Component Analysis', 2, None, '___sec4'),
|
||||
('PCA and scikit-learn', 2, None, '___sec5'),
|
||||
('More on the PCA', 2, None, '___sec6'),
|
||||
('Incremental PCA', 2, None, '___sec7'),
|
||||
('Randomized PCA', 2, None, '___sec8'),
|
||||
('Kernel PCA', 2, None, '___sec9'),
|
||||
('LLE', 2, None, '___sec10'),
|
||||
('Other techniques', 2, None, '___sec11')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -94,15 +102,16 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs001.html#___sec0" style="font-size: 80%;">Reducing the number of degrees of freedom, overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs002.html#___sec1" style="font-size: 80%;">Preprocessing our data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec3" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Other techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples, Franke function and regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">Other techniques</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -118,38 +127,53 @@ MathJax.Hub.Config({
|
||||
<a name="part0004"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec3" class="anchor">Principal Component Analysis </h2>
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<p> <!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
|
||||
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.
|
||||
<h2 id="___sec3" class="anchor">Simple preprocessing examples, breast cancer data and classification </h2>
|
||||
|
||||
<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
|
||||
We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification
|
||||
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></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>)
|
||||
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>]
|
||||
</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.
|
||||
<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.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
|
||||
cancer <span style="color: #666666">=</span> load_breast_cancer()
|
||||
|
||||
<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>
|
||||
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>)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X_train<span style="color: #666666">.</span>shape)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X_test<span style="color: #666666">.</span>shape)
|
||||
|
||||
<!-- 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)
|
||||
svm <span style="color: #666666">=</span> SVC(C<span style="color: #666666">=100</span>)
|
||||
svm<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Test set accuracy: {:.2f}"</span><span style="color: #666666">.</span>format(svm<span style="color: #666666">.</span>score(X_test,y_test)))
|
||||
|
||||
<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> MinMaxScaler, StandardScaler
|
||||
|
||||
scaler <span style="color: #666666">=</span> MinMaxScaler()
|
||||
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: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature min values before scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train<span style="color: #666666">.</span>min(axis<span style="color: #666666">=0</span>)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature max values before scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train<span style="color: #666666">.</span>max(axis<span style="color: #666666">=0</span>)))
|
||||
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature min values before scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train_scaled<span style="color: #666666">.</span>min(axis<span style="color: #666666">=0</span>)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature max values before scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train_scaled<span style="color: #666666">.</span>max(axis<span style="color: #666666">=0</span>)))
|
||||
|
||||
|
||||
svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Test set accuracy scaled data: {:.2f}"</span><span style="color: #666666">.</span>format(svm<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
|
||||
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)
|
||||
|
||||
svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Test set accuracy scaled data: {:.2f}"</span><span style="color: #666666">.</span>format(svm<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
</pre></div>
|
||||
<p>
|
||||
<p>
|
||||
@@ -168,6 +192,7 @@ X2D <span style="color: #666666">=</span> X_centered<span style="color: #666666"
|
||||
<li><a href="._DimRed-bs009.html">10</a></li>
|
||||
<li><a href="._DimRed-bs010.html">11</a></li>
|
||||
<li><a href="._DimRed-bs011.html">12</a></li>
|
||||
<li><a href="._DimRed-bs012.html">13</a></li>
|
||||
<li><a href="._DimRed-bs005.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source
|
||||
None,
|
||||
'___sec0'),
|
||||
('Preprocessing our data', 2, None, '___sec1'),
|
||||
('Simple preprocessing examples', 2, None, '___sec2'),
|
||||
('Principal Component Analysis', 2, None, '___sec3'),
|
||||
('PCA and scikit-learn', 2, None, '___sec4'),
|
||||
('More on the PCA', 2, None, '___sec5'),
|
||||
('Incremental PCA', 2, None, '___sec6'),
|
||||
('Randomized PCA', 2, None, '___sec7'),
|
||||
('Kernel PCA', 2, None, '___sec8'),
|
||||
('LLE', 2, None, '___sec9'),
|
||||
('Other techniques', 2, None, '___sec10')]}
|
||||
('Simple preprocessing examples, Franke function and regression',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Simple preprocessing examples, breast cancer data and '
|
||||
'classification',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Principal Component Analysis', 2, None, '___sec4'),
|
||||
('PCA and scikit-learn', 2, None, '___sec5'),
|
||||
('More on the PCA', 2, None, '___sec6'),
|
||||
('Incremental PCA', 2, None, '___sec7'),
|
||||
('Randomized PCA', 2, None, '___sec8'),
|
||||
('Kernel PCA', 2, None, '___sec9'),
|
||||
('LLE', 2, None, '___sec10'),
|
||||
('Other techniques', 2, None, '___sec11')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -94,15 +102,16 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs001.html#___sec0" style="font-size: 80%;">Reducing the number of degrees of freedom, overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs002.html#___sec1" style="font-size: 80%;">Preprocessing our data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec4" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Other techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples, Franke function and regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec4" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">Other techniques</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -116,36 +125,41 @@ MathJax.Hub.Config({
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
|
||||
<a name="part0005"></a>
|
||||
<!-- !split -->
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec4" class="anchor">PCA and scikit-learn </h2>
|
||||
<h2 id="___sec4" class="anchor">Principal Component Analysis </h2>
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<p> <!-- subsequent paragraphs come in larger fonts, so start with a paragraph -->
|
||||
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>
|
||||
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):
|
||||
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
|
||||
<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> 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)
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></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>)
|
||||
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>]
|
||||
</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
|
||||
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>components_<span style="color: #666666">.</span>T[:, <span style="color: #666666">0</span>])<span style="color: #666666">.</span>
|
||||
<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>
|
||||
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.
|
||||
More material to come here.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -163,6 +177,7 @@ More material to come here.
|
||||
<li><a href="._DimRed-bs009.html">10</a></li>
|
||||
<li><a href="._DimRed-bs010.html">11</a></li>
|
||||
<li><a href="._DimRed-bs011.html">12</a></li>
|
||||
<li><a href="._DimRed-bs012.html">13</a></li>
|
||||
<li><a href="._DimRed-bs006.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source
|
||||
None,
|
||||
'___sec0'),
|
||||
('Preprocessing our data', 2, None, '___sec1'),
|
||||
('Simple preprocessing examples', 2, None, '___sec2'),
|
||||
('Principal Component Analysis', 2, None, '___sec3'),
|
||||
('PCA and scikit-learn', 2, None, '___sec4'),
|
||||
('More on the PCA', 2, None, '___sec5'),
|
||||
('Incremental PCA', 2, None, '___sec6'),
|
||||
('Randomized PCA', 2, None, '___sec7'),
|
||||
('Kernel PCA', 2, None, '___sec8'),
|
||||
('LLE', 2, None, '___sec9'),
|
||||
('Other techniques', 2, None, '___sec10')]}
|
||||
('Simple preprocessing examples, Franke function and regression',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Simple preprocessing examples, breast cancer data and '
|
||||
'classification',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Principal Component Analysis', 2, None, '___sec4'),
|
||||
('PCA and scikit-learn', 2, None, '___sec5'),
|
||||
('More on the PCA', 2, None, '___sec6'),
|
||||
('Incremental PCA', 2, None, '___sec7'),
|
||||
('Randomized PCA', 2, None, '___sec8'),
|
||||
('Kernel PCA', 2, None, '___sec9'),
|
||||
('LLE', 2, None, '___sec10'),
|
||||
('Other techniques', 2, None, '___sec11')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -94,15 +102,16 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs001.html#___sec0" style="font-size: 80%;">Reducing the number of degrees of freedom, overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs002.html#___sec1" style="font-size: 80%;">Preprocessing our data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec5" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Other techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples, Franke function and regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec5" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">Other techniques</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -116,33 +125,36 @@ MathJax.Hub.Config({
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
|
||||
<a name="part0006"></a>
|
||||
<!-- !split -->
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec5" class="anchor">More on the PCA </h2>
|
||||
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:
|
||||
<h2 id="___sec5" class="anchor">PCA and scikit-learn </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>
|
||||
|
||||
<!-- 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">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)
|
||||
</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:
|
||||
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> 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>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.
|
||||
More material to come here.
|
||||
|
||||
<p>
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -160,6 +172,7 @@ X_reduced <span style="color: #666666">=</span> pca<span style="color: #666666">
|
||||
<li><a href="._DimRed-bs009.html">10</a></li>
|
||||
<li><a href="._DimRed-bs010.html">11</a></li>
|
||||
<li><a href="._DimRed-bs011.html">12</a></li>
|
||||
<li><a href="._DimRed-bs012.html">13</a></li>
|
||||
<li><a href="._DimRed-bs007.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source
|
||||
None,
|
||||
'___sec0'),
|
||||
('Preprocessing our data', 2, None, '___sec1'),
|
||||
('Simple preprocessing examples', 2, None, '___sec2'),
|
||||
('Principal Component Analysis', 2, None, '___sec3'),
|
||||
('PCA and scikit-learn', 2, None, '___sec4'),
|
||||
('More on the PCA', 2, None, '___sec5'),
|
||||
('Incremental PCA', 2, None, '___sec6'),
|
||||
('Randomized PCA', 2, None, '___sec7'),
|
||||
('Kernel PCA', 2, None, '___sec8'),
|
||||
('LLE', 2, None, '___sec9'),
|
||||
('Other techniques', 2, None, '___sec10')]}
|
||||
('Simple preprocessing examples, Franke function and regression',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Simple preprocessing examples, breast cancer data and '
|
||||
'classification',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Principal Component Analysis', 2, None, '___sec4'),
|
||||
('PCA and scikit-learn', 2, None, '___sec5'),
|
||||
('More on the PCA', 2, None, '___sec6'),
|
||||
('Incremental PCA', 2, None, '___sec7'),
|
||||
('Randomized PCA', 2, None, '___sec8'),
|
||||
('Kernel PCA', 2, None, '___sec9'),
|
||||
('LLE', 2, None, '___sec10'),
|
||||
('Other techniques', 2, None, '___sec11')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -94,15 +102,16 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs001.html#___sec0" style="font-size: 80%;">Reducing the number of degrees of freedom, overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs002.html#___sec1" style="font-size: 80%;">Preprocessing our data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Other techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples, Franke function and regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">Other techniques</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -118,13 +127,31 @@ MathJax.Hub.Config({
|
||||
<a name="part0007"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec6" class="anchor">Incremental PCA </h2>
|
||||
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).
|
||||
<h2 id="___sec6" class="anchor">More on the PCA </h2>
|
||||
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>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>
|
||||
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 -->
|
||||
@@ -142,6 +169,7 @@ instances arrive).
|
||||
<li><a href="._DimRed-bs009.html">10</a></li>
|
||||
<li><a href="._DimRed-bs010.html">11</a></li>
|
||||
<li><a href="._DimRed-bs011.html">12</a></li>
|
||||
<li><a href="._DimRed-bs012.html">13</a></li>
|
||||
<li><a href="._DimRed-bs008.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source
|
||||
None,
|
||||
'___sec0'),
|
||||
('Preprocessing our data', 2, None, '___sec1'),
|
||||
('Simple preprocessing examples', 2, None, '___sec2'),
|
||||
('Principal Component Analysis', 2, None, '___sec3'),
|
||||
('PCA and scikit-learn', 2, None, '___sec4'),
|
||||
('More on the PCA', 2, None, '___sec5'),
|
||||
('Incremental PCA', 2, None, '___sec6'),
|
||||
('Randomized PCA', 2, None, '___sec7'),
|
||||
('Kernel PCA', 2, None, '___sec8'),
|
||||
('LLE', 2, None, '___sec9'),
|
||||
('Other techniques', 2, None, '___sec10')]}
|
||||
('Simple preprocessing examples, Franke function and regression',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Simple preprocessing examples, breast cancer data and '
|
||||
'classification',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Principal Component Analysis', 2, None, '___sec4'),
|
||||
('PCA and scikit-learn', 2, None, '___sec5'),
|
||||
('More on the PCA', 2, None, '___sec6'),
|
||||
('Incremental PCA', 2, None, '___sec7'),
|
||||
('Randomized PCA', 2, None, '___sec8'),
|
||||
('Kernel PCA', 2, None, '___sec9'),
|
||||
('LLE', 2, None, '___sec10'),
|
||||
('Other techniques', 2, None, '___sec11')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -94,15 +102,16 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs001.html#___sec0" style="font-size: 80%;">Reducing the number of degrees of freedom, overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs002.html#___sec1" style="font-size: 80%;">Preprocessing our data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Other techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples, Franke function and regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">Other techniques</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -118,18 +127,12 @@ MathJax.Hub.Config({
|
||||
<a name="part0008"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec7" class="anchor">Randomized PCA </h2>
|
||||
|
||||
<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 \).
|
||||
|
||||
<p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2 id="___sec7" class="anchor">Incremental PCA </h2>
|
||||
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>
|
||||
@@ -148,6 +151,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
|
||||
<li><a href="._DimRed-bs009.html">10</a></li>
|
||||
<li><a href="._DimRed-bs010.html">11</a></li>
|
||||
<li><a href="._DimRed-bs011.html">12</a></li>
|
||||
<li><a href="._DimRed-bs012.html">13</a></li>
|
||||
<li><a href="._DimRed-bs009.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source
|
||||
None,
|
||||
'___sec0'),
|
||||
('Preprocessing our data', 2, None, '___sec1'),
|
||||
('Simple preprocessing examples', 2, None, '___sec2'),
|
||||
('Principal Component Analysis', 2, None, '___sec3'),
|
||||
('PCA and scikit-learn', 2, None, '___sec4'),
|
||||
('More on the PCA', 2, None, '___sec5'),
|
||||
('Incremental PCA', 2, None, '___sec6'),
|
||||
('Randomized PCA', 2, None, '___sec7'),
|
||||
('Kernel PCA', 2, None, '___sec8'),
|
||||
('LLE', 2, None, '___sec9'),
|
||||
('Other techniques', 2, None, '___sec10')]}
|
||||
('Simple preprocessing examples, Franke function and regression',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Simple preprocessing examples, breast cancer data and '
|
||||
'classification',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Principal Component Analysis', 2, None, '___sec4'),
|
||||
('PCA and scikit-learn', 2, None, '___sec5'),
|
||||
('More on the PCA', 2, None, '___sec6'),
|
||||
('Incremental PCA', 2, None, '___sec7'),
|
||||
('Randomized PCA', 2, None, '___sec8'),
|
||||
('Kernel PCA', 2, None, '___sec9'),
|
||||
('LLE', 2, None, '___sec10'),
|
||||
('Other techniques', 2, None, '___sec11')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -94,15 +102,16 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs001.html#___sec0" style="font-size: 80%;">Reducing the number of degrees of freedom, overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs002.html#___sec1" style="font-size: 80%;">Preprocessing our data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Other techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples, Franke function and regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">Other techniques</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -118,28 +127,14 @@ MathJax.Hub.Config({
|
||||
<a name="part0009"></a>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec8" 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="___sec8" class="anchor">Randomized 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
|
||||
<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">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)
|
||||
</pre></div>
|
||||
<p>
|
||||
</div>
|
||||
</div>
|
||||
@@ -162,6 +157,7 @@ X_reduced <span style="color: #666666">=</span> rbf_pca<span style="color: #6666
|
||||
<li class="active"><a href="._DimRed-bs009.html">10</a></li>
|
||||
<li><a href="._DimRed-bs010.html">11</a></li>
|
||||
<li><a href="._DimRed-bs011.html">12</a></li>
|
||||
<li><a href="._DimRed-bs012.html">13</a></li>
|
||||
<li><a href="._DimRed-bs010.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -46,15 +46,23 @@ Automatically generated HTML file from DocOnce source
|
||||
None,
|
||||
'___sec0'),
|
||||
('Preprocessing our data', 2, None, '___sec1'),
|
||||
('Simple preprocessing examples', 2, None, '___sec2'),
|
||||
('Principal Component Analysis', 2, None, '___sec3'),
|
||||
('PCA and scikit-learn', 2, None, '___sec4'),
|
||||
('More on the PCA', 2, None, '___sec5'),
|
||||
('Incremental PCA', 2, None, '___sec6'),
|
||||
('Randomized PCA', 2, None, '___sec7'),
|
||||
('Kernel PCA', 2, None, '___sec8'),
|
||||
('LLE', 2, None, '___sec9'),
|
||||
('Other techniques', 2, None, '___sec10')]}
|
||||
('Simple preprocessing examples, Franke function and regression',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Simple preprocessing examples, breast cancer data and '
|
||||
'classification',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Principal Component Analysis', 2, None, '___sec4'),
|
||||
('PCA and scikit-learn', 2, None, '___sec5'),
|
||||
('More on the PCA', 2, None, '___sec6'),
|
||||
('Incremental PCA', 2, None, '___sec7'),
|
||||
('Randomized PCA', 2, None, '___sec8'),
|
||||
('Kernel PCA', 2, None, '___sec9'),
|
||||
('LLE', 2, None, '___sec10'),
|
||||
('Other techniques', 2, None, '___sec11')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -94,15 +102,16 @@ MathJax.Hub.Config({
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs001.html#___sec0" style="font-size: 80%;">Reducing the number of degrees of freedom, overarching view</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs002.html#___sec1" style="font-size: 80%;">Preprocessing our data</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Other techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs003.html#___sec2" style="font-size: 80%;">Simple preprocessing examples, Franke function and regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">Principal Component Analysis</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">PCA and scikit-learn</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">More on the PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">Incremental PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Randomized PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Kernel PCA</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">LLE</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">Other techniques</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -137,7 +146,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>Oct 12, 2019</h4></center> <!-- date -->
|
||||
<center><h4>Oct 14, 2019</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
@@ -161,7 +170,7 @@ MathJax.Hub.Config({
|
||||
<li><a href="._DimRed-bs008.html">9</a></li>
|
||||
<li><a href="._DimRed-bs009.html">10</a></li>
|
||||
<li><a href="">...</a></li>
|
||||
<li><a href="._DimRed-bs011.html">12</a></li>
|
||||
<li><a href="._DimRed-bs012.html">13</a></li>
|
||||
<li><a href="._DimRed-bs001.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
@@ -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>Oct 12, 2019</h4></center> <!-- date -->
|
||||
<center><h4>Oct 14, 2019</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
@@ -200,11 +200,114 @@ This scaling has the drawback that it does not ensure that we have a particular
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec2">Simple preprocessing examples </h2>
|
||||
<h2 id="___sec2">Simple preprocessing examples, Franke function and regression </h2>
|
||||
|
||||
<p>
|
||||
We show here how we can use a simple regression case (our nuclear binding energies discussed earlier).
|
||||
Rescaling our data with different
|
||||
|
||||
<!-- 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: #228B22"># Common imports</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">os</span>
|
||||
<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>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">skl</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> mean_squared_error
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
||||
<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> MinMaxScaler, StandardScaler, Normalizer
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.svm</span> <span style="color: #8B008B; font-weight: bold">import</span> SVR
|
||||
|
||||
<span style="color: #228B22"># Where to save the figures and data files</span>
|
||||
PROJECT_ROOT_DIR = <span style="color: #CD5555">"Results"</span>
|
||||
FIGURE_ID = <span style="color: #CD5555">"Results/FigureFiles"</span>
|
||||
DATA_ID = <span style="color: #CD5555">"DataFiles/"</span>
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #8B008B">not</span> os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #8B008B">not</span> os.path.exists(FIGURE_ID):
|
||||
os.makedirs(FIGURE_ID)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #8B008B">not</span> os.path.exists(DATA_ID):
|
||||
os.makedirs(DATA_ID)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">image_path</span>(fig_id):
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> os.path.join(FIGURE_ID, fig_id)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">data_path</span>(dat_id):
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> os.path.join(DATA_ID, dat_id)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">save_fig</span>(fig_id):
|
||||
plt.savefig(image_path(fig_id) + <span style="color: #CD5555">".png"</span>, format=<span style="color: #CD5555">'png'</span>)
|
||||
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">FrankeFunction</span>(x,y):
|
||||
term1 = <span style="color: #B452CD">0.75</span>*np.exp(-(<span style="color: #B452CD">0.25</span>*(<span style="color: #B452CD">9</span>*x-<span style="color: #B452CD">2</span>)**<span style="color: #B452CD">2</span>) - <span style="color: #B452CD">0.25</span>*((<span style="color: #B452CD">9</span>*y-<span style="color: #B452CD">2</span>)**<span style="color: #B452CD">2</span>))
|
||||
term2 = <span style="color: #B452CD">0.75</span>*np.exp(-((<span style="color: #B452CD">9</span>*x+<span style="color: #B452CD">1</span>)**<span style="color: #B452CD">2</span>)/<span style="color: #B452CD">49.0</span> - <span style="color: #B452CD">0.1</span>*(<span style="color: #B452CD">9</span>*y+<span style="color: #B452CD">1</span>))
|
||||
term3 = <span style="color: #B452CD">0.5</span>*np.exp(-(<span style="color: #B452CD">9</span>*x-<span style="color: #B452CD">7</span>)**<span style="color: #B452CD">2</span>/<span style="color: #B452CD">4.0</span> - <span style="color: #B452CD">0.25</span>*((<span style="color: #B452CD">9</span>*y-<span style="color: #B452CD">3</span>)**<span style="color: #B452CD">2</span>))
|
||||
term4 = -<span style="color: #B452CD">0.2</span>*np.exp(-(<span style="color: #B452CD">9</span>*x-<span style="color: #B452CD">4</span>)**<span style="color: #B452CD">2</span> - (<span style="color: #B452CD">9</span>*y-<span style="color: #B452CD">7</span>)**<span style="color: #B452CD">2</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> term1 + term2 + term3 + term4
|
||||
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_X</span>(x, y, n ):
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">len</span>(x.shape) > <span style="color: #B452CD">1</span>:
|
||||
x = np.ravel(x)
|
||||
y = np.ravel(y)
|
||||
|
||||
N = <span style="color: #658b00">len</span>(x)
|
||||
l = <span style="color: #658b00">int</span>((n+<span style="color: #B452CD">1</span>)*(n+<span style="color: #B452CD">2</span>)/<span style="color: #B452CD">2</span>) <span style="color: #228B22"># Number of elements in beta</span>
|
||||
X = np.ones((N,l))
|
||||
|
||||
<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">1</span>,n+<span style="color: #B452CD">1</span>):
|
||||
q = <span style="color: #658b00">int</span>((i)*(i+<span style="color: #B452CD">1</span>)/<span style="color: #B452CD">2</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> k <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(i+<span style="color: #B452CD">1</span>):
|
||||
X[:,q+k] = (x**(i-k))*(y**k)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> X
|
||||
|
||||
|
||||
<span style="color: #228B22"># Making meshgrid of datapoints and compute Franke's function</span>
|
||||
n = <span style="color: #B452CD">5</span>
|
||||
N = <span style="color: #B452CD">1000</span>
|
||||
x = np.sort(np.random.uniform(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, N))
|
||||
y = np.sort(np.random.uniform(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, N))
|
||||
z = FrankeFunction(x, y)
|
||||
X = create_X(x, y, n=n)
|
||||
<span style="color: #228B22"># split in training and test data</span>
|
||||
X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=<span style="color: #B452CD">0.2</span>)
|
||||
|
||||
|
||||
svm = SVR(gamma=<span style="color: #CD5555">'auto'</span>,C=<span style="color: #B452CD">10.0</span>)
|
||||
svm.fit(X_train, y_train)
|
||||
|
||||
<span style="color: #228B22"># The mean squared error and R2 score</span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"MSE before scaling: {:.2f}"</span>.format(mean_squared_error(svm.predict(X_test), y_test)))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"R2 score before scaling {:.2f}"</span>.format(svm.score(X_test,y_test)))
|
||||
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature min values before scaling:\n {}"</span>.format(X_train.min(axis=<span style="color: #B452CD">0</span>)))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature max values before scaling:\n {}"</span>.format(X_train.max(axis=<span style="color: #B452CD">0</span>)))
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature min values after scaling:\n {}"</span>.format(X_train_scaled.min(axis=<span style="color: #B452CD">0</span>)))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature max values after scaling:\n {}"</span>.format(X_train_scaled.max(axis=<span style="color: #B452CD">0</span>)))
|
||||
|
||||
svm = SVR(gamma=<span style="color: #CD5555">'auto'</span>,C=<span style="color: #B452CD">10.0</span>)
|
||||
svm.fit(X_train_scaled, y_train)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"MSE after scaling: {:.2f}"</span>.format(mean_squared_error(svm.predict(X_test_scaled), y_test)))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"R2 score for scaled data: {:.2f}"</span>.format(svm.score(X_test_scaled,y_test)))
|
||||
</pre></div>
|
||||
</section>
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec3">Simple preprocessing examples, breast cancer data and classification </h2>
|
||||
|
||||
<p>
|
||||
We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification
|
||||
|
||||
<p>
|
||||
|
||||
@@ -253,7 +356,7 @@ svm.fit(X_train_scaled, y_train)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec3">Principal Component Analysis </h2>
|
||||
<h2 id="___sec4">Principal Component Analysis </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -290,7 +393,7 @@ X2D = X_centered.dot(W2)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec4">PCA and scikit-learn </h2>
|
||||
<h2 id="___sec5">PCA and scikit-learn </h2>
|
||||
|
||||
<p>
|
||||
Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
|
||||
@@ -321,7 +424,7 @@ More material to come here.
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec5">More on the PCA </h2>
|
||||
<h2 id="___sec6">More on the PCA </h2>
|
||||
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
|
||||
@@ -350,7 +453,7 @@ X_reduced = pca.fit_transform(X)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec6">Incremental PCA </h2>
|
||||
<h2 id="___sec7">Incremental PCA </h2>
|
||||
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
|
||||
@@ -360,7 +463,7 @@ instances arrive).
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec7">Randomized PCA </h2>
|
||||
<h2 id="___sec8">Randomized PCA </h2>
|
||||
|
||||
<p>
|
||||
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
|
||||
@@ -374,7 +477,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec8">Kernel PCA </h2>
|
||||
<h2 id="___sec9">Kernel PCA </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -400,7 +503,7 @@ X_reduced = rbf_pca.fit_transform(X)
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec9">LLE </h2>
|
||||
<h2 id="___sec10">LLE </h2>
|
||||
|
||||
<p>
|
||||
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
|
||||
@@ -412,7 +515,7 @@ these local relationships are best preserved (more details shortly).
|
||||
|
||||
|
||||
<section>
|
||||
<h2 id="___sec10">Other techniques </h2>
|
||||
<h2 id="___sec11">Other techniques </h2>
|
||||
|
||||
<p>
|
||||
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
|
||||
|
||||
@@ -66,15 +66,23 @@ div { text-align: justify; text-justify: inter-word; }
|
||||
None,
|
||||
'___sec0'),
|
||||
('Preprocessing our data', 2, None, '___sec1'),
|
||||
('Simple preprocessing examples', 2, None, '___sec2'),
|
||||
('Principal Component Analysis', 2, None, '___sec3'),
|
||||
('PCA and scikit-learn', 2, None, '___sec4'),
|
||||
('More on the PCA', 2, None, '___sec5'),
|
||||
('Incremental PCA', 2, None, '___sec6'),
|
||||
('Randomized PCA', 2, None, '___sec7'),
|
||||
('Kernel PCA', 2, None, '___sec8'),
|
||||
('LLE', 2, None, '___sec9'),
|
||||
('Other techniques', 2, None, '___sec10')]}
|
||||
('Simple preprocessing examples, Franke function and regression',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Simple preprocessing examples, breast cancer data and '
|
||||
'classification',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Principal Component Analysis', 2, None, '___sec4'),
|
||||
('PCA and scikit-learn', 2, None, '___sec5'),
|
||||
('More on the PCA', 2, None, '___sec6'),
|
||||
('Incremental PCA', 2, None, '___sec7'),
|
||||
('Randomized PCA', 2, None, '___sec8'),
|
||||
('Kernel PCA', 2, None, '___sec9'),
|
||||
('LLE', 2, None, '___sec10'),
|
||||
('Other techniques', 2, None, '___sec11')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -116,7 +124,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>Oct 12, 2019</h4></center> <!-- date -->
|
||||
<center><h4>Oct 14, 2019</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -168,11 +176,113 @@ This scaling has the drawback that it does not ensure that we have a particular
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec2">Simple preprocessing examples </h2>
|
||||
<h2 id="___sec2">Simple preprocessing examples, Franke function and regression </h2>
|
||||
|
||||
<p>
|
||||
We show here how we can use a simple regression case (our nuclear binding energies discussed earlier).
|
||||
Rescaling our data with different
|
||||
|
||||
<!-- 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: #228B22"># Common imports</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">os</span>
|
||||
<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>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">skl</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.metrics</span> <span style="color: #8B008B; font-weight: bold">import</span> mean_squared_error
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.model_selection</span> <span style="color: #8B008B; font-weight: bold">import</span> train_test_split
|
||||
<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> MinMaxScaler, StandardScaler, Normalizer
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.svm</span> <span style="color: #8B008B; font-weight: bold">import</span> SVR
|
||||
|
||||
<span style="color: #228B22"># Where to save the figures and data files</span>
|
||||
PROJECT_ROOT_DIR = <span style="color: #CD5555">"Results"</span>
|
||||
FIGURE_ID = <span style="color: #CD5555">"Results/FigureFiles"</span>
|
||||
DATA_ID = <span style="color: #CD5555">"DataFiles/"</span>
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #8B008B">not</span> os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #8B008B">not</span> os.path.exists(FIGURE_ID):
|
||||
os.makedirs(FIGURE_ID)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #8B008B">not</span> os.path.exists(DATA_ID):
|
||||
os.makedirs(DATA_ID)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">image_path</span>(fig_id):
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> os.path.join(FIGURE_ID, fig_id)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">data_path</span>(dat_id):
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> os.path.join(DATA_ID, dat_id)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">save_fig</span>(fig_id):
|
||||
plt.savefig(image_path(fig_id) + <span style="color: #CD5555">".png"</span>, format=<span style="color: #CD5555">'png'</span>)
|
||||
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">FrankeFunction</span>(x,y):
|
||||
term1 = <span style="color: #B452CD">0.75</span>*np.exp(-(<span style="color: #B452CD">0.25</span>*(<span style="color: #B452CD">9</span>*x-<span style="color: #B452CD">2</span>)**<span style="color: #B452CD">2</span>) - <span style="color: #B452CD">0.25</span>*((<span style="color: #B452CD">9</span>*y-<span style="color: #B452CD">2</span>)**<span style="color: #B452CD">2</span>))
|
||||
term2 = <span style="color: #B452CD">0.75</span>*np.exp(-((<span style="color: #B452CD">9</span>*x+<span style="color: #B452CD">1</span>)**<span style="color: #B452CD">2</span>)/<span style="color: #B452CD">49.0</span> - <span style="color: #B452CD">0.1</span>*(<span style="color: #B452CD">9</span>*y+<span style="color: #B452CD">1</span>))
|
||||
term3 = <span style="color: #B452CD">0.5</span>*np.exp(-(<span style="color: #B452CD">9</span>*x-<span style="color: #B452CD">7</span>)**<span style="color: #B452CD">2</span>/<span style="color: #B452CD">4.0</span> - <span style="color: #B452CD">0.25</span>*((<span style="color: #B452CD">9</span>*y-<span style="color: #B452CD">3</span>)**<span style="color: #B452CD">2</span>))
|
||||
term4 = -<span style="color: #B452CD">0.2</span>*np.exp(-(<span style="color: #B452CD">9</span>*x-<span style="color: #B452CD">4</span>)**<span style="color: #B452CD">2</span> - (<span style="color: #B452CD">9</span>*y-<span style="color: #B452CD">7</span>)**<span style="color: #B452CD">2</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> term1 + term2 + term3 + term4
|
||||
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">create_X</span>(x, y, n ):
|
||||
<span style="color: #8B008B; font-weight: bold">if</span> <span style="color: #658b00">len</span>(x.shape) > <span style="color: #B452CD">1</span>:
|
||||
x = np.ravel(x)
|
||||
y = np.ravel(y)
|
||||
|
||||
N = <span style="color: #658b00">len</span>(x)
|
||||
l = <span style="color: #658b00">int</span>((n+<span style="color: #B452CD">1</span>)*(n+<span style="color: #B452CD">2</span>)/<span style="color: #B452CD">2</span>) <span style="color: #228B22"># Number of elements in beta</span>
|
||||
X = np.ones((N,l))
|
||||
|
||||
<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">1</span>,n+<span style="color: #B452CD">1</span>):
|
||||
q = <span style="color: #658b00">int</span>((i)*(i+<span style="color: #B452CD">1</span>)/<span style="color: #B452CD">2</span>)
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> k <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(i+<span style="color: #B452CD">1</span>):
|
||||
X[:,q+k] = (x**(i-k))*(y**k)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> X
|
||||
|
||||
|
||||
<span style="color: #228B22"># Making meshgrid of datapoints and compute Franke's function</span>
|
||||
n = <span style="color: #B452CD">5</span>
|
||||
N = <span style="color: #B452CD">1000</span>
|
||||
x = np.sort(np.random.uniform(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, N))
|
||||
y = np.sort(np.random.uniform(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, N))
|
||||
z = FrankeFunction(x, y)
|
||||
X = create_X(x, y, n=n)
|
||||
<span style="color: #228B22"># split in training and test data</span>
|
||||
X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=<span style="color: #B452CD">0.2</span>)
|
||||
|
||||
|
||||
svm = SVR(gamma=<span style="color: #CD5555">'auto'</span>,C=<span style="color: #B452CD">10.0</span>)
|
||||
svm.fit(X_train, y_train)
|
||||
|
||||
<span style="color: #228B22"># The mean squared error and R2 score</span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"MSE before scaling: {:.2f}"</span>.format(mean_squared_error(svm.predict(X_test), y_test)))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"R2 score before scaling {:.2f}"</span>.format(svm.score(X_test,y_test)))
|
||||
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature min values before scaling:\n {}"</span>.format(X_train.min(axis=<span style="color: #B452CD">0</span>)))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature max values before scaling:\n {}"</span>.format(X_train.max(axis=<span style="color: #B452CD">0</span>)))
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature min values after scaling:\n {}"</span>.format(X_train_scaled.min(axis=<span style="color: #B452CD">0</span>)))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Feature max values after scaling:\n {}"</span>.format(X_train_scaled.max(axis=<span style="color: #B452CD">0</span>)))
|
||||
|
||||
svm = SVR(gamma=<span style="color: #CD5555">'auto'</span>,C=<span style="color: #B452CD">10.0</span>)
|
||||
svm.fit(X_train_scaled, y_train)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"MSE after scaling: {:.2f}"</span>.format(mean_squared_error(svm.predict(X_test_scaled), y_test)))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"R2 score for scaled data: {:.2f}"</span>.format(svm.score(X_test_scaled,y_test)))
|
||||
</pre></div>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec3">Simple preprocessing examples, breast cancer data and classification </h2>
|
||||
|
||||
<p>
|
||||
We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification
|
||||
|
||||
<p>
|
||||
|
||||
@@ -220,7 +330,7 @@ svm.fit(X_train_scaled, y_train)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec3">Principal Component Analysis </h2>
|
||||
<h2 id="___sec4">Principal Component Analysis </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -256,7 +366,7 @@ X2D = X_centered.dot(W2)
|
||||
<p>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec4">PCA and scikit-learn </h2>
|
||||
<h2 id="___sec5">PCA and scikit-learn </h2>
|
||||
|
||||
<p>
|
||||
Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
|
||||
@@ -287,7 +397,7 @@ More material to come here.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec5">More on the PCA </h2>
|
||||
<h2 id="___sec6">More on the PCA </h2>
|
||||
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
|
||||
@@ -315,7 +425,7 @@ X_reduced = pca.fit_transform(X)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec6">Incremental PCA </h2>
|
||||
<h2 id="___sec7">Incremental PCA </h2>
|
||||
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
|
||||
@@ -325,7 +435,7 @@ instances arrive).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec7">Randomized PCA </h2>
|
||||
<h2 id="___sec8">Randomized PCA </h2>
|
||||
|
||||
<p>
|
||||
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
|
||||
@@ -340,7 +450,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec8">Kernel PCA </h2>
|
||||
<h2 id="___sec9">Kernel PCA </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -369,7 +479,7 @@ X_reduced = rbf_pca.fit_transform(X)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec9">LLE </h2>
|
||||
<h2 id="___sec10">LLE </h2>
|
||||
|
||||
<p>
|
||||
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
|
||||
@@ -381,7 +491,7 @@ these local relationships are best preserved (more details shortly).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec10">Other techniques </h2>
|
||||
<h2 id="___sec11">Other techniques </h2>
|
||||
|
||||
<p>
|
||||
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
|
||||
|
||||
+131
-21
@@ -71,15 +71,23 @@ div { text-align: justify; text-justify: inter-word; }
|
||||
None,
|
||||
'___sec0'),
|
||||
('Preprocessing our data', 2, None, '___sec1'),
|
||||
('Simple preprocessing examples', 2, None, '___sec2'),
|
||||
('Principal Component Analysis', 2, None, '___sec3'),
|
||||
('PCA and scikit-learn', 2, None, '___sec4'),
|
||||
('More on the PCA', 2, None, '___sec5'),
|
||||
('Incremental PCA', 2, None, '___sec6'),
|
||||
('Randomized PCA', 2, None, '___sec7'),
|
||||
('Kernel PCA', 2, None, '___sec8'),
|
||||
('LLE', 2, None, '___sec9'),
|
||||
('Other techniques', 2, None, '___sec10')]}
|
||||
('Simple preprocessing examples, Franke function and regression',
|
||||
2,
|
||||
None,
|
||||
'___sec2'),
|
||||
('Simple preprocessing examples, breast cancer data and '
|
||||
'classification',
|
||||
2,
|
||||
None,
|
||||
'___sec3'),
|
||||
('Principal Component Analysis', 2, None, '___sec4'),
|
||||
('PCA and scikit-learn', 2, None, '___sec5'),
|
||||
('More on the PCA', 2, None, '___sec6'),
|
||||
('Incremental PCA', 2, None, '___sec7'),
|
||||
('Randomized PCA', 2, None, '___sec8'),
|
||||
('Kernel PCA', 2, None, '___sec9'),
|
||||
('LLE', 2, None, '___sec10'),
|
||||
('Other techniques', 2, None, '___sec11')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
@@ -121,7 +129,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>Oct 12, 2019</h4></center> <!-- date -->
|
||||
<center><h4>Oct 14, 2019</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -173,11 +181,113 @@ This scaling has the drawback that it does not ensure that we have a particular
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec2">Simple preprocessing examples </h2>
|
||||
<h2 id="___sec2">Simple preprocessing examples, Franke function and regression </h2>
|
||||
|
||||
<p>
|
||||
We show here how we can use a simple regression case (our nuclear binding energies discussed earlier).
|
||||
Rescaling our data with different
|
||||
|
||||
<!-- 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">os</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">import</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skl</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> mean_squared_error
|
||||
<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.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> MinMaxScaler, StandardScaler, Normalizer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVR
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Where to save the figures and data files</span>
|
||||
PROJECT_ROOT_DIR <span style="color: #666666">=</span> <span style="color: #BA2121">"Results"</span>
|
||||
FIGURE_ID <span style="color: #666666">=</span> <span style="color: #BA2121">"Results/FigureFiles"</span>
|
||||
DATA_ID <span style="color: #666666">=</span> <span style="color: #BA2121">"DataFiles/"</span>
|
||||
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(PROJECT_ROOT_DIR):
|
||||
os<span style="color: #666666">.</span>mkdir(PROJECT_ROOT_DIR)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(FIGURE_ID):
|
||||
os<span style="color: #666666">.</span>makedirs(FIGURE_ID)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(DATA_ID):
|
||||
os<span style="color: #666666">.</span>makedirs(DATA_ID)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">image_path</span>(fig_id):
|
||||
<span style="color: #008000; font-weight: bold">return</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(FIGURE_ID, fig_id)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">data_path</span>(dat_id):
|
||||
<span style="color: #008000; font-weight: bold">return</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(DATA_ID, dat_id)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">save_fig</span>(fig_id):
|
||||
plt<span style="color: #666666">.</span>savefig(image_path(fig_id) <span style="color: #666666">+</span> <span style="color: #BA2121">".png"</span>, format<span style="color: #666666">=</span><span style="color: #BA2121">'png'</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">5</span>
|
||||
N <span style="color: #666666">=</span> <span style="color: #666666">1000</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)
|
||||
<span style="color: #408080; font-style: italic"># split in training and test data</span>
|
||||
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X,z,test_size<span style="color: #666666">=0.2</span>)
|
||||
|
||||
|
||||
svm <span style="color: #666666">=</span> SVR(gamma<span style="color: #666666">=</span><span style="color: #BA2121">'auto'</span>,C<span style="color: #666666">=10.0</span>)
|
||||
svm<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
|
||||
<span style="color: #408080; font-style: italic"># The mean squared error and R2 score</span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"MSE before scaling: {:.2f}"</span><span style="color: #666666">.</span>format(mean_squared_error(svm<span style="color: #666666">.</span>predict(X_test), y_test)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"R2 score before scaling {:.2f}"</span><span style="color: #666666">.</span>format(svm<span style="color: #666666">.</span>score(X_test,y_test)))
|
||||
|
||||
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: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature min values before scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train<span style="color: #666666">.</span>min(axis<span style="color: #666666">=0</span>)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature max values before scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train<span style="color: #666666">.</span>max(axis<span style="color: #666666">=0</span>)))
|
||||
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature min values after scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train_scaled<span style="color: #666666">.</span>min(axis<span style="color: #666666">=0</span>)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Feature max values after scaling:</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121"> {}"</span><span style="color: #666666">.</span>format(X_train_scaled<span style="color: #666666">.</span>max(axis<span style="color: #666666">=0</span>)))
|
||||
|
||||
svm <span style="color: #666666">=</span> SVR(gamma<span style="color: #666666">=</span><span style="color: #BA2121">'auto'</span>,C<span style="color: #666666">=10.0</span>)
|
||||
svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"MSE after scaling: {:.2f}"</span><span style="color: #666666">.</span>format(mean_squared_error(svm<span style="color: #666666">.</span>predict(X_test_scaled), y_test)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"R2 score for scaled data: {:.2f}"</span><span style="color: #666666">.</span>format(svm<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
</pre></div>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec3">Simple preprocessing examples, breast cancer data and classification </h2>
|
||||
|
||||
<p>
|
||||
We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification
|
||||
|
||||
<p>
|
||||
|
||||
@@ -225,7 +335,7 @@ svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec3">Principal Component Analysis </h2>
|
||||
<h2 id="___sec4">Principal Component Analysis </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -261,7 +371,7 @@ X2D <span style="color: #666666">=</span> X_centered<span style="color: #666666"
|
||||
<p>
|
||||
<!-- !split -->
|
||||
|
||||
<h2 id="___sec4">PCA and scikit-learn </h2>
|
||||
<h2 id="___sec5">PCA and scikit-learn </h2>
|
||||
|
||||
<p>
|
||||
Scikit-Learn’s PCA class implements PCA using SVD decomposition just like we did before. The
|
||||
@@ -292,7 +402,7 @@ More material to come here.
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec5">More on the PCA </h2>
|
||||
<h2 id="___sec6">More on the PCA </h2>
|
||||
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
|
||||
@@ -320,7 +430,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="___sec6">Incremental PCA </h2>
|
||||
<h2 id="___sec7">Incremental PCA </h2>
|
||||
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
|
||||
@@ -330,7 +440,7 @@ instances arrive).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec7">Randomized PCA </h2>
|
||||
<h2 id="___sec8">Randomized PCA </h2>
|
||||
|
||||
<p>
|
||||
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
|
||||
@@ -345,7 +455,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec8">Kernel PCA </h2>
|
||||
<h2 id="___sec9">Kernel PCA </h2>
|
||||
<div class="alert alert-block alert-block alert-text-normal">
|
||||
<b></b>
|
||||
<p>
|
||||
@@ -374,7 +484,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="___sec9">LLE </h2>
|
||||
<h2 id="___sec10">LLE </h2>
|
||||
|
||||
<p>
|
||||
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
|
||||
@@ -386,7 +496,7 @@ these local relationships are best preserved (more details shortly).
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
|
||||
<h2 id="___sec10">Other techniques </h2>
|
||||
<h2 id="___sec11">Other techniques </h2>
|
||||
|
||||
<p>
|
||||
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
"<!-- 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: **Oct 12, 2019**\n",
|
||||
"Date: **Oct 14, 2019**\n",
|
||||
"\n",
|
||||
"Copyright 1999-2019, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
|
||||
"\n",
|
||||
@@ -45,11 +45,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Simple preprocessing examples\n",
|
||||
"\n",
|
||||
"We show here how we can use a simple regression case (our nuclear binding energies discussed earlier).\n",
|
||||
"Rescaling our data with different"
|
||||
"## Simple preprocessing examples, Franke function and regression"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -62,6 +58,119 @@
|
||||
"source": [
|
||||
"%matplotlib inline\n",
|
||||
"\n",
|
||||
"# Common imports\n",
|
||||
"import os\n",
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import sklearn.linear_model as skl\n",
|
||||
"from sklearn.metrics import mean_squared_error\n",
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer\n",
|
||||
"from sklearn.svm import SVR\n",
|
||||
"\n",
|
||||
"# Where to save the figures and data files\n",
|
||||
"PROJECT_ROOT_DIR = \"Results\"\n",
|
||||
"FIGURE_ID = \"Results/FigureFiles\"\n",
|
||||
"DATA_ID = \"DataFiles/\"\n",
|
||||
"\n",
|
||||
"if not os.path.exists(PROJECT_ROOT_DIR):\n",
|
||||
" os.mkdir(PROJECT_ROOT_DIR)\n",
|
||||
"\n",
|
||||
"if not os.path.exists(FIGURE_ID):\n",
|
||||
" os.makedirs(FIGURE_ID)\n",
|
||||
"\n",
|
||||
"if not os.path.exists(DATA_ID):\n",
|
||||
" os.makedirs(DATA_ID)\n",
|
||||
"\n",
|
||||
"def image_path(fig_id):\n",
|
||||
" return os.path.join(FIGURE_ID, fig_id)\n",
|
||||
"\n",
|
||||
"def data_path(dat_id):\n",
|
||||
" return os.path.join(DATA_ID, dat_id)\n",
|
||||
"\n",
|
||||
"def save_fig(fig_id):\n",
|
||||
" plt.savefig(image_path(fig_id) + \".png\", format='png')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def FrankeFunction(x,y):\n",
|
||||
"\tterm1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))\n",
|
||||
"\tterm2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))\n",
|
||||
"\tterm3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))\n",
|
||||
"\tterm4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)\n",
|
||||
"\treturn term1 + term2 + term3 + term4\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def create_X(x, y, n ):\n",
|
||||
"\tif len(x.shape) > 1:\n",
|
||||
"\t\tx = np.ravel(x)\n",
|
||||
"\t\ty = np.ravel(y)\n",
|
||||
"\n",
|
||||
"\tN = len(x)\n",
|
||||
"\tl = int((n+1)*(n+2)/2)\t\t# Number of elements in beta\n",
|
||||
"\tX = np.ones((N,l))\n",
|
||||
"\n",
|
||||
"\tfor i in range(1,n+1):\n",
|
||||
"\t\tq = int((i)*(i+1)/2)\n",
|
||||
"\t\tfor k in range(i+1):\n",
|
||||
"\t\t\tX[:,q+k] = (x**(i-k))*(y**k)\n",
|
||||
"\n",
|
||||
"\treturn X\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Making meshgrid of datapoints and compute Franke's function\n",
|
||||
"n = 5\n",
|
||||
"N = 1000\n",
|
||||
"x = np.sort(np.random.uniform(0, 1, N))\n",
|
||||
"y = np.sort(np.random.uniform(0, 1, N))\n",
|
||||
"z = FrankeFunction(x, y)\n",
|
||||
"X = create_X(x, y, n=n) \n",
|
||||
"# split in training and test data\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=0.2)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"svm = SVR(gamma='auto',C=10.0)\n",
|
||||
"svm.fit(X_train, y_train)\n",
|
||||
"\n",
|
||||
"# The mean squared error and R2 score\n",
|
||||
"print(\"MSE before scaling: {:.2f}\".format(mean_squared_error(svm.predict(X_test), y_test)))\n",
|
||||
"print(\"R2 score before scaling {:.2f}\".format(svm.score(X_test,y_test)))\n",
|
||||
"\n",
|
||||
"scaler = StandardScaler()\n",
|
||||
"scaler.fit(X_train)\n",
|
||||
"X_train_scaled = scaler.transform(X_train)\n",
|
||||
"X_test_scaled = scaler.transform(X_test)\n",
|
||||
"\n",
|
||||
"print(\"Feature min values before scaling:\\n {}\".format(X_train.min(axis=0)))\n",
|
||||
"print(\"Feature max values before scaling:\\n {}\".format(X_train.max(axis=0)))\n",
|
||||
"\n",
|
||||
"print(\"Feature min values after scaling:\\n {}\".format(X_train_scaled.min(axis=0)))\n",
|
||||
"print(\"Feature max values after scaling:\\n {}\".format(X_train_scaled.max(axis=0)))\n",
|
||||
"\n",
|
||||
"svm = SVR(gamma='auto',C=10.0)\n",
|
||||
"svm.fit(X_train_scaled, y_train)\n",
|
||||
"\n",
|
||||
"print(\"MSE after scaling: {:.2f}\".format(mean_squared_error(svm.predict(X_test_scaled), y_test)))\n",
|
||||
"print(\"R2 score for scaled data: {:.2f}\".format(svm.score(X_test_scaled,y_test)))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Simple preprocessing examples, breast cancer data and classification\n",
|
||||
"\n",
|
||||
"We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"from sklearn.model_selection import train_test_split \n",
|
||||
@@ -117,7 +226,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -144,7 +253,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -168,7 +277,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -190,7 +299,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -219,7 +328,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 7,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -242,7 +351,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 8,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -288,7 +397,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 9,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -35,12 +35,114 @@ This scaling has the drawback that it does not ensure that we have a particular
|
||||
|
||||
!eblock
|
||||
|
||||
!split
|
||||
===== Simple preprocessing examples, Franke function and regression =====
|
||||
|
||||
!bc pycod
|
||||
# Common imports
|
||||
import os
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
import sklearn.linear_model as skl
|
||||
from sklearn.metrics import mean_squared_error
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer
|
||||
from sklearn.svm import SVR
|
||||
|
||||
# Where to save the figures and data files
|
||||
PROJECT_ROOT_DIR = "Results"
|
||||
FIGURE_ID = "Results/FigureFiles"
|
||||
DATA_ID = "DataFiles/"
|
||||
|
||||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
|
||||
if not os.path.exists(FIGURE_ID):
|
||||
os.makedirs(FIGURE_ID)
|
||||
|
||||
if not os.path.exists(DATA_ID):
|
||||
os.makedirs(DATA_ID)
|
||||
|
||||
def image_path(fig_id):
|
||||
return os.path.join(FIGURE_ID, fig_id)
|
||||
|
||||
def data_path(dat_id):
|
||||
return os.path.join(DATA_ID, dat_id)
|
||||
|
||||
def save_fig(fig_id):
|
||||
plt.savefig(image_path(fig_id) + ".png", format='png')
|
||||
|
||||
|
||||
def FrankeFunction(x,y):
|
||||
term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
|
||||
term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
|
||||
term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
|
||||
term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
|
||||
return term1 + term2 + term3 + term4
|
||||
|
||||
|
||||
def create_X(x, y, n ):
|
||||
if len(x.shape) > 1:
|
||||
x = np.ravel(x)
|
||||
y = np.ravel(y)
|
||||
|
||||
N = len(x)
|
||||
l = int((n+1)*(n+2)/2) # Number of elements in beta
|
||||
X = np.ones((N,l))
|
||||
|
||||
for i in range(1,n+1):
|
||||
q = int((i)*(i+1)/2)
|
||||
for k in range(i+1):
|
||||
X[:,q+k] = (x**(i-k))*(y**k)
|
||||
|
||||
return X
|
||||
|
||||
|
||||
# Making meshgrid of datapoints and compute Franke's function
|
||||
n = 5
|
||||
N = 1000
|
||||
x = np.sort(np.random.uniform(0, 1, N))
|
||||
y = np.sort(np.random.uniform(0, 1, N))
|
||||
z = FrankeFunction(x, y)
|
||||
X = create_X(x, y, n=n)
|
||||
# split in training and test data
|
||||
X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=0.2)
|
||||
|
||||
|
||||
svm = SVR(gamma='auto',C=10.0)
|
||||
svm.fit(X_train, y_train)
|
||||
|
||||
# The mean squared error and R2 score
|
||||
print("MSE before scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test), y_test)))
|
||||
print("R2 score before scaling {:.2f}".format(svm.score(X_test,y_test)))
|
||||
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
print("Feature min values before scaling:\n {}".format(X_train.min(axis=0)))
|
||||
print("Feature max values before scaling:\n {}".format(X_train.max(axis=0)))
|
||||
|
||||
print("Feature min values after scaling:\n {}".format(X_train_scaled.min(axis=0)))
|
||||
print("Feature max values after scaling:\n {}".format(X_train_scaled.max(axis=0)))
|
||||
|
||||
svm = SVR(gamma='auto',C=10.0)
|
||||
svm.fit(X_train_scaled, y_train)
|
||||
|
||||
print("MSE after scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test_scaled), y_test)))
|
||||
print("R2 score for scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)))
|
||||
|
||||
!ec
|
||||
|
||||
|
||||
|
||||
!split
|
||||
===== Simple preprocessing examples =====
|
||||
===== Simple preprocessing examples, breast cancer data and classification =====
|
||||
|
||||
We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification
|
||||
|
||||
We show here how we can use a simple regression case (our nuclear binding energies discussed earlier).
|
||||
Rescaling our data with different
|
||||
|
||||
!bc pycod
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
@@ -7,6 +7,7 @@ import matplotlib.pyplot as plt
|
||||
import sklearn.linear_model as skl
|
||||
from sklearn.metrics import mean_squared_error
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer
|
||||
from sklearn.svm import SVR
|
||||
|
||||
# Where to save the figures and data files
|
||||
@@ -41,7 +42,7 @@ def FrankeFunction(x,y):
|
||||
return term1 + term2 + term3 + term4
|
||||
|
||||
|
||||
def create_X(x, y, n = 5):
|
||||
def create_X(x, y, n ):
|
||||
if len(x.shape) > 1:
|
||||
x = np.ravel(x)
|
||||
y = np.ravel(y)
|
||||
@@ -71,14 +72,11 @@ X_train, X_test, y_train, y_test = train_test_split(X,z,test_size=0.2)
|
||||
svm = SVR(gamma='auto',C=10.0)
|
||||
svm.fit(X_train, y_train)
|
||||
|
||||
# The mean squared error
|
||||
print("Test set accuracy: {:.2f}".format(svm.score(X_test,y_test)))
|
||||
# The mean squared error and R2 score
|
||||
print("MSE before scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test), y_test)))
|
||||
print("R2 score before scaling {:.2f}".format(svm.score(X_test,y_test)))
|
||||
|
||||
|
||||
|
||||
|
||||
from sklearn.preprocessing import MinMaxScaler, StandardScaler
|
||||
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
@@ -95,7 +93,7 @@ print("Feature max values after scaling:\n {}".format(X_train_scaled.max(axis=0)
|
||||
svm = SVR(gamma='auto',C=10.0)
|
||||
svm.fit(X_train_scaled, y_train)
|
||||
|
||||
|
||||
print("MSE after scaling: {:.2f}".format(mean_squared_error(svm.predict(X_test_scaled), y_test)))
|
||||
print("Test set accuracy scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user