update on log reg with examples

This commit is contained in:
mhjensen
2018-09-20 11:16:24 +02:00
parent 93390e5dae
commit b6c60e0fdc
20 changed files with 375 additions and 19 deletions
+5 -2
View File
@@ -49,7 +49,8 @@ Automatically generated HTML file from DocOnce source
None,
'___sec4'),
('Maximum likelihood', 2, None, '___sec5'),
('Minimizing the cross entropy', 2, None, '___sec6')]}
('Minimizing the cross entropy', 2, None, '___sec6'),
('A _scikit-learn_ example', 2, None, '___sec7')]}
end of tocinfo -->
<body>
@@ -94,6 +95,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">The cross-entropy as a cost function for logistic regression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec6" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec7" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
</ul>
</li>
@@ -128,7 +130,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 17, 2018</h4></center> <!-- date -->
<center><h4>Sep 20, 2018</h4></center> <!-- date -->
<br>
<p>
<p>
@@ -143,6 +145,7 @@ MathJax.Hub.Config({
<li><a href="._LogReg-bs006.html">7</a></li>
<li><a href="._LogReg-bs007.html">8</a></li>
<li><a href="._LogReg-bs008.html">9</a></li>
<li><a href="._LogReg-bs009.html">10</a></li>
<li><a href="._LogReg-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+4 -1
View File
@@ -49,7 +49,8 @@ Automatically generated HTML file from DocOnce source
None,
'___sec4'),
('Maximum likelihood', 2, None, '___sec5'),
('Minimizing the cross entropy', 2, None, '___sec6')]}
('Minimizing the cross entropy', 2, None, '___sec6'),
('A _scikit-learn_ example', 2, None, '___sec7')]}
end of tocinfo -->
<body>
@@ -94,6 +95,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">The cross-entropy as a cost function for logistic regression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec6" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec7" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
</ul>
</li>
@@ -149,6 +151,7 @@ models</b>, as we will see later.
<li><a href="._LogReg-bs006.html">7</a></li>
<li><a href="._LogReg-bs007.html">8</a></li>
<li><a href="._LogReg-bs008.html">9</a></li>
<li><a href="._LogReg-bs009.html">10</a></li>
<li><a href="._LogReg-bs002.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+4 -1
View File
@@ -49,7 +49,8 @@ Automatically generated HTML file from DocOnce source
None,
'___sec4'),
('Maximum likelihood', 2, None, '___sec5'),
('Minimizing the cross entropy', 2, None, '___sec6')]}
('Minimizing the cross entropy', 2, None, '___sec6'),
('A _scikit-learn_ example', 2, None, '___sec7')]}
end of tocinfo -->
<body>
@@ -94,6 +95,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">The cross-entropy as a cost function for logistic regression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec6" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec7" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
</ul>
</li>
@@ -137,6 +139,7 @@ belong.
<li><a href="._LogReg-bs006.html">7</a></li>
<li><a href="._LogReg-bs007.html">8</a></li>
<li><a href="._LogReg-bs008.html">9</a></li>
<li><a href="._LogReg-bs009.html">10</a></li>
<li><a href="._LogReg-bs003.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+4 -1
View File
@@ -49,7 +49,8 @@ Automatically generated HTML file from DocOnce source
None,
'___sec4'),
('Maximum likelihood', 2, None, '___sec5'),
('Minimizing the cross entropy', 2, None, '___sec6')]}
('Minimizing the cross entropy', 2, None, '___sec6'),
('A _scikit-learn_ example', 2, None, '___sec7')]}
end of tocinfo -->
<body>
@@ -94,6 +95,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">The cross-entropy as a cost function for logistic regression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec6" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec7" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
</ul>
</li>
@@ -137,6 +139,7 @@ where we use the short-hand notation
<li><a href="._LogReg-bs006.html">7</a></li>
<li><a href="._LogReg-bs007.html">8</a></li>
<li><a href="._LogReg-bs008.html">9</a></li>
<li><a href="._LogReg-bs009.html">10</a></li>
<li><a href="._LogReg-bs004.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+4 -1
View File
@@ -49,7 +49,8 @@ Automatically generated HTML file from DocOnce source
None,
'___sec4'),
('Maximum likelihood', 2, None, '___sec5'),
('Minimizing the cross entropy', 2, None, '___sec6')]}
('Minimizing the cross entropy', 2, None, '___sec6'),
('A _scikit-learn_ example', 2, None, '___sec7')]}
end of tocinfo -->
<body>
@@ -94,6 +95,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">The cross-entropy as a cost function for logistic regression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec6" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec7" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
</ul>
</li>
@@ -147,6 +149,7 @@ Note that \( 1-f(s)= f(-s) \), which will be useful shortly.
<li><a href="._LogReg-bs006.html">7</a></li>
<li><a href="._LogReg-bs007.html">8</a></li>
<li><a href="._LogReg-bs008.html">9</a></li>
<li><a href="._LogReg-bs009.html">10</a></li>
<li><a href="._LogReg-bs005.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+4 -1
View File
@@ -49,7 +49,8 @@ Automatically generated HTML file from DocOnce source
None,
'___sec4'),
('Maximum likelihood', 2, None, '___sec5'),
('Minimizing the cross entropy', 2, None, '___sec6')]}
('Minimizing the cross entropy', 2, None, '___sec6'),
('A _scikit-learn_ example', 2, None, '___sec7')]}
end of tocinfo -->
<body>
@@ -94,6 +95,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="#___sec4" style="font-size: 80%;">The cross-entropy as a cost function for logistic regression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec6" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec7" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
</ul>
</li>
@@ -143,6 +145,7 @@ $$
<li><a href="._LogReg-bs006.html">7</a></li>
<li><a href="._LogReg-bs007.html">8</a></li>
<li><a href="._LogReg-bs008.html">9</a></li>
<li><a href="._LogReg-bs009.html">10</a></li>
<li><a href="._LogReg-bs006.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+4 -1
View File
@@ -49,7 +49,8 @@ Automatically generated HTML file from DocOnce source
None,
'___sec4'),
('Maximum likelihood', 2, None, '___sec5'),
('Minimizing the cross entropy', 2, None, '___sec6')]}
('Minimizing the cross entropy', 2, None, '___sec6'),
('A _scikit-learn_ example', 2, None, '___sec7')]}
end of tocinfo -->
<body>
@@ -94,6 +95,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">The cross-entropy as a cost function for logistic regression</a></li>
<!-- navigation toc: --> <li><a href="#___sec5" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec6" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec7" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
</ul>
</li>
@@ -147,6 +149,7 @@ $$
<li class="active"><a href="._LogReg-bs006.html">7</a></li>
<li><a href="._LogReg-bs007.html">8</a></li>
<li><a href="._LogReg-bs008.html">9</a></li>
<li><a href="._LogReg-bs009.html">10</a></li>
<li><a href="._LogReg-bs007.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+4 -1
View File
@@ -49,7 +49,8 @@ Automatically generated HTML file from DocOnce source
None,
'___sec4'),
('Maximum likelihood', 2, None, '___sec5'),
('Minimizing the cross entropy', 2, None, '___sec6')]}
('Minimizing the cross entropy', 2, None, '___sec6'),
('A _scikit-learn_ example', 2, None, '___sec7')]}
end of tocinfo -->
<body>
@@ -94,6 +95,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">The cross-entropy as a cost function for logistic regression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec6" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec7" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
</ul>
</li>
@@ -138,6 +140,7 @@ in practice we usually supplement the cross-entropy with additional regularizati
<li><a href="._LogReg-bs006.html">7</a></li>
<li class="active"><a href="._LogReg-bs007.html">8</a></li>
<li><a href="._LogReg-bs008.html">9</a></li>
<li><a href="._LogReg-bs009.html">10</a></li>
<li><a href="._LogReg-bs008.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+6 -1
View File
@@ -49,7 +49,8 @@ Automatically generated HTML file from DocOnce source
None,
'___sec4'),
('Maximum likelihood', 2, None, '___sec5'),
('Minimizing the cross entropy', 2, None, '___sec6')]}
('Minimizing the cross entropy', 2, None, '___sec6'),
('A _scikit-learn_ example', 2, None, '___sec7')]}
end of tocinfo -->
<body>
@@ -94,6 +95,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">The cross-entropy as a cost function for logistic regression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec7" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
</ul>
</li>
@@ -130,6 +132,7 @@ f(z)[1-f(z)] \). This equation defines a transcendental equation for
be written in a closed form.
Here we need gradient descent methods!
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
@@ -143,6 +146,8 @@ Here we need gradient descent methods!
<li><a href="._LogReg-bs006.html">7</a></li>
<li><a href="._LogReg-bs007.html">8</a></li>
<li class="active"><a href="._LogReg-bs008.html">9</a></li>
<li><a href="._LogReg-bs009.html">10</a></li>
<li><a href="._LogReg-bs009.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+177
View File
@@ -0,0 +1,177 @@
<!--
Automatically generated HTML file from DocOnce source
(https://github.com/hplgit/doconce/)
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
<meta name="description" content="Data Analysis and Machine Learning: Logistic Regression">
<title>Data Analysis and Machine Learning: Logistic Regression</title>
<!-- Bootstrap style: bootstrap -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- not necessary
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
-->
<style type="text/css">
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
.dropdown-menu {
height: auto;
max-height: 400px;
overflow-x: hidden;
}
/* Adds an invisible element before each target to offset for the navigation
bar */
.anchor::before {
content:"";
display:block;
height:50px; /* fixed header height for style bootstrap */
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Logistic Regression', 2, None, '___sec0'),
('Basics', 2, None, '___sec1'),
('Linear classifier', 2, None, '___sec2'),
('Some selected properties', 2, None, '___sec3'),
('The cross-entropy as a cost function for logistic regression',
2,
None,
'___sec4'),
('Maximum likelihood', 2, None, '___sec5'),
('Minimizing the cross entropy', 2, None, '___sec6'),
('A _scikit-learn_ example', 2, None, '___sec7')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "none" },
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="LogReg-bs.html">Data Analysis and Machine Learning: Logistic Regression</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._LogReg-bs001.html#___sec0" style="font-size: 80%;">Logistic Regression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs002.html#___sec1" style="font-size: 80%;">Basics</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs003.html#___sec2" style="font-size: 80%;">Linear classifier</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs004.html#___sec3" style="font-size: 80%;">Some selected properties</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">The cross-entropy as a cost function for logistic regression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec6" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0009"></a>
<!-- !split -->
<h2 id="___sec7" 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">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 -->
<ul class="pagination">
<li><a href="._LogReg-bs008.html">&laquo;</a></li>
<li><a href="._LogReg-bs000.html">1</a></li>
<li><a href="._LogReg-bs001.html">2</a></li>
<li><a href="._LogReg-bs002.html">3</a></li>
<li><a href="._LogReg-bs003.html">4</a></li>
<li><a href="._LogReg-bs004.html">5</a></li>
<li><a href="._LogReg-bs005.html">6</a></li>
<li><a href="._LogReg-bs006.html">7</a></li>
<li><a href="._LogReg-bs007.html">8</a></li>
<li><a href="._LogReg-bs008.html">9</a></li>
<li class="active"><a href="._LogReg-bs009.html">10</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="http://..."><img width="250" align=right src="http://..."></a>
</footer>
-->
<center style="font-size:80%">
<!-- copyright only on the titlepage -->
</center>
</body>
</html>
+5 -2
View File
@@ -49,7 +49,8 @@ Automatically generated HTML file from DocOnce source
None,
'___sec4'),
('Maximum likelihood', 2, None, '___sec5'),
('Minimizing the cross entropy', 2, None, '___sec6')]}
('Minimizing the cross entropy', 2, None, '___sec6'),
('A _scikit-learn_ example', 2, None, '___sec7')]}
end of tocinfo -->
<body>
@@ -94,6 +95,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs005.html#___sec4" style="font-size: 80%;">The cross-entropy as a cost function for logistic regression</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs006.html#___sec5" style="font-size: 80%;">Maximum likelihood</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs008.html#___sec6" style="font-size: 80%;">Minimizing the cross entropy</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs009.html#___sec7" style="font-size: 80%;">A <b>scikit-learn</b> example</a></li>
</ul>
</li>
@@ -128,7 +130,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 17, 2018</h4></center> <!-- date -->
<center><h4>Sep 20, 2018</h4></center> <!-- date -->
<br>
<p>
<p>
@@ -143,6 +145,7 @@ MathJax.Hub.Config({
<li><a href="._LogReg-bs006.html">7</a></li>
<li><a href="._LogReg-bs007.html">8</a></li>
<li><a href="._LogReg-bs008.html">9</a></li>
<li><a href="._LogReg-bs009.html">10</a></li>
<li><a href="._LogReg-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+29 -1
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 17, 2018</h4></center> <!-- date -->
<center><h4>Sep 20, 2018</h4></center> <!-- date -->
<br>
<p>
@@ -356,6 +356,34 @@ Here we need gradient descent methods!
</section>
<section>
<h2 id="___sec7">A <b>scikit-learn</b> example </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn</span> <span style="color: #8B008B; font-weight: bold">import</span> datasets
iris = datasets.load_iris()
<span style="color: #658b00">list</span>(iris.keys())
[<span style="color: #CD5555">&#39;data&#39;</span>, <span style="color: #CD5555">&#39;target_names&#39;</span>, <span style="color: #CD5555">&#39;feature_names&#39;</span>, <span style="color: #CD5555">&#39;target&#39;</span>, <span style="color: #CD5555">&#39;DESCR&#39;</span>]
X = iris[<span style="color: #CD5555">&quot;data&quot;</span>][:, <span style="color: #B452CD">3</span>:] <span style="color: #228B22"># petal width</span>
y = (iris[<span style="color: #CD5555">&quot;target&quot;</span>] == <span style="color: #B452CD">2</span>).astype(np.int) <span style="color: #228B22"># 1 if Iris-Virginica, else 0</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LogisticRegression
log_reg = LogisticRegression()
log_reg.fit(X, y)
X_new = np.linspace(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">3</span>, <span style="color: #B452CD">1000</span>).reshape(-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>)
y_proba = log_reg.predict_proba(X_new)
plt.plot(X_new, y_proba[:, <span style="color: #B452CD">1</span>], <span style="color: #CD5555">&quot;g-&quot;</span>, label=<span style="color: #CD5555">&quot;Iris-Virginica&quot;</span>)
plt.plot(X_new, y_proba[:, <span style="color: #B452CD">0</span>], <span style="color: #CD5555">&quot;b--&quot;</span>, label=<span style="color: #CD5555">&quot;Not Iris-Virginica&quot;</span>)
plt.show()
</pre></div>
</section>
</div> <!-- class="slides" -->
</div> <!-- class="reveal" -->
+32 -2
View File
@@ -43,7 +43,8 @@ div { text-align: justify; text-justify: inter-word; }
None,
'___sec4'),
('Maximum likelihood', 2, None, '___sec5'),
('Minimizing the cross entropy', 2, None, '___sec6')]}
('Minimizing the cross entropy', 2, None, '___sec6'),
('A _scikit-learn_ example', 2, None, '___sec7')]}
end of tocinfo -->
<body>
@@ -85,7 +86,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 17, 2018</h4></center> <!-- date -->
<center><h4>Sep 20, 2018</h4></center> <!-- date -->
<br>
<p>
<!-- !split -->
@@ -266,6 +267,35 @@ f(z)[1-f(z)] \). This equation defines a transcendental equation for
be written in a closed form.
Here we need gradient descent methods!
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec7">A <b>scikit-learn</b> example </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn</span> <span style="color: #8B008B; font-weight: bold">import</span> datasets
iris = datasets.load_iris()
<span style="color: #658b00">list</span>(iris.keys())
[<span style="color: #CD5555">&#39;data&#39;</span>, <span style="color: #CD5555">&#39;target_names&#39;</span>, <span style="color: #CD5555">&#39;feature_names&#39;</span>, <span style="color: #CD5555">&#39;target&#39;</span>, <span style="color: #CD5555">&#39;DESCR&#39;</span>]
X = iris[<span style="color: #CD5555">&quot;data&quot;</span>][:, <span style="color: #B452CD">3</span>:] <span style="color: #228B22"># petal width</span>
y = (iris[<span style="color: #CD5555">&quot;target&quot;</span>] == <span style="color: #B452CD">2</span>).astype(np.int) <span style="color: #228B22"># 1 if Iris-Virginica, else 0</span>
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LogisticRegression
log_reg = LogisticRegression()
log_reg.fit(X, y)
X_new = np.linspace(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">3</span>, <span style="color: #B452CD">1000</span>).reshape(-<span style="color: #B452CD">1</span>, <span style="color: #B452CD">1</span>)
y_proba = log_reg.predict_proba(X_new)
plt.plot(X_new, y_proba[:, <span style="color: #B452CD">1</span>], <span style="color: #CD5555">&quot;g-&quot;</span>, label=<span style="color: #CD5555">&quot;Iris-Virginica&quot;</span>)
plt.plot(X_new, y_proba[:, <span style="color: #B452CD">0</span>], <span style="color: #CD5555">&quot;b--&quot;</span>, label=<span style="color: #CD5555">&quot;Not Iris-Virginica&quot;</span>)
plt.show()
</pre></div>
<p>
<!-- ------------------- end of main content --------------- -->
+32 -2
View File
@@ -48,7 +48,8 @@ div { text-align: justify; text-justify: inter-word; }
None,
'___sec4'),
('Maximum likelihood', 2, None, '___sec5'),
('Minimizing the cross entropy', 2, None, '___sec6')]}
('Minimizing the cross entropy', 2, None, '___sec6'),
('A _scikit-learn_ example', 2, None, '___sec7')]}
end of tocinfo -->
<body>
@@ -90,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 17, 2018</h4></center> <!-- date -->
<center><h4>Sep 20, 2018</h4></center> <!-- date -->
<br>
<p>
<!-- !split -->
@@ -271,6 +272,35 @@ f(z)[1-f(z)] \). This equation defines a transcendental equation for
be written in a closed form.
Here we need gradient descent methods!
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec7">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">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>
<!-- ------------------- end of main content --------------- -->
+35 -2
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 17, 2018**\n",
"Date: **Sep 20, 2018**\n",
"\n",
"Copyright 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
"\n",
@@ -285,7 +285,40 @@
"f(z)[1-f(z)]$. This equation defines a transcendental equation for\n",
"$\\mathbf{w}$, the solution of which, unlike linear regression, cannot\n",
"be written in a closed form. \n",
"Here we need gradient descent methods!"
"Here we need gradient descent methods!\n",
"\n",
"\n",
"## A **scikit-learn** example"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn import datasets\n",
"iris = datasets.load_iris()\n",
"list(iris.keys())\n",
"['data', 'target_names', 'feature_names', 'target', 'DESCR']\n",
"X = iris[\"data\"][:, 3:] # petal width\n",
"y = (iris[\"target\"] == 2).astype(np.int) # 1 if Iris-Virginica, else 0\n",
"\n",
"from sklearn.linear_model import LogisticRegression\n",
"log_reg = LogisticRegression()\n",
"log_reg.fit(X, y)\n",
"\n",
"X_new = np.linspace(0, 3, 1000).reshape(-1, 1)\n",
"y_proba = log_reg.predict_proba(X_new)\n",
"plt.plot(X_new, y_proba[:, 1], \"g-\", label=\"Iris-Virginica\")\n",
"plt.plot(X_new, y_proba[:, 0], \"b--\", label=\"Not Iris-Virginica\")\n",
"plt.show()"
]
}
],
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+26
View File
@@ -151,3 +151,29 @@ f(z)[1-f(z)]$. This equation defines a transcendental equation for
$\mathbf{w}$, the solution of which, unlike linear regression, cannot
be written in a closed form.
Here we need gradient descent methods!
!split
===== A _scikit-learn_ example =====
!bc pycod
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
iris = datasets.load_iris()
list(iris.keys())
['data', 'target_names', 'feature_names', 'target', 'DESCR']
X = iris["data"][:, 3:] # petal width
y = (iris["target"] == 2).astype(np.int) # 1 if Iris-Virginica, else 0
from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression()
log_reg.fit(X, y)
X_new = np.linspace(0, 3, 1000).reshape(-1, 1)
y_proba = log_reg.predict_proba(X_new)
plt.plot(X_new, y_proba[:, 1], "g-", label="Iris-Virginica")
plt.plot(X_new, y_proba[:, 0], "b--", label="Not Iris-Virginica")
plt.show()
!ec