Updated log reg

This commit is contained in:
mhjensen
2018-09-26 08:27:07 +02:00
parent 6e4633d19f
commit c5cfe919c7
26 changed files with 771 additions and 621 deletions
+22 -18
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -139,7 +143,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Sep 25, 2018</h4></center> <!-- date -->
<center><h4>Sep 26, 2018</h4></center> <!-- date -->
<br>
<p>
<p>
@@ -156,7 +160,7 @@ MathJax.Hub.Config({
<li><a href="._LogReg-bs008.html">9</a></li>
<li><a href="._LogReg-bs009.html">10</a></li>
<li><a href="">...</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+21 -17
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -172,7 +176,7 @@ failure etc.
<li><a href="._LogReg-bs009.html">10</a></li>
<li><a href="._LogReg-bs010.html">11</a></li>
<li><a href="">...</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs002.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+22 -18
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -123,7 +127,7 @@ MathJax.Hub.Config({
<h2 id="___sec1" class="anchor">Optimization and Deep learning </h2>
<p>
Logistic regression will also serve as our stepping towards neural
Logistic regression will also serve as our stepping stone towards neural
network algorithms and supervised deep learning. For logistic
learning, the minimization of the cost function leads to a non-linear
equation in the parameters \( \hat{\beta} \). The optmization of the problem calls therefore for minimization algorithms. This forms the bottle neck of all machine learning algorithms, namely how to find reliable minima of a multi-variable function. This leads us to the family of gradient descent methods. The latter are the working horses of basically all modern machine learning algorithms.
@@ -151,7 +155,7 @@ models, as we will see later.
<li><a href="._LogReg-bs010.html">11</a></li>
<li><a href="._LogReg-bs011.html">12</a></li>
<li><a href="">...</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs003.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+26 -22
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -124,20 +128,20 @@ MathJax.Hub.Config({
<p>
We consider the case where the dependent variables, also called the
responses or the outcomes) \( y_i \) are discrete and only take values
responses or the outcomes, \( y_i \) are discrete and only take values
from \( k=0,\dots,K-1 \) (i.e. \( K \) classes).
<p>
The goal is to predict the
output classes from the design matrix \( X\in\mathbb{R}^{n\times p} \)
made of \( n \) samples, each of which carries \( p \) features. The
output classes from the design matrix \( \hat{X}\in\mathbb{R}^{n\times p} \)
made of \( n \) samples, each of which carries \( p \) features or predictors. The
primary goal is to identify the classes to which new unseen samples
belong.
<p>
Let us specialize to the case of two classes only, with outputs \( y_i=0 \) and \( y_i=1 \). Our outcomes could represent the status of a credit card use who could default or not on her/his debt. That is
Let us specialize to the case of two classes only, with outputs \( y_i=0 \) and \( y_i=1 \). Our outcomes could represent the status of a credit card user who could default or not on her/his credit card debt. That is
$$
y_i = \begin{bmatrix} 0 & \mathrm{no default}\\ 1 & \mathrm{default} \end{bmatrix}.
y_i = \begin{bmatrix} 0 & \mathrm{no}\\ 1 & \mathrm{yes} \end{bmatrix}.
$$
<p>
@@ -159,7 +163,7 @@ $$
<li><a href="._LogReg-bs011.html">12</a></li>
<li><a href="._LogReg-bs012.html">13</a></li>
<li><a href="">...</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs004.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+23 -19
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -130,13 +134,13 @@ We would then have our
weighted linear combination, namely
$$
\begin{equation}
\hat{y} = \hat{X}_i^T\hat{\beta} + \hat{\epsilon},
\hat{y} = \hat{X}^T\hat{\beta} + \hat{\epsilon},
\tag{1}
\end{equation}
$$
where \( \hat{y} \) is a vector representing the possible outcomes, \( \hat{X} \) is our
\( n\times p \) design matrix and \( \hat{\beta} \) represents our estimators.
\( n\times p \) design matrix and \( \hat{\beta} \) represents our estimators/predictors.
<p>
<p>
@@ -158,7 +162,7 @@ where \( \hat{y} \) is a vector representing the possible outcomes, \( \hat{X} \
<li><a href="._LogReg-bs012.html">13</a></li>
<li><a href="._LogReg-bs013.html">14</a></li>
<li><a href="">...</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs005.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+28 -17
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -137,6 +141,13 @@ literature. This model is extremely simple. However, in many cases it is more
favorable to use a ``soft" classifier that outputs
the probability of a given category. This leads us to the logistic function.
<p>
The code for plotting the perceptron can be seen here. This si nothing but the standard <a href="https://en.wikipedia.org/wiki/Heaviside_step_function" target="_self">Heaviside step function</a>.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -158,7 +169,7 @@ the probability of a given category. This leads us to the logistic function.
<li><a href="._LogReg-bs013.html">14</a></li>
<li><a href="._LogReg-bs014.html">15</a></li>
<li><a href="">...</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs006.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+37 -17
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -123,13 +127,27 @@ MathJax.Hub.Config({
<h2 id="___sec5" class="anchor">The logistic function </h2>
<p>
A widely used model is the so-called logistic (or sigmoid) function which ranges from \( 0 \) to \( 1 \),
The perceptron is an example of a ``hard classification&quot; model. We
will encounter this model when we discuss neural networks as
well. Each datapoint is deterministically assigned to a category (i.e
\( y_i=0 \) or \( y_i=1 \)). In many cases, it is favorable to have a &quot;soft&quot;
classifier that outputs the probability of a given category rather
than a single value. For example, given \( x_i \), the classifier
outputs the probability of being in a category \( k \). Logistic regression
is the most common example of a so-called soft classifier. In logistic
regression, the probability that a data point \( x_i \)
belongs to a category \( y_i=\{0,1\} \) is given by the so-called logit function (or Sigmoid) which is meant to represent the likelihood for a given event,
$$
p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}.
$$
Note that \( 1-p(t)= p(-t) \).
The following code plots the logistic function.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -151,6 +169,8 @@ Note that \( 1-p(t)= p(-t) \).
<li><a href="._LogReg-bs013.html">14</a></li>
<li><a href="._LogReg-bs014.html">15</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="">...</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs007.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+37 -27
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -120,19 +124,24 @@ MathJax.Hub.Config({
<a name="part0007"></a>
<!-- !split -->
<h2 id="___sec6" class="anchor">Probabilistic classifiers </h2>
<h2 id="___sec6" class="anchor">Two parameters </h2>
<p>
The perceptron is an example of a ``hard classification&quot; model. We
will encounter this model when we discuss neural networks as
well. Each datapoint is deterministically assigned to a category (i.e
\( y_i=0 \) or \( y_i=1 \)). In many cases, it is favorable to have a &quot;soft&quot;
classifier that outputs the probability of a given category rather
than a single value. For example, given \( x_i \), the classifier
outputs the probability of being in a category \( k \). Logistic regression
is the most common example of a so-called soft classifier. In logistic
regression, the probability that a data point \( x_i \)
belongs to a category \( y_i=\{0,1\} \) is given by the so-called logit function (or Sigmoid)
We assume now that we have two classes with \( y_i \) either \( 0 \) or \( 1 \). Furthermore we assume also that we have only two parameters \( \beta \) in our fitting of the Sigmoid function, that is we define probabilities
$$
\begin{align*}
p(y_i=1|x_i,\hat{\beta}) &= \frac{\exp{(\beta_0+\beta_1x_i)}}{1+\exp{(\beta_0+\beta_1x_i)}},\nonumber\\
p(y_i=0|x_i,\hat{\beta}) &= 1 - p(y_i=1|x_i,\hat{\beta}),
\end{align*}
$$
where \( \hat{\beta} \) are the weights we wish to extract from data, in our case \( \beta_0 \) and \( \beta_1 \).
<p>
Note that we used
$$
p(y_i=0\vert x_i, \hat{\beta}) = 1-p(y_i=1\vert x_i, \hat{\beta}).
$$
<p>
<p>
@@ -155,6 +164,7 @@ belongs to a category \( y_i=\{0,1\} \) is given by the so-called logit function
<li><a href="._LogReg-bs013.html">14</a></li>
<li><a href="._LogReg-bs014.html">15</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs008.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+32 -26
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -118,25 +122,26 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0008"></a>
<!-- !split -->
<!-- !split -->
<h2 id="___sec7" class="anchor">Two parameters </h2>
<h2 id="___sec7" class="anchor">Maximum likelihood </h2>
<p>
We assume now that we have two classes with \( y_i \) either \( 0 \) or \( 1 \). Furthermore we assume also that we have only two parameters \( \beta \) in our fitting of the Sigmoid function, that is we define probabilities
In order to define the total likelihood for all possible outcomes from a
dataset \( \mathcal{D}=\{(y_i,x_i)\} \), with the binary labels
\( y_i\in\{0,1\} \) and where the data points are drawn independently, we use the so-called <a href="https://en.wikipedia.org/wiki/Maximum_likelihood_estimation" target="_self">Maximum Likelihood Estimation</a> (MLE) principle.
We aim thus at maximizing
the probability of seeing the observed data. We can then approximate the
likelihood in terms of the product of the individual probabilities of a specific outcome \( y_i \), that is
$$
\begin{align*}
p(y_i=1|x_i,\hat{\beta}) &= \frac{\exp{(\beta_0+\beta_1x_i)}}{1+\exp{(\beta_0+\beta_1x_i)}},\nonumber\\
p(y_i=0|x_i,\hat{\beta}) &= 1 - p(y_i=1|x_i,\hat{\beta}),
P(\mathcal{D}|\hat{\beta})& = \prod_{i=1}^n \left[p(y_i=1|x_i,\hat{\beta})\right]^{y_i}\left[1-p(y_i=1|x_i,\hat{\beta}))\right]^{1-y_i}\nonumber \\
\end{align*}
$$
where \( \hat{\beta} \) are the weights we wish to extract from data, in our case \( \beta_0 \) and \( \beta_1 \).
<p>
Note that we used
from which we obtain the log-likelihood and our <b>cost/loss</b> function
$$
p(y_i=1) = 1-p(y_i=0).
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left( y_i\log{p(y_i=1|x_i,\hat{\beta})} + (1-y_i)\log\left[1-p(y_i=1|x_i,\hat{\beta}))\right]\right).
$$
<p>
@@ -160,6 +165,7 @@ $$
<li><a href="._LogReg-bs013.html">14</a></li>
<li><a href="._LogReg-bs014.html">15</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs009.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+32 -29
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -118,28 +122,26 @@ MathJax.Hub.Config({
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0009"></a>
<!-- !split -->
<!-- !split -->
<h2 id="___sec8" class="anchor">Maximum likelihood </h2>
<h2 id="___sec8" class="anchor">The cost function rewritten </h2>
<p>
In order to define the total likelihood for all possible outcomes from a
dataset \( \mathcal{D}=\{(y_i,x_i)\} \), with the binary labels
\( y_i\in\{0,1\} \) and where the data points are drawn independently, we use the so-called Maximum Likelihood Estimation (MLE) principle.
We aim thus at maximizing
the probability of seeing the observed data. We can then approximate the
likelihood in terms of the product of the individual probabilities of a specific outcome \( y_i \), that is
Reordering the logarithms, we can rewrite the <b>cost/loss</b> function as
$$
\begin{align*}
P(\mathcal{D}|\hat{\beta})& = \prod_{i=1}^n \left[p(y_i=1|x_i,\hat{\beta})\right]^{y_i}\left[1-p(y_i=1|x_i,\hat{\beta}))\right]^{1-y_i}\nonumber \\
\end{align*}
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right).
$$
from which we obtain the log-likelihood and our <b>cost/loss</b> function
<p>
The maximum likelihood estimator is defined as the set of parameters that maximize the log-likelihood where we maximize with respect to \( \beta \).
Since the cost (error) function is just the negative log-likelihood, for logistic regression we have that
$$
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n y_i\log{p(y_i=1|x_i,\hat{\beta})} + (1-y_i)\log\left[1-p(y_i=1|x_i,\hat{\beta}))\right].
\mathcal{C}(\hat{\beta})=-\sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right).
$$
This equation is known in statistics as the <b>cross entropy</b>. Finally, we note that just as in linear regression,
in practice we often supplement the cross-entropy with additional regularization terms, usually \( L_1 \) and \( L_2 \) regularization as we did for Ridge and Lasso regression.
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -161,6 +163,7 @@ $$
<li><a href="._LogReg-bs013.html">14</a></li>
<li><a href="._LogReg-bs014.html">15</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs010.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+36 -19
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -120,14 +124,26 @@ MathJax.Hub.Config({
<a name="part0010"></a>
<!-- !split -->
<h2 id="___sec9" class="anchor">The cost function rewritten </h2>
<h2 id="___sec9" class="anchor">Minimizing the cross entropy </h2>
<p>
Reordering the logarithms, we can rewrite the <b>cost/loss</b> function as
The cross entropy is a convex function of the weights \( \hat{\beta} \) and,
therefore, any local minimizer is a global minimizer.
<p>
Minimizing this
cost function with respect to the two parameters \( \beta_0 \) and \( \beta_1 \) we obtain
$$
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{1+\exp{(\beta_0+\beta_1x_i)}}\right).
\frac{\partial \mathcal{C}(\hat{\beta})}{\partial \beta_0} = -\sum_{i=1}^n \left(y_i -\frac{\exp{(\beta_0+\beta_1x_i)}}{1+\exp{(\beta_0+\beta_1x_i)}}\right),
$$
and
$$
\frac{\partial \mathcal{C}(\hat{\beta})}{\partial \beta_1} = -\sum_{i=1}^n \left(y_ix_i -x_i\frac{\exp{(\beta_0+\beta_1x_i)}}{1+\exp{(\beta_0+\beta_1x_i)}}\right).
$$
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
@@ -148,6 +164,7 @@ $$
<li><a href="._LogReg-bs013.html">14</a></li>
<li><a href="._LogReg-bs014.html">15</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs011.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+38 -21
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -119,14 +123,26 @@ MathJax.Hub.Config({
<a name="part0011"></a>
<!-- !split -->
The maximum likelihood estimator is defined as the set of parameters that maximize the log-likelihood where we maximize with respect to \( \beta \).
Since the cost (error) function is just the negative log-likelihood, for logistic regression we have that
<h2 id="___sec10" class="anchor">A more compact expression </h2>
<p>
Let us now define a vector \( \hat{y} \) with \( n \) elements \( y_i \), an
\( n\times p \) matrix \( \hat{X} \) which contains the \( x_i \) values and a
vector \( \hat{p} \) of fitted probabilities \( p(y_i\vert x_i,\hat{\beta}) \). We can rewrite in a more compact form the first
derivative of cost function as
$$
\mathcal{C}(\hat{\beta})=-\sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{1+\exp{(\beta_0+\beta_1x_i)}}\right).
\frac{\partial \mathcal{C}(\hat{\beta})}{\partial \hat{\beta}} = -\hat{X}^T\left(\hat{y}-\hat{p}\right).
$$
This equation is known in statistics as the <b>cross entropy</b>. Finally, we note that just as in linear regression,
in practice we often supplement the cross-entropy with additional regularization terms, usually \( L_1 \) and \( L_2 \) regularization as we did for Ridge and Lasso regression.
<p>
If we in addition define a diagonal matrix \( \hat{W} \) with elements
\( p(y_i\vert x_i,\hat{\beta})(1-p(y_i\vert x_i,\hat{\beta}) \), we can obtain a compact expression of the second derivative as
$$
\frac{\partial^2 \mathcal{C}(\hat{\beta})}{\partial \hat{\beta}\partial \hat{\beta}^T} = \hat{X}^T\hat{W}\hat{X}.
$$
<p>
<p>
@@ -148,6 +164,7 @@ in practice we often supplement the cross-entropy with additional regularization
<li><a href="._LogReg-bs013.html">14</a></li>
<li><a href="._LogReg-bs014.html">15</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs012.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+22 -34
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -120,24 +124,7 @@ MathJax.Hub.Config({
<a name="part0012"></a>
<!-- !split -->
<h2 id="___sec10" class="anchor">Minimizing the cross entropy </h2>
<p>
The cross entropy is a convex function of the weights \( \hat{\beta} \) and,
therefore, any local minimizer is a global minimizer.
<p>
Minimizing this
cost function with respect to the two parameters \( \beta_0 \) and \( \beta_1 \) we obtain
$$
\frac{\partial \mathcal{C}(\hat{\beta})}{\partial \beta_0} = -\sum_{i=1}^n \left(y_i -\frac{\exp{(\beta_0+\beta_1x_i)}}{1+\exp{(\beta_0+\beta_1x_i)}}\right),
$$
and
$$
\frac{\partial \mathcal{C}(\hat{\beta})}{\partial \beta_1} = -\sum_{i=1}^n \left(y_ix_i -x_i\frac{\exp{(\beta_0+\beta_1x_i)}}{1+\exp{(\beta_0+\beta_1x_i)}}\right).
$$
<h2 id="___sec11" class="anchor">Extending to more predictors </h2>
<p>
<p>
@@ -158,6 +145,7 @@ $$
<li><a href="._LogReg-bs013.html">14</a></li>
<li><a href="._LogReg-bs014.html">15</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs013.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+22 -35
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -120,25 +124,7 @@ MathJax.Hub.Config({
<a name="part0013"></a>
<!-- !split -->
<h2 id="___sec11" class="anchor">A more compact expression </h2>
<p>
Let us now define a vector \( \hat{y} \) with \( n \) elements \( y_i \), an
\( n\times p \) matrix \( \hat{X} \) which contains the \( x_i \) values and a
vector \( \hat{p} \) of fitted probabilities \( p(y_i\vert x_i,\hat{\beta}) \). We can rewrite in a more compact form the first
derivative of cost function as
$$
\frac{\partial \mathcal{C}(\hat{\beta})}{\partial \hat{\beta}} = -\hat{X}^T\left(\hat{y}-\hat{p}\right).
$$
<p>
If we in addition define a diagonal matrix \( \hat{W} \) with elements
\( p(y_i\vert x_i,\hat{\beta})(1-p(y_i\vert x_i,\hat{\beta}) \), we can obtain a compact expression of the second derivative as
$$
\frac{\partial^2 \mathcal{C}(\hat{\beta})}{\partial \hat{\beta}\partial \hat{\beta}^T} = \hat{X}^T\hat{W}\hat{X}.
$$
<h2 id="___sec12" class="anchor">Including more classes </h2>
<p>
<p>
@@ -158,6 +144,7 @@ $$
<li class="active"><a href="._LogReg-bs013.html">14</a></li>
<li><a href="._LogReg-bs014.html">15</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs014.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+23 -37
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -120,30 +124,11 @@ MathJax.Hub.Config({
<a name="part0014"></a>
<!-- !split -->
<h2 id="___sec12" class="anchor">A <b>scikit-learn</b> example </h2>
<h2 id="___sec13" class="anchor">Optimizing the cost function </h2>
<p>
Newton's method and gradient descent methods
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn</span> <span style="color: #008000; font-weight: bold">import</span> datasets
iris <span style="color: #666666">=</span> datasets<span style="color: #666666">.</span>load_iris()
<span style="color: #008000">list</span>(iris<span style="color: #666666">.</span>keys())
[<span style="color: #BA2121">&#39;data&#39;</span>, <span style="color: #BA2121">&#39;target_names&#39;</span>, <span style="color: #BA2121">&#39;feature_names&#39;</span>, <span style="color: #BA2121">&#39;target&#39;</span>, <span style="color: #BA2121">&#39;DESCR&#39;</span>]
X <span style="color: #666666">=</span> iris[<span style="color: #BA2121">&quot;data&quot;</span>][:, <span style="color: #666666">3</span>:] <span style="color: #408080; font-style: italic"># petal width</span>
y <span style="color: #666666">=</span> (iris[<span style="color: #BA2121">&quot;target&quot;</span>] <span style="color: #666666">==</span> <span style="color: #666666">2</span>)<span style="color: #666666">.</span>astype(np<span style="color: #666666">.</span>int) <span style="color: #408080; font-style: italic"># 1 if Iris-Virginica, else 0</span>
<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
log_reg <span style="color: #666666">=</span> LogisticRegression()
log_reg<span style="color: #666666">.</span>fit(X, y)
X_new <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, <span style="color: #666666">3</span>, <span style="color: #666666">1000</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
y_proba <span style="color: #666666">=</span> log_reg<span style="color: #666666">.</span>predict_proba(X_new)
plt<span style="color: #666666">.</span>plot(X_new, y_proba[:, <span style="color: #666666">1</span>], <span style="color: #BA2121">&quot;g-&quot;</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">&quot;Iris-Virginica&quot;</span>)
plt<span style="color: #666666">.</span>plot(X_new, y_proba[:, <span style="color: #666666">0</span>], <span style="color: #BA2121">&quot;b--&quot;</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">&quot;Not Iris-Virginica&quot;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -161,6 +146,7 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._LogReg-bs013.html">14</a></li>
<li class="active"><a href="._LogReg-bs014.html">15</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs015.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+38 -63
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -120,62 +124,31 @@ MathJax.Hub.Config({
<a name="part0015"></a>
<!-- !split -->
<h2 id="___sec13" class="anchor">A simple classification problem </h2>
<h2 id="___sec14" class="anchor">A <b>scikit-learn</b> example </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">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</span> <span style="color: #008000; font-weight: bold">import</span> datasets, linear_model
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn</span> <span style="color: #008000; font-weight: bold">import</span> datasets
iris <span style="color: #666666">=</span> datasets<span style="color: #666666">.</span>load_iris()
<span style="color: #008000">list</span>(iris<span style="color: #666666">.</span>keys())
[<span style="color: #BA2121">&#39;data&#39;</span>, <span style="color: #BA2121">&#39;target_names&#39;</span>, <span style="color: #BA2121">&#39;feature_names&#39;</span>, <span style="color: #BA2121">&#39;target&#39;</span>, <span style="color: #BA2121">&#39;DESCR&#39;</span>]
X <span style="color: #666666">=</span> iris[<span style="color: #BA2121">&quot;data&quot;</span>][:, <span style="color: #666666">3</span>:] <span style="color: #408080; font-style: italic"># petal width</span>
y <span style="color: #666666">=</span> (iris[<span style="color: #BA2121">&quot;target&quot;</span>] <span style="color: #666666">==</span> <span style="color: #666666">2</span>)<span style="color: #666666">.</span>astype(np<span style="color: #666666">.</span>int) <span style="color: #408080; font-style: italic"># 1 if Iris-Virginica, else 0</span>
<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
log_reg <span style="color: #666666">=</span> LogisticRegression()
log_reg<span style="color: #666666">.</span>fit(X, y)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">generate_data</span>():
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">0</span>)
X, y <span style="color: #666666">=</span> datasets<span style="color: #666666">.</span>make_moons(<span style="color: #666666">200</span>, noise<span style="color: #666666">=0.20</span>)
<span style="color: #008000; font-weight: bold">return</span> X, y
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">visualize</span>(X, y, clf):
<span style="color: #408080; font-style: italic"># plt.scatter(X[:, 0], X[:, 1], s=40, c=y, cmap=plt.cm.Spectral)</span>
<span style="color: #408080; font-style: italic"># plt.show()</span>
plot_decision_boundary(<span style="color: #008000; font-weight: bold">lambda</span> x: clf<span style="color: #666666">.</span>predict(x), X, y)
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">&quot;Logistic Regression&quot;</span>)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">plot_decision_boundary</span>(pred_func, X, y):
<span style="color: #408080; font-style: italic"># Set min and max values and give it some padding</span>
x_min, x_max <span style="color: #666666">=</span> X[:, <span style="color: #666666">0</span>]<span style="color: #666666">.</span>min() <span style="color: #666666">-</span> <span style="color: #666666">.5</span>, X[:, <span style="color: #666666">0</span>]<span style="color: #666666">.</span>max() <span style="color: #666666">+</span> <span style="color: #666666">.5</span>
y_min, y_max <span style="color: #666666">=</span> X[:, <span style="color: #666666">1</span>]<span style="color: #666666">.</span>min() <span style="color: #666666">-</span> <span style="color: #666666">.5</span>, X[:, <span style="color: #666666">1</span>]<span style="color: #666666">.</span>max() <span style="color: #666666">+</span> <span style="color: #666666">.5</span>
h <span style="color: #666666">=</span> <span style="color: #666666">0.01</span>
<span style="color: #408080; font-style: italic"># Generate a grid of points with distance h between them</span>
xx, yy <span style="color: #666666">=</span> np<span style="color: #666666">.</span>meshgrid(np<span style="color: #666666">.</span>arange(x_min, x_max, h), np<span style="color: #666666">.</span>arange(y_min, y_max, h))
<span style="color: #408080; font-style: italic"># Predict the function value for the whole gid</span>
Z <span style="color: #666666">=</span> pred_func(np<span style="color: #666666">.</span>c_[xx<span style="color: #666666">.</span>ravel(), yy<span style="color: #666666">.</span>ravel()])
Z <span style="color: #666666">=</span> Z<span style="color: #666666">.</span>reshape(xx<span style="color: #666666">.</span>shape)
<span style="color: #408080; font-style: italic"># Plot the contour and training examples</span>
plt<span style="color: #666666">.</span>contourf(xx, yy, Z, cmap<span style="color: #666666">=</span>plt<span style="color: #666666">.</span>cm<span style="color: #666666">.</span>Spectral)
plt<span style="color: #666666">.</span>scatter(X[:, <span style="color: #666666">0</span>], X[:, <span style="color: #666666">1</span>], c<span style="color: #666666">=</span>y, cmap<span style="color: #666666">=</span>plt<span style="color: #666666">.</span>cm<span style="color: #666666">.</span>Spectral)
plt<span style="color: #666666">.</span>show()
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">classify</span>(X, y):
clf <span style="color: #666666">=</span> linear_model<span style="color: #666666">.</span>LogisticRegressionCV()
clf<span style="color: #666666">.</span>fit(X, y)
<span style="color: #008000; font-weight: bold">return</span> clf
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">main</span>():
X, y <span style="color: #666666">=</span> generate_data()
<span style="color: #408080; font-style: italic"># visualize(X, y)</span>
clf <span style="color: #666666">=</span> classify(X, y)
visualize(X, y, clf)
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #19177C">__name__</span> <span style="color: #666666">==</span> <span style="color: #BA2121">&quot;__main__&quot;</span>:
main()
X_new <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, <span style="color: #666666">3</span>, <span style="color: #666666">1000</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
y_proba <span style="color: #666666">=</span> log_reg<span style="color: #666666">.</span>predict_proba(X_new)
plt<span style="color: #666666">.</span>plot(X_new, y_proba[:, <span style="color: #666666">1</span>], <span style="color: #BA2121">&quot;g-&quot;</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">&quot;Iris-Virginica&quot;</span>)
plt<span style="color: #666666">.</span>plot(X_new, y_proba[:, <span style="color: #666666">0</span>], <span style="color: #BA2121">&quot;b--&quot;</span>, label<span style="color: #666666">=</span><span style="color: #BA2121">&quot;Not Iris-Virginica&quot;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
@@ -191,6 +164,8 @@ MathJax.Hub.Config({
<li><a href="._LogReg-bs013.html">14</a></li>
<li><a href="._LogReg-bs014.html">15</a></li>
<li class="active"><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs016.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+22 -18
View File
@@ -46,14 +46,16 @@ Automatically generated HTML file from DocOnce source
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -97,14 +99,16 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">The logistic function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Probabilistic classifiers</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec10" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec11" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec12" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec13" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs007.html#___sec6" style="font-size: 80%;">Two parameters</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec7" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec8" style="font-size: 80%;">The cost function rewritten</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs010.html#___sec9" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs011.html#___sec10" style="font-size: 80%;">A more compact expression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs012.html#___sec11" style="font-size: 80%;">Extending to more predictors</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs013.html#___sec12" style="font-size: 80%;">Including more classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs014.html#___sec13" style="font-size: 80%;">Optimizing the cost function</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
</ul>
</li>
@@ -139,7 +143,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Sep 25, 2018</h4></center> <!-- date -->
<center><h4>Sep 26, 2018</h4></center> <!-- date -->
<br>
<p>
<p>
@@ -156,7 +160,7 @@ MathJax.Hub.Config({
<li><a href="._LogReg-bs008.html">9</a></li>
<li><a href="._LogReg-bs009.html">10</a></li>
<li><a href="">...</a></li>
<li><a href="._LogReg-bs015.html">16</a></li>
<li><a href="._LogReg-bs016.html">17</a></li>
<li><a href="._LogReg-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+62 -40
View File
@@ -148,7 +148,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>&nbsp;<br>
<center><h4>Sep 25, 2018</h4></center> <!-- date -->
<center><h4>Sep 26, 2018</h4></center> <!-- date -->
<br>
<p>
@@ -197,7 +197,7 @@ failure etc.
<h2 id="___sec1">Optimization and Deep learning </h2>
<p>
Logistic regression will also serve as our stepping towards neural
Logistic regression will also serve as our stepping stone towards neural
network algorithms and supervised deep learning. For logistic
learning, the minimization of the cost function leads to a non-linear
equation in the parameters \( \hat{\beta} \). The optmization of the problem calls therefore for minimization algorithms. This forms the bottle neck of all machine learning algorithms, namely how to find reliable minima of a multi-variable function. This leads us to the family of gradient descent methods. The latter are the working horses of basically all modern machine learning algorithms.
@@ -214,21 +214,21 @@ models, as we will see later.
<p>
We consider the case where the dependent variables, also called the
responses or the outcomes) \( y_i \) are discrete and only take values
responses or the outcomes, \( y_i \) are discrete and only take values
from \( k=0,\dots,K-1 \) (i.e. \( K \) classes).
<p>
The goal is to predict the
output classes from the design matrix \( X\in\mathbb{R}^{n\times p} \)
made of \( n \) samples, each of which carries \( p \) features. The
output classes from the design matrix \( \hat{X}\in\mathbb{R}^{n\times p} \)
made of \( n \) samples, each of which carries \( p \) features or predictors. The
primary goal is to identify the classes to which new unseen samples
belong.
<p>
Let us specialize to the case of two classes only, with outputs \( y_i=0 \) and \( y_i=1 \). Our outcomes could represent the status of a credit card use who could default or not on her/his debt. That is
Let us specialize to the case of two classes only, with outputs \( y_i=0 \) and \( y_i=1 \). Our outcomes could represent the status of a credit card user who could default or not on her/his credit card debt. That is
<p>&nbsp;<br>
$$
y_i = \begin{bmatrix} 0 & \mathrm{no default}\\ 1 & \mathrm{default} \end{bmatrix}.
y_i = \begin{bmatrix} 0 & \mathrm{no}\\ 1 & \mathrm{yes} \end{bmatrix}.
$$
<p>&nbsp;<br>
</section>
@@ -246,14 +246,14 @@ weighted linear combination, namely
<p>&nbsp;<br>
$$
\begin{equation}
\hat{y} = \hat{X}_i^T\hat{\beta} + \hat{\epsilon},
\hat{y} = \hat{X}^T\hat{\beta} + \hat{\epsilon},
\tag{1}
\end{equation}
$$
<p>&nbsp;<br>
where \( \hat{y} \) is a vector representing the possible outcomes, \( \hat{X} \) is our
\( n\times p \) design matrix and \( \hat{\beta} \) represents our estimators.
\( n\times p \) design matrix and \( \hat{\beta} \) represents our estimators/predictors.
</section>
@@ -274,27 +274,20 @@ We will encounter this model in our first demonstration of neural networks. Hist
literature. This model is extremely simple. However, in many cases it is more
favorable to use a ``soft" classifier that outputs
the probability of a given category. This leads us to the logistic function.
<p>
The code for plotting the perceptron can be seen here. This si nothing but the standard <a href="https://en.wikipedia.org/wiki/Heaviside_step_function" target="_blank">Heaviside step function</a>.
<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>
</pre></div>
</section>
<section>
<h2 id="___sec5">The logistic function </h2>
<p>
A widely used model is the so-called logistic (or sigmoid) function which ranges from \( 0 \) to \( 1 \),
<p>&nbsp;<br>
$$
p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}.
$$
<p>&nbsp;<br>
Note that \( 1-p(t)= p(-t) \).
</section>
<section>
<h2 id="___sec6">Probabilistic classifiers </h2>
<p>
The perceptron is an example of a ``hard classification&quot; model. We
will encounter this model when we discuss neural networks as
@@ -305,12 +298,25 @@ than a single value. For example, given \( x_i \), the classifier
outputs the probability of being in a category \( k \). Logistic regression
is the most common example of a so-called soft classifier. In logistic
regression, the probability that a data point \( x_i \)
belongs to a category \( y_i=\{0,1\} \) is given by the so-called logit function (or Sigmoid)
belongs to a category \( y_i=\{0,1\} \) is given by the so-called logit function (or Sigmoid) which is meant to represent the likelihood for a given event,
<p>&nbsp;<br>
$$
p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}.
$$
<p>&nbsp;<br>
Note that \( 1-p(t)= p(-t) \).
The following code plots the logistic function.
<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>
</pre></div>
</section>
<section>
<h2 id="___sec7">Two parameters </h2>
<h2 id="___sec6">Two parameters </h2>
<p>
We assume now that we have two classes with \( y_i \) either \( 0 \) or \( 1 \). Furthermore we assume also that we have only two parameters \( \beta \) in our fitting of the Sigmoid function, that is we define probabilities
@@ -329,19 +335,19 @@ where \( \hat{\beta} \) are the weights we wish to extract from data, in our cas
Note that we used
<p>&nbsp;<br>
$$
p(y_i=1) = 1-p(y_i=0).
p(y_i=0\vert x_i, \hat{\beta}) = 1-p(y_i=1\vert x_i, \hat{\beta}).
$$
<p>&nbsp;<br>
</section>
<section>
<h2 id="___sec8">Maximum likelihood </h2>
<h2 id="___sec7">Maximum likelihood </h2>
<p>
In order to define the total likelihood for all possible outcomes from a
dataset \( \mathcal{D}=\{(y_i,x_i)\} \), with the binary labels
\( y_i\in\{0,1\} \) and where the data points are drawn independently, we use the so-called Maximum Likelihood Estimation (MLE) principle.
\( y_i\in\{0,1\} \) and where the data points are drawn independently, we use the so-called <a href="https://en.wikipedia.org/wiki/Maximum_likelihood_estimation" target="_blank">Maximum Likelihood Estimation</a> (MLE) principle.
We aim thus at maximizing
the probability of seeing the observed data. We can then approximate the
likelihood in terms of the product of the individual probabilities of a specific outcome \( y_i \), that is
@@ -356,31 +362,29 @@ $$
from which we obtain the log-likelihood and our <b>cost/loss</b> function
<p>&nbsp;<br>
$$
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n y_i\log{p(y_i=1|x_i,\hat{\beta})} + (1-y_i)\log\left[1-p(y_i=1|x_i,\hat{\beta}))\right].
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left( y_i\log{p(y_i=1|x_i,\hat{\beta})} + (1-y_i)\log\left[1-p(y_i=1|x_i,\hat{\beta}))\right]\right).
$$
<p>&nbsp;<br>
</section>
<section>
<h2 id="___sec9">The cost function rewritten </h2>
<h2 id="___sec8">The cost function rewritten </h2>
<p>
Reordering the logarithms, we can rewrite the <b>cost/loss</b> function as
<p>&nbsp;<br>
$$
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{1+\exp{(\beta_0+\beta_1x_i)}}\right).
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right).
$$
<p>&nbsp;<br>
</section>
<section>
<p>
The maximum likelihood estimator is defined as the set of parameters that maximize the log-likelihood where we maximize with respect to \( \beta \).
Since the cost (error) function is just the negative log-likelihood, for logistic regression we have that
<p>&nbsp;<br>
$$
\mathcal{C}(\hat{\beta})=-\sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{1+\exp{(\beta_0+\beta_1x_i)}}\right).
\mathcal{C}(\hat{\beta})=-\sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right).
$$
<p>&nbsp;<br>
@@ -390,7 +394,7 @@ in practice we often supplement the cross-entropy with additional regularization
<section>
<h2 id="___sec10">Minimizing the cross entropy </h2>
<h2 id="___sec9">Minimizing the cross entropy </h2>
<p>
The cross entropy is a convex function of the weights \( \hat{\beta} \) and,
@@ -416,7 +420,7 @@ $$
<section>
<h2 id="___sec11">A more compact expression </h2>
<h2 id="___sec10">A more compact expression </h2>
<p>
Let us now define a vector \( \hat{y} \) with \( n \) elements \( y_i \), an
@@ -443,7 +447,25 @@ $$
<section>
<h2 id="___sec12">A <b>scikit-learn</b> example </h2>
<h2 id="___sec11">Extending to more predictors </h2>
</section>
<section>
<h2 id="___sec12">Including more classes </h2>
</section>
<section>
<h2 id="___sec13">Optimizing the cost function </h2>
<p>
Newton's method and gradient descent methods
</section>
<section>
<h2 id="___sec14">A <b>scikit-learn</b> example </h2>
<p>
@@ -471,7 +493,7 @@ plt.show()
<section>
<h2 id="___sec13">A simple classification problem </h2>
<h2 id="___sec15">A simple classification problem </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
+68 -44
View File
@@ -40,14 +40,16 @@ div { text-align: justify; text-justify: inter-word; }
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -89,7 +91,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Sep 25, 2018</h4></center> <!-- date -->
<center><h4>Sep 26, 2018</h4></center> <!-- date -->
<br>
<p>
<!-- !split -->
@@ -132,7 +134,7 @@ failure etc.
<h2 id="___sec1">Optimization and Deep learning </h2>
<p>
Logistic regression will also serve as our stepping towards neural
Logistic regression will also serve as our stepping stone towards neural
network algorithms and supervised deep learning. For logistic
learning, the minimization of the cost function leads to a non-linear
equation in the parameters \( \hat{\beta} \). The optmization of the problem calls therefore for minimization algorithms. This forms the bottle neck of all machine learning algorithms, namely how to find reliable minima of a multi-variable function. This leads us to the family of gradient descent methods. The latter are the working horses of basically all modern machine learning algorithms.
@@ -149,20 +151,20 @@ models, as we will see later.
<p>
We consider the case where the dependent variables, also called the
responses or the outcomes) \( y_i \) are discrete and only take values
responses or the outcomes, \( y_i \) are discrete and only take values
from \( k=0,\dots,K-1 \) (i.e. \( K \) classes).
<p>
The goal is to predict the
output classes from the design matrix \( X\in\mathbb{R}^{n\times p} \)
made of \( n \) samples, each of which carries \( p \) features. The
output classes from the design matrix \( \hat{X}\in\mathbb{R}^{n\times p} \)
made of \( n \) samples, each of which carries \( p \) features or predictors. The
primary goal is to identify the classes to which new unseen samples
belong.
<p>
Let us specialize to the case of two classes only, with outputs \( y_i=0 \) and \( y_i=1 \). Our outcomes could represent the status of a credit card use who could default or not on her/his debt. That is
Let us specialize to the case of two classes only, with outputs \( y_i=0 \) and \( y_i=1 \). Our outcomes could represent the status of a credit card user who could default or not on her/his credit card debt. That is
$$
y_i = \begin{bmatrix} 0 & \mathrm{no default}\\ 1 & \mathrm{default} \end{bmatrix}.
y_i = \begin{bmatrix} 0 & \mathrm{no}\\ 1 & \mathrm{yes} \end{bmatrix}.
$$
<p>
@@ -178,13 +180,13 @@ We would then have our
weighted linear combination, namely
$$
\begin{equation}
\hat{y} = \hat{X}_i^T\hat{\beta} + \hat{\epsilon},
\hat{y} = \hat{X}^T\hat{\beta} + \hat{\epsilon},
\label{_auto1}
\end{equation}
$$
where \( \hat{y} \) is a vector representing the possible outcomes, \( \hat{X} \) is our
\( n\times p \) design matrix and \( \hat{\beta} \) represents our estimators.
\( n\times p \) design matrix and \( \hat{\beta} \) represents our estimators/predictors.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
@@ -206,24 +208,18 @@ literature. This model is extremely simple. However, in many cases it is more
favorable to use a ``soft" classifier that outputs
the probability of a given category. This leads us to the logistic function.
<p>
The code for plotting the perceptron can be seen here. This si nothing but the standard <a href="https://en.wikipedia.org/wiki/Heaviside_step_function" target="_blank">Heaviside step function</a>.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec5">The logistic function </h2>
<p>
A widely used model is the so-called logistic (or sigmoid) function which ranges from \( 0 \) to \( 1 \),
$$
p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}.
$$
Note that \( 1-p(t)= p(-t) \).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec6">Probabilistic classifiers </h2>
<p>
The perceptron is an example of a ``hard classification&quot; model. We
will encounter this model when we discuss neural networks as
@@ -234,12 +230,22 @@ than a single value. For example, given \( x_i \), the classifier
outputs the probability of being in a category \( k \). Logistic regression
is the most common example of a so-called soft classifier. In logistic
regression, the probability that a data point \( x_i \)
belongs to a category \( y_i=\{0,1\} \) is given by the so-called logit function (or Sigmoid)
belongs to a category \( y_i=\{0,1\} \) is given by the so-called logit function (or Sigmoid) which is meant to represent the likelihood for a given event,
$$
p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}.
$$
Note that \( 1-p(t)= p(-t) \).
The following code plots the logistic function.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec7">Two parameters </h2>
<h2 id="___sec6">Two parameters </h2>
<p>
We assume now that we have two classes with \( y_i \) either \( 0 \) or \( 1 \). Furthermore we assume also that we have only two parameters \( \beta \) in our fitting of the Sigmoid function, that is we define probabilities
@@ -255,18 +261,18 @@ where \( \hat{\beta} \) are the weights we wish to extract from data, in our cas
<p>
Note that we used
$$
p(y_i=1) = 1-p(y_i=0).
p(y_i=0\vert x_i, \hat{\beta}) = 1-p(y_i=1\vert x_i, \hat{\beta}).
$$
<p>
<!-- !split -->
<h2 id="___sec8">Maximum likelihood </h2>
<h2 id="___sec7">Maximum likelihood </h2>
<p>
In order to define the total likelihood for all possible outcomes from a
dataset \( \mathcal{D}=\{(y_i,x_i)\} \), with the binary labels
\( y_i\in\{0,1\} \) and where the data points are drawn independently, we use the so-called Maximum Likelihood Estimation (MLE) principle.
\( y_i\in\{0,1\} \) and where the data points are drawn independently, we use the so-called <a href="https://en.wikipedia.org/wiki/Maximum_likelihood_estimation" target="_blank">Maximum Likelihood Estimation</a> (MLE) principle.
We aim thus at maximizing
the probability of seeing the observed data. We can then approximate the
likelihood in terms of the product of the individual probabilities of a specific outcome \( y_i \), that is
@@ -278,25 +284,25 @@ $$
from which we obtain the log-likelihood and our <b>cost/loss</b> function
$$
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n y_i\log{p(y_i=1|x_i,\hat{\beta})} + (1-y_i)\log\left[1-p(y_i=1|x_i,\hat{\beta}))\right].
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left( y_i\log{p(y_i=1|x_i,\hat{\beta})} + (1-y_i)\log\left[1-p(y_i=1|x_i,\hat{\beta}))\right]\right).
$$
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec9">The cost function rewritten </h2>
<h2 id="___sec8">The cost function rewritten </h2>
<p>
Reordering the logarithms, we can rewrite the <b>cost/loss</b> function as
$$
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{1+\exp{(\beta_0+\beta_1x_i)}}\right).
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right).
$$
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<p>
The maximum likelihood estimator is defined as the set of parameters that maximize the log-likelihood where we maximize with respect to \( \beta \).
Since the cost (error) function is just the negative log-likelihood, for logistic regression we have that
$$
\mathcal{C}(\hat{\beta})=-\sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{1+\exp{(\beta_0+\beta_1x_i)}}\right).
\mathcal{C}(\hat{\beta})=-\sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right).
$$
This equation is known in statistics as the <b>cross entropy</b>. Finally, we note that just as in linear regression,
@@ -305,7 +311,7 @@ in practice we often supplement the cross-entropy with additional regularization
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec10">Minimizing the cross entropy </h2>
<h2 id="___sec9">Minimizing the cross entropy </h2>
<p>
The cross entropy is a convex function of the weights \( \hat{\beta} \) and,
@@ -327,7 +333,7 @@ $$
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec11">A more compact expression </h2>
<h2 id="___sec10">A more compact expression </h2>
<p>
Let us now define a vector \( \hat{y} \) with \( n \) elements \( y_i \), an
@@ -350,7 +356,25 @@ $$
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec12">A <b>scikit-learn</b> example </h2>
<h2 id="___sec11">Extending to more predictors </h2>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec12">Including more classes </h2>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec13">Optimizing the cost function </h2>
<p>
Newton's method and gradient descent methods
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec14">A <b>scikit-learn</b> example </h2>
<p>
@@ -377,7 +401,7 @@ plt.show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec13">A simple classification problem </h2>
<h2 id="___sec15">A simple classification problem </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
+68 -44
View File
@@ -45,14 +45,16 @@ div { text-align: justify; text-justify: inter-word; }
('Linear classifier', 2, None, '___sec3'),
('Some selected properties', 2, None, '___sec4'),
('The logistic function', 2, None, '___sec5'),
('Probabilistic classifiers', 2, None, '___sec6'),
('Two parameters', 2, None, '___sec7'),
('Maximum likelihood', 2, None, '___sec8'),
('The cost function rewritten', 2, None, '___sec9'),
('Minimizing the cross entropy', 2, None, '___sec10'),
('A more compact expression', 2, None, '___sec11'),
('A _scikit-learn_ example', 2, None, '___sec12'),
('A simple classification problem', 2, None, '___sec13')]}
('Two parameters', 2, None, '___sec6'),
('Maximum likelihood', 2, None, '___sec7'),
('The cost function rewritten', 2, None, '___sec8'),
('Minimizing the cross entropy', 2, None, '___sec9'),
('A more compact expression', 2, None, '___sec10'),
('Extending to more predictors', 2, None, '___sec11'),
('Including more classes', 2, None, '___sec12'),
('Optimizing the cost function', 2, None, '___sec13'),
('A _scikit-learn_ example', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15')]}
end of tocinfo -->
<body>
@@ -94,7 +96,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Sep 25, 2018</h4></center> <!-- date -->
<center><h4>Sep 26, 2018</h4></center> <!-- date -->
<br>
<p>
<!-- !split -->
@@ -137,7 +139,7 @@ failure etc.
<h2 id="___sec1">Optimization and Deep learning </h2>
<p>
Logistic regression will also serve as our stepping towards neural
Logistic regression will also serve as our stepping stone towards neural
network algorithms and supervised deep learning. For logistic
learning, the minimization of the cost function leads to a non-linear
equation in the parameters \( \hat{\beta} \). The optmization of the problem calls therefore for minimization algorithms. This forms the bottle neck of all machine learning algorithms, namely how to find reliable minima of a multi-variable function. This leads us to the family of gradient descent methods. The latter are the working horses of basically all modern machine learning algorithms.
@@ -154,20 +156,20 @@ models, as we will see later.
<p>
We consider the case where the dependent variables, also called the
responses or the outcomes) \( y_i \) are discrete and only take values
responses or the outcomes, \( y_i \) are discrete and only take values
from \( k=0,\dots,K-1 \) (i.e. \( K \) classes).
<p>
The goal is to predict the
output classes from the design matrix \( X\in\mathbb{R}^{n\times p} \)
made of \( n \) samples, each of which carries \( p \) features. The
output classes from the design matrix \( \hat{X}\in\mathbb{R}^{n\times p} \)
made of \( n \) samples, each of which carries \( p \) features or predictors. The
primary goal is to identify the classes to which new unseen samples
belong.
<p>
Let us specialize to the case of two classes only, with outputs \( y_i=0 \) and \( y_i=1 \). Our outcomes could represent the status of a credit card use who could default or not on her/his debt. That is
Let us specialize to the case of two classes only, with outputs \( y_i=0 \) and \( y_i=1 \). Our outcomes could represent the status of a credit card user who could default or not on her/his credit card debt. That is
$$
y_i = \begin{bmatrix} 0 & \mathrm{no default}\\ 1 & \mathrm{default} \end{bmatrix}.
y_i = \begin{bmatrix} 0 & \mathrm{no}\\ 1 & \mathrm{yes} \end{bmatrix}.
$$
<p>
@@ -183,13 +185,13 @@ We would then have our
weighted linear combination, namely
$$
\begin{equation}
\hat{y} = \hat{X}_i^T\hat{\beta} + \hat{\epsilon},
\hat{y} = \hat{X}^T\hat{\beta} + \hat{\epsilon},
\label{_auto1}
\end{equation}
$$
where \( \hat{y} \) is a vector representing the possible outcomes, \( \hat{X} \) is our
\( n\times p \) design matrix and \( \hat{\beta} \) represents our estimators.
\( n\times p \) design matrix and \( \hat{\beta} \) represents our estimators/predictors.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
@@ -211,24 +213,18 @@ literature. This model is extremely simple. However, in many cases it is more
favorable to use a ``soft" classifier that outputs
the probability of a given category. This leads us to the logistic function.
<p>
The code for plotting the perceptron can be seen here. This si nothing but the standard <a href="https://en.wikipedia.org/wiki/Heaviside_step_function" target="_blank">Heaviside step function</a>.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec5">The logistic function </h2>
<p>
A widely used model is the so-called logistic (or sigmoid) function which ranges from \( 0 \) to \( 1 \),
$$
p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}.
$$
Note that \( 1-p(t)= p(-t) \).
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec6">Probabilistic classifiers </h2>
<p>
The perceptron is an example of a ``hard classification&quot; model. We
will encounter this model when we discuss neural networks as
@@ -239,12 +235,22 @@ than a single value. For example, given \( x_i \), the classifier
outputs the probability of being in a category \( k \). Logistic regression
is the most common example of a so-called soft classifier. In logistic
regression, the probability that a data point \( x_i \)
belongs to a category \( y_i=\{0,1\} \) is given by the so-called logit function (or Sigmoid)
belongs to a category \( y_i=\{0,1\} \) is given by the so-called logit function (or Sigmoid) which is meant to represent the likelihood for a given event,
$$
p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}.
$$
Note that \( 1-p(t)= p(-t) \).
The following code plots the logistic function.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec7">Two parameters </h2>
<h2 id="___sec6">Two parameters </h2>
<p>
We assume now that we have two classes with \( y_i \) either \( 0 \) or \( 1 \). Furthermore we assume also that we have only two parameters \( \beta \) in our fitting of the Sigmoid function, that is we define probabilities
@@ -260,18 +266,18 @@ where \( \hat{\beta} \) are the weights we wish to extract from data, in our cas
<p>
Note that we used
$$
p(y_i=1) = 1-p(y_i=0).
p(y_i=0\vert x_i, \hat{\beta}) = 1-p(y_i=1\vert x_i, \hat{\beta}).
$$
<p>
<!-- !split -->
<h2 id="___sec8">Maximum likelihood </h2>
<h2 id="___sec7">Maximum likelihood </h2>
<p>
In order to define the total likelihood for all possible outcomes from a
dataset \( \mathcal{D}=\{(y_i,x_i)\} \), with the binary labels
\( y_i\in\{0,1\} \) and where the data points are drawn independently, we use the so-called Maximum Likelihood Estimation (MLE) principle.
\( y_i\in\{0,1\} \) and where the data points are drawn independently, we use the so-called <a href="https://en.wikipedia.org/wiki/Maximum_likelihood_estimation" target="_blank">Maximum Likelihood Estimation</a> (MLE) principle.
We aim thus at maximizing
the probability of seeing the observed data. We can then approximate the
likelihood in terms of the product of the individual probabilities of a specific outcome \( y_i \), that is
@@ -283,25 +289,25 @@ $$
from which we obtain the log-likelihood and our <b>cost/loss</b> function
$$
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n y_i\log{p(y_i=1|x_i,\hat{\beta})} + (1-y_i)\log\left[1-p(y_i=1|x_i,\hat{\beta}))\right].
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left( y_i\log{p(y_i=1|x_i,\hat{\beta})} + (1-y_i)\log\left[1-p(y_i=1|x_i,\hat{\beta}))\right]\right).
$$
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec9">The cost function rewritten </h2>
<h2 id="___sec8">The cost function rewritten </h2>
<p>
Reordering the logarithms, we can rewrite the <b>cost/loss</b> function as
$$
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{1+\exp{(\beta_0+\beta_1x_i)}}\right).
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right).
$$
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<p>
The maximum likelihood estimator is defined as the set of parameters that maximize the log-likelihood where we maximize with respect to \( \beta \).
Since the cost (error) function is just the negative log-likelihood, for logistic regression we have that
$$
\mathcal{C}(\hat{\beta})=-\sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{1+\exp{(\beta_0+\beta_1x_i)}}\right).
\mathcal{C}(\hat{\beta})=-\sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right).
$$
This equation is known in statistics as the <b>cross entropy</b>. Finally, we note that just as in linear regression,
@@ -310,7 +316,7 @@ in practice we often supplement the cross-entropy with additional regularization
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec10">Minimizing the cross entropy </h2>
<h2 id="___sec9">Minimizing the cross entropy </h2>
<p>
The cross entropy is a convex function of the weights \( \hat{\beta} \) and,
@@ -332,7 +338,7 @@ $$
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec11">A more compact expression </h2>
<h2 id="___sec10">A more compact expression </h2>
<p>
Let us now define a vector \( \hat{y} \) with \( n \) elements \( y_i \), an
@@ -355,7 +361,25 @@ $$
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec12">A <b>scikit-learn</b> example </h2>
<h2 id="___sec11">Extending to more predictors </h2>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec12">Including more classes </h2>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec13">Optimizing the cost function </h2>
<p>
Newton's method and gradient descent methods
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec14">A <b>scikit-learn</b> example </h2>
<p>
@@ -382,7 +406,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec13">A simple classification problem </h2>
<h2 id="___sec15">A simple classification problem </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
+48 -30
View File
@@ -10,7 +10,7 @@
"<!-- Author: --> \n",
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
"\n",
"Date: **Sep 25, 2018**\n",
"Date: **Sep 26, 2018**\n",
"\n",
"Copyright 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
"\n",
@@ -50,7 +50,7 @@
"\n",
"## Optimization and Deep learning\n",
"\n",
"Logistic regression will also serve as our stepping towards neural\n",
"Logistic regression will also serve as our stepping stone towards neural\n",
"network algorithms and supervised deep learning. For logistic\n",
"learning, the minimization of the cost function leads to a non-linear\n",
"equation in the parameters $\\hat{\\beta}$. The optmization of the problem calls therefore for minimization algorithms. This forms the bottle neck of all machine learning algorithms, namely how to find reliable minima of a multi-variable function. This leads us to the family of gradient descent methods. The latter are the working horses of basically all modern machine learning algorithms. \n",
@@ -64,16 +64,16 @@
"## Basics\n",
"\n",
"We consider the case where the dependent variables, also called the\n",
"responses or the outcomes) $y_i$ are discrete and only take values\n",
"responses or the outcomes, $y_i$ are discrete and only take values\n",
"from $k=0,\\dots,K-1$ (i.e. $K$ classes).\n",
"\n",
"The goal is to predict the\n",
"output classes from the design matrix $X\\in\\mathbb{R}^{n\\times p}$\n",
"made of $n$ samples, each of which carries $p$ features. The\n",
"output classes from the design matrix $\\hat{X}\\in\\mathbb{R}^{n\\times p}$\n",
"made of $n$ samples, each of which carries $p$ features or predictors. The\n",
"primary goal is to identify the classes to which new unseen samples\n",
"belong.\n",
"\n",
"Let us specialize to the case of two classes only, with outputs $y_i=0$ and $y_i=1$. Our outcomes could represent the status of a credit card use who could default or not on her/his debt. That is"
"Let us specialize to the case of two classes only, with outputs $y_i=0$ and $y_i=1$. Our outcomes could represent the status of a credit card user who could default or not on her/his credit card debt. That is"
]
},
{
@@ -81,7 +81,7 @@
"metadata": {},
"source": [
"$$\n",
"y_i = \\begin{bmatrix} 0 & \\mathrm{no default}\\\\ 1 & \\mathrm{default} \\end{bmatrix}.\n",
"y_i = \\begin{bmatrix} 0 & \\mathrm{no}\\\\ 1 & \\mathrm{yes} \\end{bmatrix}.\n",
"$$"
]
},
@@ -106,7 +106,7 @@
"\n",
"$$\n",
"\\begin{equation}\n",
"\\hat{y} = \\hat{X}_i^T\\hat{\\beta} + \\hat{\\epsilon},\n",
"\\hat{y} = \\hat{X}^T\\hat{\\beta} + \\hat{\\epsilon},\n",
"\\label{_auto1} \\tag{1}\n",
"\\end{equation}\n",
"$$"
@@ -117,7 +117,7 @@
"metadata": {},
"source": [
"where $\\hat{y}$ is a vector representing the possible outcomes, $\\hat{X}$ is our\n",
"$n\\times p$ design matrix and $\\hat{\\beta}$ represents our estimators.\n",
"$n\\times p$ design matrix and $\\hat{\\beta}$ represents our estimators/predictors.\n",
"\n",
"## Some selected properties\n",
"\n",
@@ -134,9 +134,25 @@
"favorable to use a ``soft\" classifier that outputs\n",
"the probability of a given category. This leads us to the logistic function.\n",
"\n",
"The code for plotting the perceptron can be seen here. This si nothing but the standard [Heaviside step function](https://en.wikipedia.org/wiki/Heaviside_step_function)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The logistic function\n",
"\n",
"A widely used model is the so-called logistic (or sigmoid) function which ranges from $0$ to $1$,"
"The perceptron is an example of a ``hard classification\" model. We\n",
"will encounter this model when we discuss neural networks as\n",
"well. Each datapoint is deterministically assigned to a category (i.e\n",
"$y_i=0$ or $y_i=1$). In many cases, it is favorable to have a \"soft\"\n",
"classifier that outputs the probability of a given category rather\n",
"than a single value. For example, given $x_i$, the classifier\n",
"outputs the probability of being in a category $k$. Logistic regression\n",
"is the most common example of a so-called soft classifier. In logistic\n",
"regression, the probability that a data point $x_i$\n",
"belongs to a category $y_i=\\{0,1\\}$ is given by the so-called logit function (or Sigmoid) which is meant to represent the likelihood for a given event,"
]
},
{
@@ -153,21 +169,13 @@
"metadata": {},
"source": [
"Note that $1-p(t)= p(-t)$.\n",
"\n",
"## Probabilistic classifiers\n",
"\n",
"The perceptron is an example of a ``hard classification\" model. We\n",
"will encounter this model when we discuss neural networks as\n",
"well. Each datapoint is deterministically assigned to a category (i.e\n",
"$y_i=0$ or $y_i=1$). In many cases, it is favorable to have a \"soft\"\n",
"classifier that outputs the probability of a given category rather\n",
"than a single value. For example, given $x_i$, the classifier\n",
"outputs the probability of being in a category $k$. Logistic regression\n",
"is the most common example of a so-called soft classifier. In logistic\n",
"regression, the probability that a data point $x_i$\n",
"belongs to a category $y_i=\\{0,1\\}$ is given by the so-called logit function (or Sigmoid)\n",
"\n",
"\n",
"The following code plots the logistic function."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Two parameters\n",
"\n",
"We assume now that we have two classes with $y_i$ either $0$ or $1$. Furthermore we assume also that we have only two parameters $\\beta$ in our fitting of the Sigmoid function, that is we define probabilities"
@@ -199,7 +207,7 @@
"metadata": {},
"source": [
"$$\n",
"p(y_i=1) = 1-p(y_i=0).\n",
"p(y_i=0\\vert x_i, \\hat{\\beta}) = 1-p(y_i=1\\vert x_i, \\hat{\\beta}).\n",
"$$"
]
},
@@ -212,7 +220,7 @@
"\n",
"In order to define the total likelihood for all possible outcomes from a \n",
"dataset $\\mathcal{D}=\\{(y_i,x_i)\\}$, with the binary labels\n",
"$y_i\\in\\{0,1\\}$ and where the data points are drawn independently, we use the so-called Maximum Likelihood Estimation (MLE) principle. \n",
"$y_i\\in\\{0,1\\}$ and where the data points are drawn independently, we use the so-called [Maximum Likelihood Estimation](https://en.wikipedia.org/wiki/Maximum_likelihood_estimation) (MLE) principle. \n",
"We aim thus at maximizing \n",
"the probability of seeing the observed data. We can then approximate the \n",
"likelihood in terms of the product of the individual probabilities of a specific outcome $y_i$, that is"
@@ -241,7 +249,7 @@
"metadata": {},
"source": [
"$$\n",
"\\mathcal{C}(\\hat{\\beta}) = \\sum_{i=1}^n y_i\\log{p(y_i=1|x_i,\\hat{\\beta})} + (1-y_i)\\log\\left[1-p(y_i=1|x_i,\\hat{\\beta}))\\right].\n",
"\\mathcal{C}(\\hat{\\beta}) = \\sum_{i=1}^n \\left( y_i\\log{p(y_i=1|x_i,\\hat{\\beta})} + (1-y_i)\\log\\left[1-p(y_i=1|x_i,\\hat{\\beta}))\\right]\\right).\n",
"$$"
]
},
@@ -259,7 +267,7 @@
"metadata": {},
"source": [
"$$\n",
"\\mathcal{C}(\\hat{\\beta}) = \\sum_{i=1}^n \\left(y_i(\\beta_0+\\beta_1x_i) -\\log{1+\\exp{(\\beta_0+\\beta_1x_i)}}\\right).\n",
"\\mathcal{C}(\\hat{\\beta}) = \\sum_{i=1}^n \\left(y_i(\\beta_0+\\beta_1x_i) -\\log{(1+\\exp{(\\beta_0+\\beta_1x_i)})}\\right).\n",
"$$"
]
},
@@ -276,7 +284,7 @@
"metadata": {},
"source": [
"$$\n",
"\\mathcal{C}(\\hat{\\beta})=-\\sum_{i=1}^n \\left(y_i(\\beta_0+\\beta_1x_i) -\\log{1+\\exp{(\\beta_0+\\beta_1x_i)}}\\right).\n",
"\\mathcal{C}(\\hat{\\beta})=-\\sum_{i=1}^n \\left(y_i(\\beta_0+\\beta_1x_i) -\\log{(1+\\exp{(\\beta_0+\\beta_1x_i)})}\\right).\n",
"$$"
]
},
@@ -364,6 +372,16 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Extending to more predictors\n",
"\n",
"## Including more classes\n",
"\n",
"## Optimizing the cost function\n",
"\n",
"Newton's method and gradient descent methods\n",
"\n",
"\n",
"\n",
"## A **scikit-learn** example"
]
},
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+44 -26
View File
@@ -37,7 +37,7 @@ failure etc.
!split
===== Optimization and Deep learning =====
Logistic regression will also serve as our stepping towards neural
Logistic regression will also serve as our stepping stone towards neural
network algorithms and supervised deep learning. For logistic
learning, the minimization of the cost function leads to a non-linear
equation in the parameters $\hat{\beta}$. The optmization of the problem calls therefore for minimization algorithms. This forms the bottle neck of all machine learning algorithms, namely how to find reliable minima of a multi-variable function. This leads us to the family of gradient descent methods. The latter are the working horses of basically all modern machine learning algorithms.
@@ -51,19 +51,19 @@ models, as we will see later.
===== Basics =====
We consider the case where the dependent variables, also called the
responses or the outcomes) $y_i$ are discrete and only take values
responses or the outcomes, $y_i$ are discrete and only take values
from $k=0,\dots,K-1$ (i.e. $K$ classes).
The goal is to predict the
output classes from the design matrix $X\in\mathbb{R}^{n\times p}$
made of $n$ samples, each of which carries $p$ features. The
output classes from the design matrix $\hat{X}\in\mathbb{R}^{n\times p}$
made of $n$ samples, each of which carries $p$ features or predictors. The
primary goal is to identify the classes to which new unseen samples
belong.
Let us specialize to the case of two classes only, with outputs $y_i=0$ and $y_i=1$. Our outcomes could represent the status of a credit card use who could default or not on her/his debt. That is
Let us specialize to the case of two classes only, with outputs $y_i=0$ and $y_i=1$. Our outcomes could represent the status of a credit card user who could default or not on her/his credit card debt. That is
!bt
\[
y_i = \begin{bmatrix} 0 & \mathrm{no default}\\ 1 & \mathrm{default} \end{bmatrix}.
y_i = \begin{bmatrix} 0 & \mathrm{no}\\ 1 & \mathrm{yes} \end{bmatrix}.
\]
!et
@@ -78,11 +78,11 @@ We would then have our
weighted linear combination, namely
!bt
\begin{equation}
\hat{y} = \hat{X}_i^T\hat{\beta} + \hat{\epsilon},
\hat{y} = \hat{X}^T\hat{\beta} + \hat{\epsilon},
\end{equation}
!et
where $\hat{y}$ is a vector representing the possible outcomes, $\hat{X}$ is our
$n\times p$ design matrix and $\hat{\beta}$ represents our estimators.
$n\times p$ design matrix and $\hat{\beta}$ represents our estimators/predictors.
!split
===== Some selected properties =====
@@ -100,20 +100,15 @@ literature. This model is extremely simple. However, in many cases it is more
favorable to use a ``soft" classifier that outputs
the probability of a given category. This leads us to the logistic function.
The code for plotting the perceptron can be seen here. This si nothing but the standard "Heaviside step function":"https://en.wikipedia.org/wiki/Heaviside_step_function".
!bc pycod
!ec
!split
===== The logistic function =====
A widely used model is the so-called logistic (or sigmoid) function which ranges from $0$ to $1$,
!bt
\[
p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}.
\]
!et
Note that $1-p(t)= p(-t)$.
!split
===== Probabilistic classifiers =====
The perceptron is an example of a ``hard classification'' model. We
will encounter this model when we discuss neural networks as
well. Each datapoint is deterministically assigned to a category (i.e
@@ -123,7 +118,18 @@ than a single value. For example, given $x_i$, the classifier
outputs the probability of being in a category $k$. Logistic regression
is the most common example of a so-called soft classifier. In logistic
regression, the probability that a data point $x_i$
belongs to a category $y_i=\{0,1\}$ is given by the so-called logit function (or Sigmoid)
belongs to a category $y_i=\{0,1\}$ is given by the so-called logit function (or Sigmoid) which is meant to represent the likelihood for a given event,
!bt
\[
p(t) = \frac{1}{1+\mathrm \exp{-t}}=\frac{\exp{t}}{1+\mathrm \exp{t}}.
\]
!et
Note that $1-p(t)= p(-t)$.
The following code plots the logistic function.
!bc pycod
!ec
!split
@@ -141,7 +147,7 @@ where $\hat{\beta}$ are the weights we wish to extract from data, in our case $\
Note that we used
!bt
\[
p(y_i=1) = 1-p(y_i=0).
p(y_i=0\vert x_i, \hat{\beta}) = 1-p(y_i=1\vert x_i, \hat{\beta}).
\]
!et
@@ -150,7 +156,7 @@ p(y_i=1) = 1-p(y_i=0).
In order to define the total likelihood for all possible outcomes from a
dataset $\mathcal{D}=\{(y_i,x_i)\}$, with the binary labels
$y_i\in\{0,1\}$ and where the data points are drawn independently, we use the so-called Maximum Likelihood Estimation (MLE) principle.
$y_i\in\{0,1\}$ and where the data points are drawn independently, we use the so-called "Maximum Likelihood Estimation":"https://en.wikipedia.org/wiki/Maximum_likelihood_estimation" (MLE) principle.
We aim thus at maximizing
the probability of seeing the observed data. We can then approximate the
likelihood in terms of the product of the individual probabilities of a specific outcome $y_i$, that is
@@ -162,7 +168,7 @@ P(\mathcal{D}|\hat{\beta})& = \prod_{i=1}^n \left[p(y_i=1|x_i,\hat{\beta})\right
from which we obtain the log-likelihood and our _cost/loss_ function
!bt
\[
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n y_i\log{p(y_i=1|x_i,\hat{\beta})} + (1-y_i)\log\left[1-p(y_i=1|x_i,\hat{\beta}))\right].
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left( y_i\log{p(y_i=1|x_i,\hat{\beta})} + (1-y_i)\log\left[1-p(y_i=1|x_i,\hat{\beta}))\right]\right).
\]
!et
@@ -172,15 +178,15 @@ from which we obtain the log-likelihood and our _cost/loss_ function
Reordering the logarithms, we can rewrite the _cost/loss_ function as
!bt
\[
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{1+\exp{(\beta_0+\beta_1x_i)}}\right).
\mathcal{C}(\hat{\beta}) = \sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right).
\]
!et
!split
The maximum likelihood estimator is defined as the set of parameters that maximize the log-likelihood where we maximize with respect to $\beta$.
Since the cost (error) function is just the negative log-likelihood, for logistic regression we have that
!bt
\[
\mathcal{C}(\hat{\beta})=-\sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{1+\exp{(\beta_0+\beta_1x_i)}}\right).
\mathcal{C}(\hat{\beta})=-\sum_{i=1}^n \left(y_i(\beta_0+\beta_1x_i) -\log{(1+\exp{(\beta_0+\beta_1x_i)})}\right).
\]
!et
This equation is known in statistics as the _cross entropy_. Finally, we note that just as in linear regression,
@@ -231,6 +237,18 @@ $p(y_i\vert x_i,\hat{\beta})(1-p(y_i\vert x_i,\hat{\beta})$, we can obtain a com
\]
!et
!split
===== Extending to more predictors =====
!split
===== Including more classes =====
!split
===== Optimizing the cost function =====
Newton's method and gradient descent methods
!split
===== A _scikit-learn_ example =====