updating codes

This commit is contained in:
mhjensen
2019-10-15 07:59:28 +02:00
parent e2f09224e6
commit c266649550
20 changed files with 813 additions and 337 deletions
+24 -19
View File
@@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source
None,
'___sec2'),
('Simple preprocessing examples, breast cancer data and '
'classification',
'classification, Support Vector Machines',
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')]}
('More on Cancer Data, now with Logistic Regression',
2,
None,
'___sec4'),
('Principal Component Analysis', 2, None, '___sec5'),
('PCA and scikit-learn', 2, None, '___sec6'),
('More on the PCA', 2, None, '___sec7'),
('Incremental PCA', 2, None, '___sec8'),
('Randomized PCA', 2, None, '___sec9'),
('Kernel PCA', 2, None, '___sec10'),
('LLE', 2, None, '___sec11'),
('Other techniques', 2, None, '___sec12')]}
end of tocinfo -->
<body>
@@ -103,15 +107,16 @@ MathJax.Hub.Config({
<!-- 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, 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>
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">More on Cancer Data, now with Logistic Regression</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">Principal Component Analysis</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">PCA and scikit-learn</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">More on the PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Incremental PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Randomized PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Kernel PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">LLE</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs013.html#___sec12" style="font-size: 80%;">Other techniques</a></li>
</ul>
</li>
@@ -170,7 +175,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-bs012.html">13</a></li>
<li><a href="._DimRed-bs013.html">14</a></li>
<li><a href="._DimRed-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+24 -19
View File
@@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source
None,
'___sec2'),
('Simple preprocessing examples, breast cancer data and '
'classification',
'classification, Support Vector Machines',
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')]}
('More on Cancer Data, now with Logistic Regression',
2,
None,
'___sec4'),
('Principal Component Analysis', 2, None, '___sec5'),
('PCA and scikit-learn', 2, None, '___sec6'),
('More on the PCA', 2, None, '___sec7'),
('Incremental PCA', 2, None, '___sec8'),
('Randomized PCA', 2, None, '___sec9'),
('Kernel PCA', 2, None, '___sec10'),
('LLE', 2, None, '___sec11'),
('Other techniques', 2, None, '___sec12')]}
end of tocinfo -->
<body>
@@ -103,15 +107,16 @@ MathJax.Hub.Config({
<!-- 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, 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>
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">More on Cancer Data, now with Logistic Regression</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">Principal Component Analysis</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">PCA and scikit-learn</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">More on the PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Incremental PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Randomized PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Kernel PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">LLE</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs013.html#___sec12" style="font-size: 80%;">Other techniques</a></li>
</ul>
</li>
@@ -165,7 +170,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-bs012.html">13</a></li>
<li><a href="._DimRed-bs013.html">14</a></li>
<li><a href="._DimRed-bs002.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+24 -19
View File
@@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source
None,
'___sec2'),
('Simple preprocessing examples, breast cancer data and '
'classification',
'classification, Support Vector Machines',
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')]}
('More on Cancer Data, now with Logistic Regression',
2,
None,
'___sec4'),
('Principal Component Analysis', 2, None, '___sec5'),
('PCA and scikit-learn', 2, None, '___sec6'),
('More on the PCA', 2, None, '___sec7'),
('Incremental PCA', 2, None, '___sec8'),
('Randomized PCA', 2, None, '___sec9'),
('Kernel PCA', 2, None, '___sec10'),
('LLE', 2, None, '___sec11'),
('Other techniques', 2, None, '___sec12')]}
end of tocinfo -->
<body>
@@ -103,15 +107,16 @@ MathJax.Hub.Config({
<!-- 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, 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>
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">More on Cancer Data, now with Logistic Regression</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">Principal Component Analysis</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">PCA and scikit-learn</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">More on the PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Incremental PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Randomized PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Kernel PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">LLE</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs013.html#___sec12" style="font-size: 80%;">Other techniques</a></li>
</ul>
</li>
@@ -167,7 +172,7 @@ This scaling has the drawback that it does not ensure that we have a particular
<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-bs013.html">14</a></li>
<li><a href="._DimRed-bs003.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+25 -18
View File
@@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source
None,
'___sec2'),
('Simple preprocessing examples, breast cancer data and '
'classification',
'classification, Support Vector Machines',
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')]}
('More on Cancer Data, now with Logistic Regression',
2,
None,
'___sec4'),
('Principal Component Analysis', 2, None, '___sec5'),
('PCA and scikit-learn', 2, None, '___sec6'),
('More on the PCA', 2, None, '___sec7'),
('Incremental PCA', 2, None, '___sec8'),
('Randomized PCA', 2, None, '___sec9'),
('Kernel PCA', 2, None, '___sec10'),
('LLE', 2, None, '___sec11'),
('Other techniques', 2, None, '___sec12')]}
end of tocinfo -->
<body>
@@ -103,15 +107,16 @@ MathJax.Hub.Config({
<!-- 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, 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>
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">More on Cancer Data, now with Logistic Regression</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">Principal Component Analysis</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">PCA and scikit-learn</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">More on the PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Incremental PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Randomized PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Kernel PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">LLE</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs013.html#___sec12" style="font-size: 80%;">Other techniques</a></li>
</ul>
</li>
@@ -245,6 +250,8 @@ svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<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="">...</a></li>
<li><a href="._DimRed-bs013.html">14</a></li>
<li><a href="._DimRed-bs004.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+25 -19
View File
@@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source
None,
'___sec2'),
('Simple preprocessing examples, breast cancer data and '
'classification',
'classification, Support Vector Machines',
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')]}
('More on Cancer Data, now with Logistic Regression',
2,
None,
'___sec4'),
('Principal Component Analysis', 2, None, '___sec5'),
('PCA and scikit-learn', 2, None, '___sec6'),
('More on the PCA', 2, None, '___sec7'),
('Incremental PCA', 2, None, '___sec8'),
('Randomized PCA', 2, None, '___sec9'),
('Kernel PCA', 2, None, '___sec10'),
('LLE', 2, None, '___sec11'),
('Other techniques', 2, None, '___sec12')]}
end of tocinfo -->
<body>
@@ -103,15 +107,16 @@ MathJax.Hub.Config({
<!-- 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, 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>
<!-- navigation toc: --> <li><a href="#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">More on Cancer Data, now with Logistic Regression</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">Principal Component Analysis</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">PCA and scikit-learn</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">More on the PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Incremental PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Randomized PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Kernel PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">LLE</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs013.html#___sec12" style="font-size: 80%;">Other techniques</a></li>
</ul>
</li>
@@ -127,7 +132,7 @@ MathJax.Hub.Config({
<a name="part0004"></a>
<!-- !split -->
<h2 id="___sec3" class="anchor">Simple preprocessing examples, breast cancer data and classification </h2>
<h2 id="___sec3" class="anchor">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines </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
@@ -193,6 +198,7 @@ svm<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<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-bs013.html">14</a></li>
<li><a href="._DimRed-bs005.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+70 -45
View File
@@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source
None,
'___sec2'),
('Simple preprocessing examples, breast cancer data and '
'classification',
'classification, Support Vector Machines',
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')]}
('More on Cancer Data, now with Logistic Regression',
2,
None,
'___sec4'),
('Principal Component Analysis', 2, None, '___sec5'),
('PCA and scikit-learn', 2, None, '___sec6'),
('More on the PCA', 2, None, '___sec7'),
('Incremental PCA', 2, None, '___sec8'),
('Randomized PCA', 2, None, '___sec9'),
('Kernel PCA', 2, None, '___sec10'),
('LLE', 2, None, '___sec11'),
('Other techniques', 2, None, '___sec12')]}
end of tocinfo -->
<body>
@@ -103,15 +107,16 @@ MathJax.Hub.Config({
<!-- 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, 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>
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines</a></li>
<!-- navigation toc: --> <li><a href="#___sec4" style="font-size: 80%;">More on Cancer Data, now with Logistic Regression</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">Principal Component Analysis</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">PCA and scikit-learn</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">More on the PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Incremental PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Randomized PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Kernel PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">LLE</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs013.html#___sec12" style="font-size: 80%;">Other techniques</a></li>
</ul>
</li>
@@ -127,38 +132,57 @@ MathJax.Hub.Config({
<a name="part0005"></a>
<!-- !split -->
<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>
The following Python code uses NumPy&#8217;s <b>svd()</b> function to obtain all the principal components of the
training set, then extracts the first two principal components
<h2 id="___sec4" class="anchor">More on Cancer Data, now with Logistic 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>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&#8217;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&#8217;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.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
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>
fig, axes <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(<span style="color: #666666">15</span>,<span style="color: #666666">2</span>,figsize<span style="color: #666666">=</span>(<span style="color: #666666">10</span>,<span style="color: #666666">20</span>))
male <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">0</span>]
bene <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">1</span>]
ax <span style="color: #666666">=</span> axes<span style="color: #666666">.</span>ravel()
<!-- 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)
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">30</span>):
_, bins <span style="color: #666666">=</span> np<span style="color: #666666">.</span>histogram(cancer<span style="color: #666666">.</span>data[:,i], bins <span style="color: #666666">=50</span>)
ax[i]<span style="color: #666666">.</span>hist(male[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
ax[i]<span style="color: #666666">.</span>hist(bene[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
ax[i]<span style="color: #666666">.</span>set_title(cancer<span style="color: #666666">.</span>feature_names[i])
ax[i]<span style="color: #666666">.</span>set_yticks(())
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">&quot;Feature magnitude&quot;</span>)
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">&quot;Frequency&quot;</span>)
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>legend([<span style="color: #BA2121">&quot;Male&quot;</span>, <span style="color: #BA2121">&quot;Bene&quot;</span>], loc <span style="color: #666666">=</span><span style="color: #BA2121">&quot;best&quot;</span>)
fig<span style="color: #666666">.</span>tight_layout()
plt<span style="color: #666666">.</span>show()
<span style="color: #408080; font-style: italic"># Set up training data</span>
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: #408080; font-style: italic"># Perform Logistic Regression (replace with own code) </span>
logreg <span style="color: #666666">=</span> LogisticRegression()
logreg<span style="color: #666666">.</span>fit(X_train, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy: {:.2f}&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test,y_test)))
<span style="color: #408080; font-style: italic"># Scale data</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
scaler <span style="color: #666666">=</span> StandardScaler()
scaler<span style="color: #666666">.</span>fit(X_train)
X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_train)
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<span style="color: #408080; font-style: italic">#svm.fit(X_train_scaled, y_train)</span>
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy scaled data: {:.2f}&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
<span style="color: #408080; font-style: italic"># Now add PCA</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>)
pca<span style="color: #666666">.</span>fit(X_train_scaled)
X_pca <span style="color: #666666">=</span> pca<span style="color: #666666">.</span>transform(X_train_scaled)
</pre></div>
<p>
<p>
@@ -178,6 +202,7 @@ X2D <span style="color: #666666">=</span> X_centered<span style="color: #666666"
<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-bs013.html">14</a></li>
<li><a href="._DimRed-bs006.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+47 -36
View File
@@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source
None,
'___sec2'),
('Simple preprocessing examples, breast cancer data and '
'classification',
'classification, Support Vector Machines',
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')]}
('More on Cancer Data, now with Logistic Regression',
2,
None,
'___sec4'),
('Principal Component Analysis', 2, None, '___sec5'),
('PCA and scikit-learn', 2, None, '___sec6'),
('More on the PCA', 2, None, '___sec7'),
('Incremental PCA', 2, None, '___sec8'),
('Randomized PCA', 2, None, '___sec9'),
('Kernel PCA', 2, None, '___sec10'),
('LLE', 2, None, '___sec11'),
('Other techniques', 2, None, '___sec12')]}
end of tocinfo -->
<body>
@@ -103,15 +107,16 @@ MathJax.Hub.Config({
<!-- 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, 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>
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">More on Cancer Data, now with Logistic Regression</a></li>
<!-- navigation toc: --> <li><a href="#___sec5" style="font-size: 80%;">Principal Component Analysis</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">PCA and scikit-learn</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">More on the PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Incremental PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Randomized PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Kernel PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">LLE</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs013.html#___sec12" style="font-size: 80%;">Other techniques</a></li>
</ul>
</li>
@@ -125,36 +130,41 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0006"></a>
<!-- !split -->
<!-- !split -->
<h2 id="___sec5" class="anchor">PCA and scikit-learn </h2>
<h2 id="___sec5" 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&#8217;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&#8217;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&#8217;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&#8217;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&#8217;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 -->
@@ -173,6 +183,7 @@ More material to come here.
<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-bs013.html">14</a></li>
<li><a href="._DimRed-bs007.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+44 -35
View File
@@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source
None,
'___sec2'),
('Simple preprocessing examples, breast cancer data and '
'classification',
'classification, Support Vector Machines',
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')]}
('More on Cancer Data, now with Logistic Regression',
2,
None,
'___sec4'),
('Principal Component Analysis', 2, None, '___sec5'),
('PCA and scikit-learn', 2, None, '___sec6'),
('More on the PCA', 2, None, '___sec7'),
('Incremental PCA', 2, None, '___sec8'),
('Randomized PCA', 2, None, '___sec9'),
('Kernel PCA', 2, None, '___sec10'),
('LLE', 2, None, '___sec11'),
('Other techniques', 2, None, '___sec12')]}
end of tocinfo -->
<body>
@@ -103,15 +107,16 @@ MathJax.Hub.Config({
<!-- 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, 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>
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">More on Cancer Data, now with Logistic Regression</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">Principal Component Analysis</a></li>
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;">PCA and scikit-learn</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">More on the PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Incremental PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Randomized PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Kernel PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">LLE</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs013.html#___sec12" style="font-size: 80%;">Other techniques</a></li>
</ul>
</li>
@@ -125,33 +130,36 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0007"></a>
<!-- !split -->
<!-- !split -->
<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 &#8212; 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&#8217;s variance:
<h2 id="___sec6" class="anchor">PCA and scikit-learn </h2>
<p>
Scikit-Learn&#8217;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">&gt;=</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&#8217;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 -->
@@ -170,6 +178,7 @@ X_reduced <span style="color: #666666">=</span> pca<span style="color: #666666">
<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-bs013.html">14</a></li>
<li><a href="._DimRed-bs008.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+48 -24
View File
@@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source
None,
'___sec2'),
('Simple preprocessing examples, breast cancer data and '
'classification',
'classification, Support Vector Machines',
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')]}
('More on Cancer Data, now with Logistic Regression',
2,
None,
'___sec4'),
('Principal Component Analysis', 2, None, '___sec5'),
('PCA and scikit-learn', 2, None, '___sec6'),
('More on the PCA', 2, None, '___sec7'),
('Incremental PCA', 2, None, '___sec8'),
('Randomized PCA', 2, None, '___sec9'),
('Kernel PCA', 2, None, '___sec10'),
('LLE', 2, None, '___sec11'),
('Other techniques', 2, None, '___sec12')]}
end of tocinfo -->
<body>
@@ -103,15 +107,16 @@ MathJax.Hub.Config({
<!-- 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, 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>
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">More on Cancer Data, now with Logistic Regression</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">Principal Component Analysis</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">PCA and scikit-learn</a></li>
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;">More on the PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Incremental PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Randomized PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Kernel PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">LLE</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs013.html#___sec12" style="font-size: 80%;">Other techniques</a></li>
</ul>
</li>
@@ -127,13 +132,31 @@ MathJax.Hub.Config({
<a name="part0008"></a>
<!-- !split -->
<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).
<h2 id="___sec7" 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 &#8212; 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&#8217;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">&gt;=</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 -->
@@ -152,6 +175,7 @@ instances arrive).
<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-bs013.html">14</a></li>
<li><a href="._DimRed-bs009.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+30 -30
View File
@@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source
None,
'___sec2'),
('Simple preprocessing examples, breast cancer data and '
'classification',
'classification, Support Vector Machines',
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')]}
('More on Cancer Data, now with Logistic Regression',
2,
None,
'___sec4'),
('Principal Component Analysis', 2, None, '___sec5'),
('PCA and scikit-learn', 2, None, '___sec6'),
('More on the PCA', 2, None, '___sec7'),
('Incremental PCA', 2, None, '___sec8'),
('Randomized PCA', 2, None, '___sec9'),
('Kernel PCA', 2, None, '___sec10'),
('LLE', 2, None, '___sec11'),
('Other techniques', 2, None, '___sec12')]}
end of tocinfo -->
<body>
@@ -103,15 +107,16 @@ MathJax.Hub.Config({
<!-- 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, 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>
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">More on Cancer Data, now with Logistic Regression</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">Principal Component Analysis</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">PCA and scikit-learn</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">More on the PCA</a></li>
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;">Incremental PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Randomized PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Kernel PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">LLE</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs013.html#___sec12" style="font-size: 80%;">Other techniques</a></li>
</ul>
</li>
@@ -127,18 +132,12 @@ MathJax.Hub.Config({
<a name="part0009"></a>
<!-- !split -->
<h2 id="___sec8" 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="___sec8" 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>
@@ -158,6 +157,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
<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-bs013.html">14</a></li>
<li><a href="._DimRed-bs010.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+24 -19
View File
@@ -51,18 +51,22 @@ Automatically generated HTML file from DocOnce source
None,
'___sec2'),
('Simple preprocessing examples, breast cancer data and '
'classification',
'classification, Support Vector Machines',
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')]}
('More on Cancer Data, now with Logistic Regression',
2,
None,
'___sec4'),
('Principal Component Analysis', 2, None, '___sec5'),
('PCA and scikit-learn', 2, None, '___sec6'),
('More on the PCA', 2, None, '___sec7'),
('Incremental PCA', 2, None, '___sec8'),
('Randomized PCA', 2, None, '___sec9'),
('Kernel PCA', 2, None, '___sec10'),
('LLE', 2, None, '___sec11'),
('Other techniques', 2, None, '___sec12')]}
end of tocinfo -->
<body>
@@ -103,15 +107,16 @@ MathJax.Hub.Config({
<!-- 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, 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>
<!-- navigation toc: --> <li><a href="._DimRed-bs004.html#___sec3" style="font-size: 80%;">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs005.html#___sec4" style="font-size: 80%;">More on Cancer Data, now with Logistic Regression</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs006.html#___sec5" style="font-size: 80%;">Principal Component Analysis</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs007.html#___sec6" style="font-size: 80%;">PCA and scikit-learn</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs008.html#___sec7" style="font-size: 80%;">More on the PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs009.html#___sec8" style="font-size: 80%;">Incremental PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs010.html#___sec9" style="font-size: 80%;">Randomized PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs011.html#___sec10" style="font-size: 80%;">Kernel PCA</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs012.html#___sec11" style="font-size: 80%;">LLE</a></li>
<!-- navigation toc: --> <li><a href="._DimRed-bs013.html#___sec12" style="font-size: 80%;">Other techniques</a></li>
</ul>
</li>
@@ -170,7 +175,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-bs012.html">13</a></li>
<li><a href="._DimRed-bs013.html">14</a></li>
<li><a href="._DimRed-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+65 -9
View File
@@ -304,7 +304,7 @@ svm.fit(X_train_scaled, y_train)
<section>
<h2 id="___sec3">Simple preprocessing examples, breast cancer data and classification </h2>
<h2 id="___sec3">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines </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
@@ -356,7 +356,63 @@ svm.fit(X_train_scaled, y_train)
<section>
<h2 id="___sec4">Principal Component Analysis </h2>
<h2 id="___sec4">More on Cancer Data, now with Logistic Regression </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">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">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">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.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_breast_cancer
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LogisticRegression
cancer = load_breast_cancer()
fig, axes = plt.subplots(<span style="color: #B452CD">15</span>,<span style="color: #B452CD">2</span>,figsize=(<span style="color: #B452CD">10</span>,<span style="color: #B452CD">20</span>))
male = cancer.data[cancer.target == <span style="color: #B452CD">0</span>]
bene = cancer.data[cancer.target == <span style="color: #B452CD">1</span>]
ax = axes.ravel()
<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">30</span>):
_, bins = np.histogram(cancer.data[:,i], bins =<span style="color: #B452CD">50</span>)
ax[i].hist(male[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
ax[i].hist(bene[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
ax[i].set_title(cancer.feature_names[i])
ax[i].set_yticks(())
ax[<span style="color: #B452CD">0</span>].set_xlabel(<span style="color: #CD5555">&quot;Feature magnitude&quot;</span>)
ax[<span style="color: #B452CD">0</span>].set_ylabel(<span style="color: #CD5555">&quot;Frequency&quot;</span>)
ax[<span style="color: #B452CD">0</span>].legend([<span style="color: #CD5555">&quot;Male&quot;</span>, <span style="color: #CD5555">&quot;Bene&quot;</span>], loc =<span style="color: #CD5555">&quot;best&quot;</span>)
fig.tight_layout()
plt.show()
<span style="color: #228B22"># Set up training data</span>
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=<span style="color: #B452CD">0</span>)
<span style="color: #228B22"># Perform Logistic Regression (replace with own code) </span>
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy: {:.2f}&quot;</span>.format(logreg.score(X_test,y_test)))
<span style="color: #228B22"># Scale data</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
logreg.fit(X_train_scaled, y_train)
<span style="color: #228B22">#svm.fit(X_train_scaled, y_train)</span>
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy scaled data: {:.2f}&quot;</span>.format(logreg.score(X_test_scaled,y_test)))
<span style="color: #228B22"># Now add PCA</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.decomposition</span> <span style="color: #8B008B; font-weight: bold">import</span> PCA
pca = PCA(n_components = <span style="color: #B452CD">2</span>)
pca.fit(X_train_scaled)
X_pca = pca.transform(X_train_scaled)
</pre></div>
</section>
<section>
<h2 id="___sec5">Principal Component Analysis </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -393,7 +449,7 @@ X2D = X_centered.dot(W2)
<section>
<h2 id="___sec5">PCA and scikit-learn </h2>
<h2 id="___sec6">PCA and scikit-learn </h2>
<p>
Scikit-Learn&#8217;s PCA class implements PCA using SVD decomposition just like we did before. The
@@ -424,7 +480,7 @@ More material to come here.
<section>
<h2 id="___sec6">More on the PCA </h2>
<h2 id="___sec7">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 &#8212; in that case you will
@@ -453,7 +509,7 @@ X_reduced = pca.fit_transform(X)
<section>
<h2 id="___sec7">Incremental PCA </h2>
<h2 id="___sec8">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
@@ -463,7 +519,7 @@ instances arrive).
<section>
<h2 id="___sec8">Randomized PCA </h2>
<h2 id="___sec9">Randomized PCA </h2>
<p>
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
@@ -477,7 +533,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
<section>
<h2 id="___sec9">Kernel PCA </h2>
<h2 id="___sec10">Kernel PCA </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -503,7 +559,7 @@ X_reduced = rbf_pca.fit_transform(X)
<section>
<h2 id="___sec10">LLE </h2>
<h2 id="___sec11">LLE </h2>
<p>
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
@@ -515,7 +571,7 @@ these local relationships are best preserved (more details shortly).
<section>
<h2 id="___sec11">Other techniques </h2>
<h2 id="___sec12">Other techniques </h2>
<p>
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
+77 -18
View File
@@ -71,18 +71,22 @@ div { text-align: justify; text-justify: inter-word; }
None,
'___sec2'),
('Simple preprocessing examples, breast cancer data and '
'classification',
'classification, Support Vector Machines',
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')]}
('More on Cancer Data, now with Logistic Regression',
2,
None,
'___sec4'),
('Principal Component Analysis', 2, None, '___sec5'),
('PCA and scikit-learn', 2, None, '___sec6'),
('More on the PCA', 2, None, '___sec7'),
('Incremental PCA', 2, None, '___sec8'),
('Randomized PCA', 2, None, '___sec9'),
('Kernel PCA', 2, None, '___sec10'),
('LLE', 2, None, '___sec11'),
('Other techniques', 2, None, '___sec12')]}
end of tocinfo -->
<body>
@@ -279,7 +283,7 @@ svm.fit(X_train_scaled, y_train)
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec3">Simple preprocessing examples, breast cancer data and classification </h2>
<h2 id="___sec3">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines </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
@@ -330,7 +334,62 @@ svm.fit(X_train_scaled, y_train)
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec4">Principal Component Analysis </h2>
<h2 id="___sec4">More on Cancer Data, now with Logistic Regression </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">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">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">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.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_breast_cancer
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LogisticRegression
cancer = load_breast_cancer()
fig, axes = plt.subplots(<span style="color: #B452CD">15</span>,<span style="color: #B452CD">2</span>,figsize=(<span style="color: #B452CD">10</span>,<span style="color: #B452CD">20</span>))
male = cancer.data[cancer.target == <span style="color: #B452CD">0</span>]
bene = cancer.data[cancer.target == <span style="color: #B452CD">1</span>]
ax = axes.ravel()
<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">30</span>):
_, bins = np.histogram(cancer.data[:,i], bins =<span style="color: #B452CD">50</span>)
ax[i].hist(male[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
ax[i].hist(bene[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
ax[i].set_title(cancer.feature_names[i])
ax[i].set_yticks(())
ax[<span style="color: #B452CD">0</span>].set_xlabel(<span style="color: #CD5555">&quot;Feature magnitude&quot;</span>)
ax[<span style="color: #B452CD">0</span>].set_ylabel(<span style="color: #CD5555">&quot;Frequency&quot;</span>)
ax[<span style="color: #B452CD">0</span>].legend([<span style="color: #CD5555">&quot;Male&quot;</span>, <span style="color: #CD5555">&quot;Bene&quot;</span>], loc =<span style="color: #CD5555">&quot;best&quot;</span>)
fig.tight_layout()
plt.show()
<span style="color: #228B22"># Set up training data</span>
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=<span style="color: #B452CD">0</span>)
<span style="color: #228B22"># Perform Logistic Regression (replace with own code) </span>
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy: {:.2f}&quot;</span>.format(logreg.score(X_test,y_test)))
<span style="color: #228B22"># Scale data</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
logreg.fit(X_train_scaled, y_train)
<span style="color: #228B22">#svm.fit(X_train_scaled, y_train)</span>
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Test set accuracy scaled data: {:.2f}&quot;</span>.format(logreg.score(X_test_scaled,y_test)))
<span style="color: #228B22"># Now add PCA</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.decomposition</span> <span style="color: #8B008B; font-weight: bold">import</span> PCA
pca = PCA(n_components = <span style="color: #B452CD">2</span>)
pca.fit(X_train_scaled)
X_pca = pca.transform(X_train_scaled)
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec5">Principal Component Analysis </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -366,7 +425,7 @@ X2D = X_centered.dot(W2)
<p>
<!-- !split -->
<h2 id="___sec5">PCA and scikit-learn </h2>
<h2 id="___sec6">PCA and scikit-learn </h2>
<p>
Scikit-Learn&#8217;s PCA class implements PCA using SVD decomposition just like we did before. The
@@ -397,7 +456,7 @@ More material to come here.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec6">More on the PCA </h2>
<h2 id="___sec7">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 &#8212; in that case you will
@@ -425,7 +484,7 @@ X_reduced = pca.fit_transform(X)
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec7">Incremental PCA </h2>
<h2 id="___sec8">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
@@ -435,7 +494,7 @@ instances arrive).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec8">Randomized PCA </h2>
<h2 id="___sec9">Randomized PCA </h2>
<p>
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
@@ -450,7 +509,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec9">Kernel PCA </h2>
<h2 id="___sec10">Kernel PCA </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -479,7 +538,7 @@ X_reduced = rbf_pca.fit_transform(X)
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec10">LLE </h2>
<h2 id="___sec11">LLE </h2>
<p>
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
@@ -491,7 +550,7 @@ these local relationships are best preserved (more details shortly).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec11">Other techniques </h2>
<h2 id="___sec12">Other techniques </h2>
<p>
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
+77 -18
View File
@@ -76,18 +76,22 @@ div { text-align: justify; text-justify: inter-word; }
None,
'___sec2'),
('Simple preprocessing examples, breast cancer data and '
'classification',
'classification, Support Vector Machines',
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')]}
('More on Cancer Data, now with Logistic Regression',
2,
None,
'___sec4'),
('Principal Component Analysis', 2, None, '___sec5'),
('PCA and scikit-learn', 2, None, '___sec6'),
('More on the PCA', 2, None, '___sec7'),
('Incremental PCA', 2, None, '___sec8'),
('Randomized PCA', 2, None, '___sec9'),
('Kernel PCA', 2, None, '___sec10'),
('LLE', 2, None, '___sec11'),
('Other techniques', 2, None, '___sec12')]}
end of tocinfo -->
<body>
@@ -284,7 +288,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">Simple preprocessing examples, breast cancer data and classification </h2>
<h2 id="___sec3">Simple preprocessing examples, breast cancer data and classification, Support Vector Machines </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
@@ -335,7 +339,62 @@ 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="___sec4">Principal Component Analysis </h2>
<h2 id="___sec4">More on Cancer Data, now with Logistic 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>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
cancer <span style="color: #666666">=</span> load_breast_cancer()
fig, axes <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(<span style="color: #666666">15</span>,<span style="color: #666666">2</span>,figsize<span style="color: #666666">=</span>(<span style="color: #666666">10</span>,<span style="color: #666666">20</span>))
male <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">0</span>]
bene <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">1</span>]
ax <span style="color: #666666">=</span> axes<span style="color: #666666">.</span>ravel()
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">30</span>):
_, bins <span style="color: #666666">=</span> np<span style="color: #666666">.</span>histogram(cancer<span style="color: #666666">.</span>data[:,i], bins <span style="color: #666666">=50</span>)
ax[i]<span style="color: #666666">.</span>hist(male[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
ax[i]<span style="color: #666666">.</span>hist(bene[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
ax[i]<span style="color: #666666">.</span>set_title(cancer<span style="color: #666666">.</span>feature_names[i])
ax[i]<span style="color: #666666">.</span>set_yticks(())
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">&quot;Feature magnitude&quot;</span>)
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>set_ylabel(<span style="color: #BA2121">&quot;Frequency&quot;</span>)
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>legend([<span style="color: #BA2121">&quot;Male&quot;</span>, <span style="color: #BA2121">&quot;Bene&quot;</span>], loc <span style="color: #666666">=</span><span style="color: #BA2121">&quot;best&quot;</span>)
fig<span style="color: #666666">.</span>tight_layout()
plt<span style="color: #666666">.</span>show()
<span style="color: #408080; font-style: italic"># Set up training data</span>
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: #408080; font-style: italic"># Perform Logistic Regression (replace with own code) </span>
logreg <span style="color: #666666">=</span> LogisticRegression()
logreg<span style="color: #666666">.</span>fit(X_train, y_train)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy: {:.2f}&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test,y_test)))
<span style="color: #408080; font-style: italic"># Scale data</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
scaler <span style="color: #666666">=</span> StandardScaler()
scaler<span style="color: #666666">.</span>fit(X_train)
X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_train)
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
<span style="color: #408080; font-style: italic">#svm.fit(X_train_scaled, y_train)</span>
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Test set accuracy scaled data: {:.2f}&quot;</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
<span style="color: #408080; font-style: italic"># Now add PCA</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>)
pca<span style="color: #666666">.</span>fit(X_train_scaled)
X_pca <span style="color: #666666">=</span> pca<span style="color: #666666">.</span>transform(X_train_scaled)
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec5">Principal Component Analysis </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -371,7 +430,7 @@ X2D <span style="color: #666666">=</span> X_centered<span style="color: #666666"
<p>
<!-- !split -->
<h2 id="___sec5">PCA and scikit-learn </h2>
<h2 id="___sec6">PCA and scikit-learn </h2>
<p>
Scikit-Learn&#8217;s PCA class implements PCA using SVD decomposition just like we did before. The
@@ -402,7 +461,7 @@ More material to come here.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec6">More on the PCA </h2>
<h2 id="___sec7">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 &#8212; in that case you will
@@ -430,7 +489,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="___sec7">Incremental PCA </h2>
<h2 id="___sec8">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
@@ -440,7 +499,7 @@ instances arrive).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec8">Randomized PCA </h2>
<h2 id="___sec9">Randomized PCA </h2>
<p>
Scikit-Learn offers yet another option to perform PCA, called Randomized PCA. This is a stochastic
@@ -455,7 +514,7 @@ previous algorithms when \( d \) is much smaller than \( n \).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec9">Kernel PCA </h2>
<h2 id="___sec10">Kernel PCA </h2>
<div class="alert alert-block alert-block alert-text-normal">
<b></b>
<p>
@@ -484,7 +543,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="___sec10">LLE </h2>
<h2 id="___sec11">LLE </h2>
<p>
Locally Linear Embedding (LLE) is another very powerful nonlinear dimensionality reduction
@@ -496,7 +555,7 @@ these local relationships are best preserved (more details shortly).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec11">Other techniques </h2>
<h2 id="___sec12">Other techniques </h2>
<p>
There are many other dimensionality reduction techniques, several of which are available in Scikit-Learn.
+72 -8
View File
@@ -158,7 +158,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simple preprocessing examples, breast cancer data and classification\n",
"## Simple preprocessing examples, breast cancer data and classification, Support Vector Machines\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"
]
@@ -212,6 +212,70 @@
"print(\"Test set accuracy scaled data: {:.2f}\".format(svm.score(X_test_scaled,y_test)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## More on Cancer Data, now with Logistic Regression"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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",
"from sklearn.datasets import load_breast_cancer\n",
"from sklearn.linear_model import LogisticRegression\n",
"cancer = load_breast_cancer()\n",
"\n",
"fig, axes = plt.subplots(15,2,figsize=(10,20))\n",
"male = cancer.data[cancer.target == 0]\n",
"bene = cancer.data[cancer.target == 1]\n",
"ax = axes.ravel()\n",
"\n",
"for i in range(30):\n",
" _, bins = np.histogram(cancer.data[:,i], bins =50)\n",
" ax[i].hist(male[:,i], bins = bins, alpha = 0.5)\n",
" ax[i].hist(bene[:,i], bins = bins, alpha = 0.5)\n",
" ax[i].set_title(cancer.feature_names[i])\n",
" ax[i].set_yticks(())\n",
"ax[0].set_xlabel(\"Feature magnitude\")\n",
"ax[0].set_ylabel(\"Frequency\")\n",
"ax[0].legend([\"Male\", \"Bene\"], loc =\"best\")\n",
"fig.tight_layout()\n",
"plt.show()\n",
"\n",
"# Set up training data\n",
"X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)\n",
"# Perform Logistic Regression (replace with own code) \n",
"logreg = LogisticRegression()\n",
"logreg.fit(X_train, y_train)\n",
"print(\"Test set accuracy: {:.2f}\".format(logreg.score(X_test,y_test)))\n",
"\n",
"# Scale data\n",
"from sklearn.preprocessing import StandardScaler\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",
"logreg.fit(X_train_scaled, y_train)\n",
"#svm.fit(X_train_scaled, y_train)\n",
"print(\"Test set accuracy scaled data: {:.2f}\".format(logreg.score(X_test_scaled,y_test)))\n",
"\n",
"# Now add PCA\n",
"from sklearn.decomposition import PCA\n",
"pca = PCA(n_components = 2)\n",
"pca.fit(X_train_scaled)\n",
"\n",
"X_pca = pca.transform(X_train_scaled)"
]
},
{
"cell_type": "markdown",
"metadata": {},
@@ -226,7 +290,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {
"collapsed": false
},
@@ -253,7 +317,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {
"collapsed": false
},
@@ -277,7 +341,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {
"collapsed": false
},
@@ -299,7 +363,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {
"collapsed": false
},
@@ -328,7 +392,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {
"collapsed": false
},
@@ -351,7 +415,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {
"collapsed": false
},
@@ -397,7 +461,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {
"collapsed": false
},
Binary file not shown.
Binary file not shown.
+54 -1
View File
@@ -139,7 +139,7 @@ print("R2 score for scaled data: {:.2f}".format(svm.score(X_test_scaled,y_test)
!split
===== Simple preprocessing examples, breast cancer data and classification =====
===== Simple preprocessing examples, breast cancer data and classification, Support Vector Machines =====
We show here how we can use a simple regression case on the breast cancer data using support vector machine as algorithm for classification
@@ -188,6 +188,59 @@ print("Test set accuracy scaled data: {:.2f}".format(svm.score(X_test_scaled,y_t
!ec
!split
===== More on Cancer Data, now with Logistic Regression =====
!bc pycod
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
cancer = load_breast_cancer()
fig, axes = plt.subplots(15,2,figsize=(10,20))
male = cancer.data[cancer.target == 0]
bene = cancer.data[cancer.target == 1]
ax = axes.ravel()
for i in range(30):
_, bins = np.histogram(cancer.data[:,i], bins =50)
ax[i].hist(male[:,i], bins = bins, alpha = 0.5)
ax[i].hist(bene[:,i], bins = bins, alpha = 0.5)
ax[i].set_title(cancer.feature_names[i])
ax[i].set_yticks(())
ax[0].set_xlabel("Feature magnitude")
ax[0].set_ylabel("Frequency")
ax[0].legend(["Male", "Bene"], loc ="best")
fig.tight_layout()
plt.show()
# Set up training data
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
# Perform Logistic Regression (replace with own code)
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
print("Test set accuracy: {:.2f}".format(logreg.score(X_test,y_test)))
# Scale data
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
logreg.fit(X_train_scaled, y_train)
#svm.fit(X_train_scaled, y_train)
print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
# Now add PCA
from sklearn.decomposition import PCA
pca = PCA(n_components = 2)
pca.fit(X_train_scaled)
X_pca = pca.transform(X_train_scaled)
!ec
!split
===== Principal Component Analysis =====
+43
View File
@@ -0,0 +1,43 @@
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
print(X_train.shape)
print(X_test.shape)
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
#svm = SVC(C=100)
#svm.fit(X_train, y_train)
print("Test set accuracy: {:.2f}".format(logreg.score(X_test,y_test)))
from sklearn.preprocessing import MinMaxScaler, StandardScaler
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 before scaling:\n {}".format(X_train_scaled.min(axis=0)))
print("Feature max values before scaling:\n {}".format(X_train_scaled.max(axis=0)))
logreg.fit(X_train_scaled, y_train)
#svm.fit(X_train_scaled, y_train)
print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
scaler = StandardScaler()
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
logreg.fit(X_train_scaled, y_train)
#svm.fit(X_train_scaled, y_train)
print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
+40
View File
@@ -0,0 +1,40 @@
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
cancer = load_breast_cancer()
fig, axes = plt.subplots(15,2,figsize=(10,20))
malignant = cancer.data[cancer.target == 0]
benign = cancer.data[cancer.target == 1]
ax = axes.ravel()
for i in range(30):
_, bins = np.histogram(cancer.data[:,i], bins =50)
ax[i].hist(malignant[:,i], bins = bins, alpha = 0.5)
ax[i].hist(benign[:,i], bins = bins, alpha = 0.5)
ax[i].set_title(cancer.feature_names[i])
ax[i].set_yticks(())
ax[0].set_xlabel("Feature magnitude")
ax[0].set_ylabel("Frequency")
ax[0].legend(["Malignant", "Benign"], loc ="best")
fig.tight_layout()
plt.show()
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
print(X_train.shape)
print(X_test.shape)
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
print("Test set accuracy from Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test)))
from sklearn.preprocessing import MinMaxScaler, StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
logreg.fit(X_train_scaled, y_train)
print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))