update on credit card data

This commit is contained in:
mhjensen
2019-10-17 14:16:25 +02:00
parent 20b036b0b9
commit 22b05b3422
27 changed files with 243 additions and 828 deletions
+3 -3
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
@@ -152,7 +152,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Oct 9, 2019</h4></center> <!-- date -->
<center><h4>Oct 17, 2019</h4></center> <!-- date -->
<br>
<p>
<p>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+2 -2
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
+10 -202
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
@@ -135,210 +135,18 @@ MathJax.Hub.Config({
<h2 id="___sec16" class="anchor">The Credit Card example </h2>
Here we use the the <a href="https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients" target="_self">credit card data</a>.
The data are from an extensive database from Taiwan and include more than ten predictors. More text and clean up of code will be added.
The data are from an extensive database from Taiwan and include more than ten predictors.
<p>
For categorical data -Scikit-Learn- provides a so-called <b>one-hot encoder</b>.
This is called one-hot
encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold).
<b>Scikit-Learn</b> provides a OneHotEncoder encoder to convert integer categorical values into one-hot
<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">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">os</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> OneHotEncoder
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.compose</span> <span style="color: #008000; font-weight: bold">import</span> ColumnTransformer
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler, OneHotEncoder
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> confusion_matrix, accuracy_score, roc_auc_score
<span style="color: #408080; font-style: italic"># Trying to set the seed</span>
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">0</span>)
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">random</span>
random<span style="color: #666666">.</span>seed(<span style="color: #666666">0</span>)
<span style="color: #408080; font-style: italic"># Reading file into data frame</span>
cwd <span style="color: #666666">=</span> os<span style="color: #666666">.</span>getcwd()
filename <span style="color: #666666">=</span> cwd <span style="color: #666666">+</span> <span style="color: #BA2121">&#39;/default of credit card clients.xls&#39;</span>
nanDict <span style="color: #666666">=</span> {}
df <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>read_excel(filename, header<span style="color: #666666">=1</span>, skiprows<span style="color: #666666">=0</span>, index_col<span style="color: #666666">=0</span>, na_values<span style="color: #666666">=</span>nanDict)
df<span style="color: #666666">.</span>rename(index<span style="color: #666666">=</span><span style="color: #008000">str</span>, columns<span style="color: #666666">=</span>{<span style="color: #BA2121">&quot;default payment next month&quot;</span>: <span style="color: #BA2121">&quot;defaultPaymentNextMonth&quot;</span>}, inplace<span style="color: #666666">=</span><span style="color: #008000">True</span>)
<span style="color: #408080; font-style: italic"># Features and targets </span>
X <span style="color: #666666">=</span> df<span style="color: #666666">.</span>loc[:, df<span style="color: #666666">.</span>columns <span style="color: #666666">!=</span> <span style="color: #BA2121">&#39;defaultPaymentNextMonth&#39;</span>]<span style="color: #666666">.</span>values
y <span style="color: #666666">=</span> df<span style="color: #666666">.</span>loc[:, df<span style="color: #666666">.</span>columns <span style="color: #666666">==</span> <span style="color: #BA2121">&#39;defaultPaymentNextMonth&#39;</span>]<span style="color: #666666">.</span>values
<span style="color: #408080; font-style: italic"># Categorical variables to one-hot&#39;s</span>
onehotencoder <span style="color: #666666">=</span> OneHotEncoder(categories<span style="color: #666666">=</span><span style="color: #BA2121">&quot;auto&quot;</span>)
X <span style="color: #666666">=</span> ColumnTransformer(
[(<span style="color: #BA2121">&quot;&quot;</span>, onehotencoder, [<span style="color: #666666">3</span>]),],
remainder<span style="color: #666666">=</span><span style="color: #BA2121">&quot;passthrough&quot;</span>
)<span style="color: #666666">.</span>fit_transform(X)
y<span style="color: #666666">.</span>shape
<span style="color: #408080; font-style: italic"># Train-test split</span>
trainingShare <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>
seed <span style="color: #666666">=</span> <span style="color: #666666">1</span>
XTrain, XTest, yTrain, yTest<span style="color: #666666">=</span>train_test_split(X, y, train_size<span style="color: #666666">=</span>trainingShare, \
test_size <span style="color: #666666">=</span> <span style="color: #666666">1-</span>trainingShare,
random_state<span style="color: #666666">=</span>seed)
<span style="color: #408080; font-style: italic"># Input Scaling</span>
sc <span style="color: #666666">=</span> StandardScaler()
XTrain <span style="color: #666666">=</span> sc<span style="color: #666666">.</span>fit_transform(XTrain)
XTest <span style="color: #666666">=</span> sc<span style="color: #666666">.</span>transform(XTest)
<span style="color: #408080; font-style: italic"># One-hot&#39;s of the target vector</span>
Y_train_onehot, Y_test_onehot <span style="color: #666666">=</span> onehotencoder<span style="color: #666666">.</span>fit_transform(yTrain), onehotencoder<span style="color: #666666">.</span>fit_transform(yTest)
<span style="color: #408080; font-style: italic"># Remove instances with zeros only for past bill statements or paid amounts</span>
<span style="color: #BA2121; font-style: italic">&#39;&#39;&#39;</span>
<span style="color: #BA2121; font-style: italic">df = df.drop(df[(df.BILL_AMT1 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.BILL_AMT2 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.BILL_AMT3 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.BILL_AMT4 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.BILL_AMT5 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.BILL_AMT6 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.PAY_AMT1 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.PAY_AMT2 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.PAY_AMT3 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.PAY_AMT4 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.PAY_AMT5 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.PAY_AMT6 == 0)].index)</span>
<span style="color: #BA2121; font-style: italic">&#39;&#39;&#39;</span>
df <span style="color: #666666">=</span> df<span style="color: #666666">.</span>drop(df[(df<span style="color: #666666">.</span>BILL_AMT1 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>BILL_AMT2 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>BILL_AMT3 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>BILL_AMT4 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>BILL_AMT5 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>BILL_AMT6 <span style="color: #666666">==</span> <span style="color: #666666">0</span>)]<span style="color: #666666">.</span>index)
df <span style="color: #666666">=</span> df<span style="color: #666666">.</span>drop(df[(df<span style="color: #666666">.</span>PAY_AMT1 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>PAY_AMT2 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>PAY_AMT3 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>PAY_AMT4 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>PAY_AMT5 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>PAY_AMT6 <span style="color: #666666">==</span> <span style="color: #666666">0</span>)]<span style="color: #666666">.</span>index)
<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
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> GridSearchCV
lambdas<span style="color: #666666">=</span>np<span style="color: #666666">.</span>logspace(<span style="color: #666666">-5</span>,<span style="color: #666666">7</span>,<span style="color: #666666">13</span>)
parameters <span style="color: #666666">=</span> [{<span style="color: #BA2121">&#39;C&#39;</span>: <span style="color: #666666">1./</span>lambdas, <span style="color: #BA2121">&quot;solver&quot;</span>:[<span style="color: #BA2121">&quot;lbfgs&quot;</span>]}]<span style="color: #408080; font-style: italic">#*len(parameters)}]</span>
scoring <span style="color: #666666">=</span> [<span style="color: #BA2121">&#39;accuracy&#39;</span>, <span style="color: #BA2121">&#39;roc_auc&#39;</span>]
logReg <span style="color: #666666">=</span> LogisticRegression()
gridSearch <span style="color: #666666">=</span> GridSearchCV(logReg, parameters, cv<span style="color: #666666">=5</span>, scoring<span style="color: #666666">=</span>scoring, refit<span style="color: #666666">=</span><span style="color: #BA2121">&#39;roc_auc&#39;</span>)
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># &quot;refit&quot; gives the metric used deciding best model. </span>
<span style="color: #408080; font-style: italic"># See more http://scikit-learn.org/stable/auto_examples/model_selection/plot_multi_metric_evaluation.html</span>
gridSearch<span style="color: #666666">.</span>fit(XTrain, yTrain<span style="color: #666666">.</span>ravel())
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">gridSearchSummary</span>(method, scoring):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;Prints best parameters from Grid search</span>
<span style="color: #BA2121; font-style: italic"> and AUC with standard deviation for all </span>
<span style="color: #BA2121; font-style: italic"> parameter combos &quot;&quot;&quot;</span>
method <span style="color: #666666">=</span> <span style="color: #008000">eval</span>(method)
<span style="color: #008000; font-weight: bold">if</span> scoring <span style="color: #666666">==</span> <span style="color: #BA2121">&#39;accuracy&#39;</span>:
mean <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;mean_test_score&#39;</span>
sd <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;std_test_score&#39;</span>
<span style="color: #008000; font-weight: bold">elif</span> scoring <span style="color: #666666">==</span> <span style="color: #BA2121">&#39;auc&#39;</span>:
mean <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;mean_test_roc_auc&#39;</span>
sd <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;std_test_roc_auc&#39;</span>
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Best: </span><span style="color: #BB6688; font-weight: bold">%f</span><span style="color: #BA2121"> using </span><span style="color: #BB6688; font-weight: bold">%s</span><span style="color: #BA2121">&quot;</span> <span style="color: #666666">%</span> (method<span style="color: #666666">.</span>best_score_, method<span style="color: #666666">.</span>best_params_))
means <span style="color: #666666">=</span> method<span style="color: #666666">.</span>cv_results_[mean]
stds <span style="color: #666666">=</span> method<span style="color: #666666">.</span>cv_results_[sd]
params <span style="color: #666666">=</span> method<span style="color: #666666">.</span>cv_results_[<span style="color: #BA2121">&#39;params&#39;</span>]
<span style="color: #008000; font-weight: bold">for</span> mean, stdev, param <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">zip</span>(means, stds, params):
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;</span><span style="color: #BB6688; font-weight: bold">%f</span><span style="color: #BA2121"> (</span><span style="color: #BB6688; font-weight: bold">%f</span><span style="color: #BA2121">) with: </span><span style="color: #BB6688; font-weight: bold">%r</span><span style="color: #BA2121">&quot;</span> <span style="color: #666666">%</span> (mean, stdev, param))
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">createConfusionMatrix</span>(method, printOut<span style="color: #666666">=</span><span style="color: #008000">True</span>):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #BA2121; font-style: italic"> Computes and prints confusion matrices, accuracy scores,</span>
<span style="color: #BA2121; font-style: italic"> and AUC for test and training sets </span>
<span style="color: #BA2121; font-style: italic"> &quot;&quot;&quot;</span>
confusionArray <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(<span style="color: #666666">6</span>, dtype<span style="color: #666666">=</span><span style="color: #008000">object</span>)
method <span style="color: #666666">=</span> <span style="color: #008000">eval</span>(method)
<span style="color: #408080; font-style: italic"># Train</span>
yPredTrain <span style="color: #666666">=</span> method<span style="color: #666666">.</span>predict(XTrain)
yPredTrain <span style="color: #666666">=</span> (yPredTrain <span style="color: #666666">&gt;</span> <span style="color: #666666">0.5</span>)
cm <span style="color: #666666">=</span> confusion_matrix(
yTrain, yPredTrain)
cm <span style="color: #666666">=</span> np<span style="color: #666666">.</span>around(cm<span style="color: #666666">/</span>cm<span style="color: #666666">.</span>sum(axis<span style="color: #666666">=1</span>)[:,<span style="color: #008000">None</span>], <span style="color: #666666">2</span>)
confusionArray[<span style="color: #666666">0</span>] <span style="color: #666666">=</span> cm
accScore <span style="color: #666666">=</span> accuracy_score(yTrain, yPredTrain)
confusionArray[<span style="color: #666666">1</span>] <span style="color: #666666">=</span> accScore
AUC <span style="color: #666666">=</span> roc_auc_score(yTrain, yPredTrain)
confusionArray[<span style="color: #666666">2</span>] <span style="color: #666666">=</span> AUC
<span style="color: #008000; font-weight: bold">if</span> printOut:
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">################### Training ###############&#39;</span>)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">Training Confusion matrix: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">&#39;</span>, cm)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">Training Accuracy score: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">&#39;</span>, accScore)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">Train AUC: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">&#39;</span>, AUC)
<span style="color: #408080; font-style: italic"># Test</span>
yPred <span style="color: #666666">=</span> method<span style="color: #666666">.</span>predict(XTest)
yPred <span style="color: #666666">=</span> (yPred <span style="color: #666666">&gt;</span> <span style="color: #666666">0.5</span>)
cm <span style="color: #666666">=</span> confusion_matrix(
yTest, yPred)
cm <span style="color: #666666">=</span> np<span style="color: #666666">.</span>around(cm<span style="color: #666666">/</span>cm<span style="color: #666666">.</span>sum(axis<span style="color: #666666">=1</span>)[:,<span style="color: #008000">None</span>], <span style="color: #666666">2</span>)
confusionArray[<span style="color: #666666">3</span>] <span style="color: #666666">=</span> cm
accScore <span style="color: #666666">=</span> accuracy_score(yTest, yPred)
confusionArray[<span style="color: #666666">4</span>] <span style="color: #666666">=</span> accScore
AUC <span style="color: #666666">=</span> roc_auc_score(yTest, yPred)
confusionArray[<span style="color: #666666">5</span>] <span style="color: #666666">=</span> AUC
<span style="color: #008000; font-weight: bold">if</span> printOut:
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">################### Testing ###############&#39;</span>)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">Test Confusion matrix: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">&#39;</span>, cm)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">Test Accuracy score: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">&#39;</span>, accScore)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">TestAUC: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">&#39;</span>, AUC)
<span style="color: #008000; font-weight: bold">return</span> confusionArray
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">seaborn</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</span>
seaborn<span style="color: #666666">.</span>set(style<span style="color: #666666">=</span><span style="color: #BA2121">&quot;white&quot;</span>, context<span style="color: #666666">=</span><span style="color: #BA2121">&quot;notebook&quot;</span>, font_scale<span style="color: #666666">=1.5</span>,
rc<span style="color: #666666">=</span>{<span style="color: #BA2121">&quot;axes.grid&quot;</span>: <span style="color: #008000">True</span>, <span style="color: #BA2121">&quot;legend.frameon&quot;</span>: <span style="color: #008000">False</span>,
<span style="color: #BA2121">&quot;lines.markeredgewidth&quot;</span>: <span style="color: #666666">1.4</span>, <span style="color: #BA2121">&quot;lines.markersize&quot;</span>: <span style="color: #666666">10</span>})
seaborn<span style="color: #666666">.</span>set_context(<span style="color: #BA2121">&quot;notebook&quot;</span>, font_scale<span style="color: #666666">=1.5</span>, rc<span style="color: #666666">=</span>{<span style="color: #BA2121">&quot;lines.linewidth&quot;</span>: <span style="color: #666666">4.5</span>})
yPred <span style="color: #666666">=</span> gridSearch<span style="color: #666666">.</span>predict_proba(XTest)
<span style="color: #008000; font-weight: bold">print</span>(yTest<span style="color: #666666">.</span>ravel()<span style="color: #666666">.</span>shape, yPred<span style="color: #666666">.</span>shape)
<span style="color: #408080; font-style: italic">#skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred_onehot)</span>
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_cumulative_gain(yTest<span style="color: #666666">.</span>ravel(), yPred)
defaults <span style="color: #666666">=</span> <span style="color: #008000">sum</span>(yTest <span style="color: #666666">==</span> <span style="color: #666666">1</span>)
total <span style="color: #666666">=</span> <span style="color: #008000">len</span>(yTest)
defaultRate <span style="color: #666666">=</span> defaults<span style="color: #666666">/</span>total
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">bestCurve</span>(defaults, total, defaultRate):
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, total)
y1 <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, defaults)
y2 <span style="color: #666666">=</span> np<span style="color: #666666">.</span>ones(total<span style="color: #666666">-</span>defaults)
y3 <span style="color: #666666">=</span> np<span style="color: #666666">.</span>concatenate([y1,y2])
<span style="color: #008000; font-weight: bold">return</span> x, y3
x, best <span style="color: #666666">=</span> bestCurve(defaults<span style="color: #666666">=</span>defaults, total<span style="color: #666666">=</span>total, defaultRate<span style="color: #666666">=</span>defaultRate)
plt<span style="color: #666666">.</span>plot(x, best)
plt<span style="color: #666666">.</span>show()
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> OneHotEncoder
encoder <span style="color: #666666">=</span> OneHotEncoder()
</pre></div>
<p>
<p>
+99 -3
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
@@ -133,7 +133,103 @@ MathJax.Hub.Config({
<a name="part0018"></a>
<!-- !split -->
<h2 id="___sec17" class="anchor">Analysis of the credi card example </h2>
<h2 id="___sec17" class="anchor">How to read the Credit Card data </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">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">os</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> OneHotEncoder
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.compose</span> <span style="color: #008000; font-weight: bold">import</span> ColumnTransformer
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler, OneHotEncoder
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> confusion_matrix, accuracy_score, roc_auc_score
<span style="color: #408080; font-style: italic"># Trying to set the seed</span>
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">0</span>)
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">random</span>
random<span style="color: #666666">.</span>seed(<span style="color: #666666">0</span>)
<span style="color: #408080; font-style: italic"># Reading file into data frame</span>
cwd <span style="color: #666666">=</span> os<span style="color: #666666">.</span>getcwd()
filename <span style="color: #666666">=</span> cwd <span style="color: #666666">+</span> <span style="color: #BA2121">&#39;/default of credit card clients.xls&#39;</span>
nanDict <span style="color: #666666">=</span> {}
df <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>read_excel(filename, header<span style="color: #666666">=1</span>, skiprows<span style="color: #666666">=0</span>, index_col<span style="color: #666666">=0</span>, na_values<span style="color: #666666">=</span>nanDict)
df<span style="color: #666666">.</span>rename(index<span style="color: #666666">=</span><span style="color: #008000">str</span>, columns<span style="color: #666666">=</span>{<span style="color: #BA2121">&quot;default payment next month&quot;</span>: <span style="color: #BA2121">&quot;defaultPaymentNextMonth&quot;</span>}, inplace<span style="color: #666666">=</span><span style="color: #008000">True</span>)
<span style="color: #408080; font-style: italic"># Features and targets </span>
X <span style="color: #666666">=</span> df<span style="color: #666666">.</span>loc[:, df<span style="color: #666666">.</span>columns <span style="color: #666666">!=</span> <span style="color: #BA2121">&#39;defaultPaymentNextMonth&#39;</span>]<span style="color: #666666">.</span>values
y <span style="color: #666666">=</span> df<span style="color: #666666">.</span>loc[:, df<span style="color: #666666">.</span>columns <span style="color: #666666">==</span> <span style="color: #BA2121">&#39;defaultPaymentNextMonth&#39;</span>]<span style="color: #666666">.</span>values
<span style="color: #408080; font-style: italic"># Categorical variables to one-hot&#39;s</span>
onehotencoder <span style="color: #666666">=</span> OneHotEncoder(categories<span style="color: #666666">=</span><span style="color: #BA2121">&quot;auto&quot;</span>)
X <span style="color: #666666">=</span> ColumnTransformer(
[(<span style="color: #BA2121">&quot;&quot;</span>, onehotencoder, [<span style="color: #666666">3</span>]),],
remainder<span style="color: #666666">=</span><span style="color: #BA2121">&quot;passthrough&quot;</span>
)<span style="color: #666666">.</span>fit_transform(X)
y<span style="color: #666666">.</span>shape
<span style="color: #408080; font-style: italic"># Train-test split</span>
trainingShare <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>
seed <span style="color: #666666">=</span> <span style="color: #666666">1</span>
XTrain, XTest, yTrain, yTest<span style="color: #666666">=</span>train_test_split(X, y, train_size<span style="color: #666666">=</span>trainingShare, \
test_size <span style="color: #666666">=</span> <span style="color: #666666">1-</span>trainingShare,
random_state<span style="color: #666666">=</span>seed)
<span style="color: #408080; font-style: italic"># Input Scaling</span>
sc <span style="color: #666666">=</span> StandardScaler()
XTrain <span style="color: #666666">=</span> sc<span style="color: #666666">.</span>fit_transform(XTrain)
XTest <span style="color: #666666">=</span> sc<span style="color: #666666">.</span>transform(XTest)
<span style="color: #408080; font-style: italic"># One-hot&#39;s of the target vector</span>
Y_train_onehot, Y_test_onehot <span style="color: #666666">=</span> onehotencoder<span style="color: #666666">.</span>fit_transform(yTrain), onehotencoder<span style="color: #666666">.</span>fit_transform(yTest)
<span style="color: #408080; font-style: italic"># Remove instances with zeros only for past bill statements or paid amounts</span>
<span style="color: #BA2121; font-style: italic">&#39;&#39;&#39;</span>
<span style="color: #BA2121; font-style: italic">df = df.drop(df[(df.BILL_AMT1 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.BILL_AMT2 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.BILL_AMT3 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.BILL_AMT4 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.BILL_AMT5 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.BILL_AMT6 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.PAY_AMT1 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.PAY_AMT2 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.PAY_AMT3 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.PAY_AMT4 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.PAY_AMT5 == 0) &amp;</span>
<span style="color: #BA2121; font-style: italic"> (df.PAY_AMT6 == 0)].index)</span>
<span style="color: #BA2121; font-style: italic">&#39;&#39;&#39;</span>
df <span style="color: #666666">=</span> df<span style="color: #666666">.</span>drop(df[(df<span style="color: #666666">.</span>BILL_AMT1 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>BILL_AMT2 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>BILL_AMT3 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>BILL_AMT4 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>BILL_AMT5 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>BILL_AMT6 <span style="color: #666666">==</span> <span style="color: #666666">0</span>)]<span style="color: #666666">.</span>index)
df <span style="color: #666666">=</span> df<span style="color: #666666">.</span>drop(df[(df<span style="color: #666666">.</span>PAY_AMT1 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>PAY_AMT2 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>PAY_AMT3 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>PAY_AMT4 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>PAY_AMT5 <span style="color: #666666">==</span> <span style="color: #666666">0</span>) <span style="color: #666666">&amp;</span>
(df<span style="color: #666666">.</span>PAY_AMT6 <span style="color: #666666">==</span> <span style="color: #666666">0</span>)]<span style="color: #666666">.</span>index)
<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
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> GridSearchCV
lambdas<span style="color: #666666">=</span>np<span style="color: #666666">.</span>logspace(<span style="color: #666666">-5</span>,<span style="color: #666666">7</span>,<span style="color: #666666">13</span>)
parameters <span style="color: #666666">=</span> [{<span style="color: #BA2121">&#39;C&#39;</span>: <span style="color: #666666">1./</span>lambdas, <span style="color: #BA2121">&quot;solver&quot;</span>:[<span style="color: #BA2121">&quot;lbfgs&quot;</span>]}]<span style="color: #408080; font-style: italic">#*len(parameters)}]</span>
scoring <span style="color: #666666">=</span> [<span style="color: #BA2121">&#39;accuracy&#39;</span>, <span style="color: #BA2121">&#39;roc_auc&#39;</span>]
logReg <span style="color: #666666">=</span> LogisticRegression()
gridSearch <span style="color: #666666">=</span> GridSearchCV(logReg, parameters, cv<span style="color: #666666">=5</span>, scoring<span style="color: #666666">=</span>scoring, refit<span style="color: #666666">=</span><span style="color: #BA2121">&#39;roc_auc&#39;</span>)
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
+3 -3
View File
@@ -62,7 +62,7 @@ Automatically generated HTML file from DocOnce source
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -117,7 +117,7 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._LogReg-bs015.html#___sec14" style="font-size: 80%;">More classes</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs016.html#___sec15" style="font-size: 80%;">A simple classification problem</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs017.html#___sec16" style="font-size: 80%;">The Credit Card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">Analysis of the credi card example</a></li>
<!-- navigation toc: --> <li><a href="._LogReg-bs018.html#___sec17" style="font-size: 80%;">How to read the Credit Card data</a></li>
</ul>
</li>
@@ -152,7 +152,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Oct 9, 2019</h4></center> <!-- date -->
<center><h4>Oct 17, 2019</h4></center> <!-- date -->
<br>
<p>
<p>
+19 -116
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>Oct 9, 2019</h4></center> <!-- date -->
<center><h4>Oct 17, 2019</h4></center> <!-- date -->
<br>
<p>
@@ -668,7 +668,24 @@ methods</a>.
<section>
<h2 id="___sec16">The Credit Card example </h2>
Here we use the the <a href="https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients" target="_blank">credit card data</a>.
The data are from an extensive database from Taiwan and include more than ten predictors. More text and clean up of code will be added.
The data are from an extensive database from Taiwan and include more than ten predictors.
<p>
For categorical data -Scikit-Learn- provides a so-called <b>one-hot encoder</b>.
This is called one-hot
encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold).
<b>Scikit-Learn</b> provides a OneHotEncoder encoder to convert integer categorical values into one-hot
<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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> OneHotEncoder
encoder = OneHotEncoder()
</pre></div>
</section>
<section>
<h2 id="___sec17">How to read the Credit Card data </h2>
<p>
@@ -764,120 +781,6 @@ scoring = [<span style="color: #CD5555">&#39;accuracy&#39;</span>, <span style="
logReg = LogisticRegression()
gridSearch = GridSearchCV(logReg, parameters, cv=<span style="color: #B452CD">5</span>, scoring=scoring, refit=<span style="color: #CD5555">&#39;roc_auc&#39;</span>)
</pre></div>
<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: #228B22"># &quot;refit&quot; gives the metric used deciding best model. </span>
<span style="color: #228B22"># See more http://scikit-learn.org/stable/auto_examples/model_selection/plot_multi_metric_evaluation.html</span>
gridSearch.fit(XTrain, yTrain.ravel())
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">gridSearchSummary</span>(method, scoring):
<span style="color: #CD5555">&quot;&quot;&quot;Prints best parameters from Grid search</span>
<span style="color: #CD5555"> and AUC with standard deviation for all </span>
<span style="color: #CD5555"> parameter combos &quot;&quot;&quot;</span>
method = <span style="color: #658b00">eval</span>(method)
<span style="color: #8B008B; font-weight: bold">if</span> scoring == <span style="color: #CD5555">&#39;accuracy&#39;</span>:
mean = <span style="color: #CD5555">&#39;mean_test_score&#39;</span>
sd = <span style="color: #CD5555">&#39;std_test_score&#39;</span>
<span style="color: #8B008B; font-weight: bold">elif</span> scoring == <span style="color: #CD5555">&#39;auc&#39;</span>:
mean = <span style="color: #CD5555">&#39;mean_test_roc_auc&#39;</span>
sd = <span style="color: #CD5555">&#39;std_test_roc_auc&#39;</span>
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Best: %f using %s&quot;</span> % (method.best_score_, method.best_params_))
means = method.cv_results_[mean]
stds = method.cv_results_[sd]
params = method.cv_results_[<span style="color: #CD5555">&#39;params&#39;</span>]
<span style="color: #8B008B; font-weight: bold">for</span> mean, stdev, param <span style="color: #8B008B">in</span> <span style="color: #658b00">zip</span>(means, stds, params):
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;%f (%f) with: %r&quot;</span> % (mean, stdev, param))
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">createConfusionMatrix</span>(method, printOut=<span style="color: #658b00">True</span>):
<span style="color: #CD5555">&quot;&quot;&quot;</span>
<span style="color: #CD5555"> Computes and prints confusion matrices, accuracy scores,</span>
<span style="color: #CD5555"> and AUC for test and training sets </span>
<span style="color: #CD5555"> &quot;&quot;&quot;</span>
confusionArray = np.zeros(<span style="color: #B452CD">6</span>, dtype=<span style="color: #658b00">object</span>)
method = <span style="color: #658b00">eval</span>(method)
<span style="color: #228B22"># Train</span>
yPredTrain = method.predict(XTrain)
yPredTrain = (yPredTrain &gt; <span style="color: #B452CD">0.5</span>)
cm = confusion_matrix(
yTrain, yPredTrain)
cm = np.around(cm/cm.sum(axis=<span style="color: #B452CD">1</span>)[:,<span style="color: #658b00">None</span>], <span style="color: #B452CD">2</span>)
confusionArray[<span style="color: #B452CD">0</span>] = cm
accScore = accuracy_score(yTrain, yPredTrain)
confusionArray[<span style="color: #B452CD">1</span>] = accScore
AUC = roc_auc_score(yTrain, yPredTrain)
confusionArray[<span style="color: #B452CD">2</span>] = AUC
<span style="color: #8B008B; font-weight: bold">if</span> printOut:
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\n################### Training ###############&#39;</span>)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\nTraining Confusion matrix: \n&#39;</span>, cm)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\nTraining Accuracy score: \n&#39;</span>, accScore)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\nTrain AUC: \n&#39;</span>, AUC)
<span style="color: #228B22"># Test</span>
yPred = method.predict(XTest)
yPred = (yPred &gt; <span style="color: #B452CD">0.5</span>)
cm = confusion_matrix(
yTest, yPred)
cm = np.around(cm/cm.sum(axis=<span style="color: #B452CD">1</span>)[:,<span style="color: #658b00">None</span>], <span style="color: #B452CD">2</span>)
confusionArray[<span style="color: #B452CD">3</span>] = cm
accScore = accuracy_score(yTest, yPred)
confusionArray[<span style="color: #B452CD">4</span>] = accScore
AUC = roc_auc_score(yTest, yPred)
confusionArray[<span style="color: #B452CD">5</span>] = AUC
<span style="color: #8B008B; font-weight: bold">if</span> printOut:
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\n################### Testing ###############&#39;</span>)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\nTest Confusion matrix: \n&#39;</span>, cm)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\nTest Accuracy score: \n&#39;</span>, accScore)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\nTestAUC: \n&#39;</span>, AUC)
<span style="color: #8B008B; font-weight: bold">return</span> confusionArray
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">seaborn</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">scikitplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">skplt</span>
seaborn.set(style=<span style="color: #CD5555">&quot;white&quot;</span>, context=<span style="color: #CD5555">&quot;notebook&quot;</span>, font_scale=<span style="color: #B452CD">1.5</span>,
rc={<span style="color: #CD5555">&quot;axes.grid&quot;</span>: <span style="color: #658b00">True</span>, <span style="color: #CD5555">&quot;legend.frameon&quot;</span>: <span style="color: #658b00">False</span>,
<span style="color: #CD5555">&quot;lines.markeredgewidth&quot;</span>: <span style="color: #B452CD">1.4</span>, <span style="color: #CD5555">&quot;lines.markersize&quot;</span>: <span style="color: #B452CD">10</span>})
seaborn.set_context(<span style="color: #CD5555">&quot;notebook&quot;</span>, font_scale=<span style="color: #B452CD">1.5</span>, rc={<span style="color: #CD5555">&quot;lines.linewidth&quot;</span>: <span style="color: #B452CD">4.5</span>})
yPred = gridSearch.predict_proba(XTest)
<span style="color: #8B008B; font-weight: bold">print</span>(yTest.ravel().shape, yPred.shape)
<span style="color: #228B22">#skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred_onehot)</span>
skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred)
defaults = <span style="color: #658b00">sum</span>(yTest == <span style="color: #B452CD">1</span>)
total = <span style="color: #658b00">len</span>(yTest)
defaultRate = defaults/total
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">bestCurve</span>(defaults, total, defaultRate):
x = np.linspace(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, total)
y1 = np.linspace(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, defaults)
y2 = np.ones(total-defaults)
y3 = np.concatenate([y1,y2])
<span style="color: #8B008B; font-weight: bold">return</span> x, y3
x, best = bestCurve(defaults=defaults, total=total, defaultRate=defaultRate)
plt.plot(x, best)
plt.show()
</pre></div>
</section>
<section>
<h2 id="___sec17">Analysis of the credi card example </h2>
</section>
+19 -115
View File
@@ -56,7 +56,7 @@ div { text-align: justify; text-justify: inter-word; }
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -98,7 +98,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Oct 9, 2019</h4></center> <!-- date -->
<center><h4>Oct 17, 2019</h4></center> <!-- date -->
<br>
<p>
<!-- !split -->
@@ -570,7 +570,23 @@ methods</a>.
<h2 id="___sec16">The Credit Card example </h2>
Here we use the the <a href="https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients" target="_blank">credit card data</a>.
The data are from an extensive database from Taiwan and include more than ten predictors. More text and clean up of code will be added.
The data are from an extensive database from Taiwan and include more than ten predictors.
<p>
For categorical data -Scikit-Learn- provides a so-called <b>one-hot encoder</b>.
This is called one-hot
encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold).
<b>Scikit-Learn</b> provides a OneHotEncoder encoder to convert integer categorical values into one-hot
<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">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> OneHotEncoder
encoder = OneHotEncoder()
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec17">How to read the Credit Card data </h2>
<p>
@@ -668,118 +684,6 @@ gridSearch = GridSearchCV(logReg, parameters, cv=<span style="color: #B452CD">5<
</pre></div>
<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: #228B22"># &quot;refit&quot; gives the metric used deciding best model. </span>
<span style="color: #228B22"># See more http://scikit-learn.org/stable/auto_examples/model_selection/plot_multi_metric_evaluation.html</span>
gridSearch.fit(XTrain, yTrain.ravel())
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">gridSearchSummary</span>(method, scoring):
<span style="color: #CD5555">&quot;&quot;&quot;Prints best parameters from Grid search</span>
<span style="color: #CD5555"> and AUC with standard deviation for all </span>
<span style="color: #CD5555"> parameter combos &quot;&quot;&quot;</span>
method = <span style="color: #658b00">eval</span>(method)
<span style="color: #8B008B; font-weight: bold">if</span> scoring == <span style="color: #CD5555">&#39;accuracy&#39;</span>:
mean = <span style="color: #CD5555">&#39;mean_test_score&#39;</span>
sd = <span style="color: #CD5555">&#39;std_test_score&#39;</span>
<span style="color: #8B008B; font-weight: bold">elif</span> scoring == <span style="color: #CD5555">&#39;auc&#39;</span>:
mean = <span style="color: #CD5555">&#39;mean_test_roc_auc&#39;</span>
sd = <span style="color: #CD5555">&#39;std_test_roc_auc&#39;</span>
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;Best: %f using %s&quot;</span> % (method.best_score_, method.best_params_))
means = method.cv_results_[mean]
stds = method.cv_results_[sd]
params = method.cv_results_[<span style="color: #CD5555">&#39;params&#39;</span>]
<span style="color: #8B008B; font-weight: bold">for</span> mean, stdev, param <span style="color: #8B008B">in</span> <span style="color: #658b00">zip</span>(means, stds, params):
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&quot;%f (%f) with: %r&quot;</span> % (mean, stdev, param))
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">createConfusionMatrix</span>(method, printOut=<span style="color: #658b00">True</span>):
<span style="color: #CD5555">&quot;&quot;&quot;</span>
<span style="color: #CD5555"> Computes and prints confusion matrices, accuracy scores,</span>
<span style="color: #CD5555"> and AUC for test and training sets </span>
<span style="color: #CD5555"> &quot;&quot;&quot;</span>
confusionArray = np.zeros(<span style="color: #B452CD">6</span>, dtype=<span style="color: #658b00">object</span>)
method = <span style="color: #658b00">eval</span>(method)
<span style="color: #228B22"># Train</span>
yPredTrain = method.predict(XTrain)
yPredTrain = (yPredTrain &gt; <span style="color: #B452CD">0.5</span>)
cm = confusion_matrix(
yTrain, yPredTrain)
cm = np.around(cm/cm.sum(axis=<span style="color: #B452CD">1</span>)[:,<span style="color: #658b00">None</span>], <span style="color: #B452CD">2</span>)
confusionArray[<span style="color: #B452CD">0</span>] = cm
accScore = accuracy_score(yTrain, yPredTrain)
confusionArray[<span style="color: #B452CD">1</span>] = accScore
AUC = roc_auc_score(yTrain, yPredTrain)
confusionArray[<span style="color: #B452CD">2</span>] = AUC
<span style="color: #8B008B; font-weight: bold">if</span> printOut:
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\n################### Training ###############&#39;</span>)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\nTraining Confusion matrix: \n&#39;</span>, cm)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\nTraining Accuracy score: \n&#39;</span>, accScore)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\nTrain AUC: \n&#39;</span>, AUC)
<span style="color: #228B22"># Test</span>
yPred = method.predict(XTest)
yPred = (yPred &gt; <span style="color: #B452CD">0.5</span>)
cm = confusion_matrix(
yTest, yPred)
cm = np.around(cm/cm.sum(axis=<span style="color: #B452CD">1</span>)[:,<span style="color: #658b00">None</span>], <span style="color: #B452CD">2</span>)
confusionArray[<span style="color: #B452CD">3</span>] = cm
accScore = accuracy_score(yTest, yPred)
confusionArray[<span style="color: #B452CD">4</span>] = accScore
AUC = roc_auc_score(yTest, yPred)
confusionArray[<span style="color: #B452CD">5</span>] = AUC
<span style="color: #8B008B; font-weight: bold">if</span> printOut:
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\n################### Testing ###############&#39;</span>)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\nTest Confusion matrix: \n&#39;</span>, cm)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\nTest Accuracy score: \n&#39;</span>, accScore)
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">&#39;\nTestAUC: \n&#39;</span>, AUC)
<span style="color: #8B008B; font-weight: bold">return</span> confusionArray
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">seaborn</span>
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">scikitplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">skplt</span>
seaborn.set(style=<span style="color: #CD5555">&quot;white&quot;</span>, context=<span style="color: #CD5555">&quot;notebook&quot;</span>, font_scale=<span style="color: #B452CD">1.5</span>,
rc={<span style="color: #CD5555">&quot;axes.grid&quot;</span>: <span style="color: #658b00">True</span>, <span style="color: #CD5555">&quot;legend.frameon&quot;</span>: <span style="color: #658b00">False</span>,
<span style="color: #CD5555">&quot;lines.markeredgewidth&quot;</span>: <span style="color: #B452CD">1.4</span>, <span style="color: #CD5555">&quot;lines.markersize&quot;</span>: <span style="color: #B452CD">10</span>})
seaborn.set_context(<span style="color: #CD5555">&quot;notebook&quot;</span>, font_scale=<span style="color: #B452CD">1.5</span>, rc={<span style="color: #CD5555">&quot;lines.linewidth&quot;</span>: <span style="color: #B452CD">4.5</span>})
yPred = gridSearch.predict_proba(XTest)
<span style="color: #8B008B; font-weight: bold">print</span>(yTest.ravel().shape, yPred.shape)
<span style="color: #228B22">#skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred_onehot)</span>
skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred)
defaults = <span style="color: #658b00">sum</span>(yTest == <span style="color: #B452CD">1</span>)
total = <span style="color: #658b00">len</span>(yTest)
defaultRate = defaults/total
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">bestCurve</span>(defaults, total, defaultRate):
x = np.linspace(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, total)
y1 = np.linspace(<span style="color: #B452CD">0</span>, <span style="color: #B452CD">1</span>, defaults)
y2 = np.ones(total-defaults)
y3 = np.concatenate([y1,y2])
<span style="color: #8B008B; font-weight: bold">return</span> x, y3
x, best = bestCurve(defaults=defaults, total=total, defaultRate=defaultRate)
plt.plot(x, best)
plt.show()
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec17">Analysis of the credi card example </h2>
<!-- ------------------- end of main content --------------- -->
+19 -115
View File
@@ -61,7 +61,7 @@ div { text-align: justify; text-justify: inter-word; }
('More classes', 2, None, '___sec14'),
('A simple classification problem', 2, None, '___sec15'),
('The Credit Card example', 2, None, '___sec16'),
('Analysis of the credi card example', 2, None, '___sec17')]}
('How to read the Credit Card data', 2, None, '___sec17')]}
end of tocinfo -->
<body>
@@ -103,7 +103,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Oct 9, 2019</h4></center> <!-- date -->
<center><h4>Oct 17, 2019</h4></center> <!-- date -->
<br>
<p>
<!-- !split -->
@@ -575,7 +575,23 @@ methods</a>.
<h2 id="___sec16">The Credit Card example </h2>
Here we use the the <a href="https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients" target="_blank">credit card data</a>.
The data are from an extensive database from Taiwan and include more than ten predictors. More text and clean up of code will be added.
The data are from an extensive database from Taiwan and include more than ten predictors.
<p>
For categorical data -Scikit-Learn- provides a so-called <b>one-hot encoder</b>.
This is called one-hot
encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold).
<b>Scikit-Learn</b> provides a OneHotEncoder encoder to convert integer categorical values into one-hot
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> OneHotEncoder
encoder <span style="color: #666666">=</span> OneHotEncoder()
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec17">How to read the Credit Card data </h2>
<p>
@@ -673,118 +689,6 @@ gridSearch <span style="color: #666666">=</span> GridSearchCV(logReg, parameters
</pre></div>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># &quot;refit&quot; gives the metric used deciding best model. </span>
<span style="color: #408080; font-style: italic"># See more http://scikit-learn.org/stable/auto_examples/model_selection/plot_multi_metric_evaluation.html</span>
gridSearch<span style="color: #666666">.</span>fit(XTrain, yTrain<span style="color: #666666">.</span>ravel())
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">gridSearchSummary</span>(method, scoring):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;Prints best parameters from Grid search</span>
<span style="color: #BA2121; font-style: italic"> and AUC with standard deviation for all </span>
<span style="color: #BA2121; font-style: italic"> parameter combos &quot;&quot;&quot;</span>
method <span style="color: #666666">=</span> <span style="color: #008000">eval</span>(method)
<span style="color: #008000; font-weight: bold">if</span> scoring <span style="color: #666666">==</span> <span style="color: #BA2121">&#39;accuracy&#39;</span>:
mean <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;mean_test_score&#39;</span>
sd <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;std_test_score&#39;</span>
<span style="color: #008000; font-weight: bold">elif</span> scoring <span style="color: #666666">==</span> <span style="color: #BA2121">&#39;auc&#39;</span>:
mean <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;mean_test_roc_auc&#39;</span>
sd <span style="color: #666666">=</span> <span style="color: #BA2121">&#39;std_test_roc_auc&#39;</span>
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Best: </span><span style="color: #BB6688; font-weight: bold">%f</span><span style="color: #BA2121"> using </span><span style="color: #BB6688; font-weight: bold">%s</span><span style="color: #BA2121">&quot;</span> <span style="color: #666666">%</span> (method<span style="color: #666666">.</span>best_score_, method<span style="color: #666666">.</span>best_params_))
means <span style="color: #666666">=</span> method<span style="color: #666666">.</span>cv_results_[mean]
stds <span style="color: #666666">=</span> method<span style="color: #666666">.</span>cv_results_[sd]
params <span style="color: #666666">=</span> method<span style="color: #666666">.</span>cv_results_[<span style="color: #BA2121">&#39;params&#39;</span>]
<span style="color: #008000; font-weight: bold">for</span> mean, stdev, param <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">zip</span>(means, stds, params):
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;</span><span style="color: #BB6688; font-weight: bold">%f</span><span style="color: #BA2121"> (</span><span style="color: #BB6688; font-weight: bold">%f</span><span style="color: #BA2121">) with: </span><span style="color: #BB6688; font-weight: bold">%r</span><span style="color: #BA2121">&quot;</span> <span style="color: #666666">%</span> (mean, stdev, param))
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">createConfusionMatrix</span>(method, printOut<span style="color: #666666">=</span><span style="color: #008000">True</span>):
<span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #BA2121; font-style: italic"> Computes and prints confusion matrices, accuracy scores,</span>
<span style="color: #BA2121; font-style: italic"> and AUC for test and training sets </span>
<span style="color: #BA2121; font-style: italic"> &quot;&quot;&quot;</span>
confusionArray <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(<span style="color: #666666">6</span>, dtype<span style="color: #666666">=</span><span style="color: #008000">object</span>)
method <span style="color: #666666">=</span> <span style="color: #008000">eval</span>(method)
<span style="color: #408080; font-style: italic"># Train</span>
yPredTrain <span style="color: #666666">=</span> method<span style="color: #666666">.</span>predict(XTrain)
yPredTrain <span style="color: #666666">=</span> (yPredTrain <span style="color: #666666">&gt;</span> <span style="color: #666666">0.5</span>)
cm <span style="color: #666666">=</span> confusion_matrix(
yTrain, yPredTrain)
cm <span style="color: #666666">=</span> np<span style="color: #666666">.</span>around(cm<span style="color: #666666">/</span>cm<span style="color: #666666">.</span>sum(axis<span style="color: #666666">=1</span>)[:,<span style="color: #008000">None</span>], <span style="color: #666666">2</span>)
confusionArray[<span style="color: #666666">0</span>] <span style="color: #666666">=</span> cm
accScore <span style="color: #666666">=</span> accuracy_score(yTrain, yPredTrain)
confusionArray[<span style="color: #666666">1</span>] <span style="color: #666666">=</span> accScore
AUC <span style="color: #666666">=</span> roc_auc_score(yTrain, yPredTrain)
confusionArray[<span style="color: #666666">2</span>] <span style="color: #666666">=</span> AUC
<span style="color: #008000; font-weight: bold">if</span> printOut:
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">################### Training ###############&#39;</span>)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">Training Confusion matrix: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">&#39;</span>, cm)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">Training Accuracy score: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">&#39;</span>, accScore)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">Train AUC: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">&#39;</span>, AUC)
<span style="color: #408080; font-style: italic"># Test</span>
yPred <span style="color: #666666">=</span> method<span style="color: #666666">.</span>predict(XTest)
yPred <span style="color: #666666">=</span> (yPred <span style="color: #666666">&gt;</span> <span style="color: #666666">0.5</span>)
cm <span style="color: #666666">=</span> confusion_matrix(
yTest, yPred)
cm <span style="color: #666666">=</span> np<span style="color: #666666">.</span>around(cm<span style="color: #666666">/</span>cm<span style="color: #666666">.</span>sum(axis<span style="color: #666666">=1</span>)[:,<span style="color: #008000">None</span>], <span style="color: #666666">2</span>)
confusionArray[<span style="color: #666666">3</span>] <span style="color: #666666">=</span> cm
accScore <span style="color: #666666">=</span> accuracy_score(yTest, yPred)
confusionArray[<span style="color: #666666">4</span>] <span style="color: #666666">=</span> accScore
AUC <span style="color: #666666">=</span> roc_auc_score(yTest, yPred)
confusionArray[<span style="color: #666666">5</span>] <span style="color: #666666">=</span> AUC
<span style="color: #008000; font-weight: bold">if</span> printOut:
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">################### Testing ###############&#39;</span>)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">Test Confusion matrix: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">&#39;</span>, cm)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">Test Accuracy score: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">&#39;</span>, accScore)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;</span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">TestAUC: </span><span style="color: #BB6622; font-weight: bold">\n</span><span style="color: #BA2121">&#39;</span>, AUC)
<span style="color: #008000; font-weight: bold">return</span> confusionArray
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">seaborn</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">scikitplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">skplt</span>
seaborn<span style="color: #666666">.</span>set(style<span style="color: #666666">=</span><span style="color: #BA2121">&quot;white&quot;</span>, context<span style="color: #666666">=</span><span style="color: #BA2121">&quot;notebook&quot;</span>, font_scale<span style="color: #666666">=1.5</span>,
rc<span style="color: #666666">=</span>{<span style="color: #BA2121">&quot;axes.grid&quot;</span>: <span style="color: #008000">True</span>, <span style="color: #BA2121">&quot;legend.frameon&quot;</span>: <span style="color: #008000">False</span>,
<span style="color: #BA2121">&quot;lines.markeredgewidth&quot;</span>: <span style="color: #666666">1.4</span>, <span style="color: #BA2121">&quot;lines.markersize&quot;</span>: <span style="color: #666666">10</span>})
seaborn<span style="color: #666666">.</span>set_context(<span style="color: #BA2121">&quot;notebook&quot;</span>, font_scale<span style="color: #666666">=1.5</span>, rc<span style="color: #666666">=</span>{<span style="color: #BA2121">&quot;lines.linewidth&quot;</span>: <span style="color: #666666">4.5</span>})
yPred <span style="color: #666666">=</span> gridSearch<span style="color: #666666">.</span>predict_proba(XTest)
<span style="color: #008000; font-weight: bold">print</span>(yTest<span style="color: #666666">.</span>ravel()<span style="color: #666666">.</span>shape, yPred<span style="color: #666666">.</span>shape)
<span style="color: #408080; font-style: italic">#skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred_onehot)</span>
skplt<span style="color: #666666">.</span>metrics<span style="color: #666666">.</span>plot_cumulative_gain(yTest<span style="color: #666666">.</span>ravel(), yPred)
defaults <span style="color: #666666">=</span> <span style="color: #008000">sum</span>(yTest <span style="color: #666666">==</span> <span style="color: #666666">1</span>)
total <span style="color: #666666">=</span> <span style="color: #008000">len</span>(yTest)
defaultRate <span style="color: #666666">=</span> defaults<span style="color: #666666">/</span>total
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">bestCurve</span>(defaults, total, defaultRate):
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, total)
y1 <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>, <span style="color: #666666">1</span>, defaults)
y2 <span style="color: #666666">=</span> np<span style="color: #666666">.</span>ones(total<span style="color: #666666">-</span>defaults)
y3 <span style="color: #666666">=</span> np<span style="color: #666666">.</span>concatenate([y1,y2])
<span style="color: #008000; font-weight: bold">return</span> x, y3
x, best <span style="color: #666666">=</span> bestCurve(defaults<span style="color: #666666">=</span>defaults, total<span style="color: #666666">=</span>total, defaultRate<span style="color: #666666">=</span>defaultRate)
plt<span style="color: #666666">.</span>plot(x, best)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec17">Analysis of the credi card example </h2>
<!-- ------------------- end of main content --------------- -->
+26 -125
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: **Oct 9, 2019**\n",
"Date: **Oct 17, 2019**\n",
"\n",
"Copyright 1999-2019, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
"\n",
@@ -669,7 +669,12 @@
"source": [
"## The Credit Card example\n",
"Here we use the the [credit card data](https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients). \n",
"The data are from an extensive database from Taiwan and include more than ten predictors. More text and clean up of code will be added."
"The data are from an extensive database from Taiwan and include more than ten predictors.\n",
"\n",
"For categorical data -Scikit-Learn- provides a so-called **one-hot encoder**.\n",
"This is called one-hot\n",
"encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold).\n",
"**Scikit-Learn** provides a OneHotEncoder encoder to convert integer categorical values into one-hot"
]
},
{
@@ -679,6 +684,25 @@
"collapsed": false
},
"outputs": [],
"source": [
"from sklearn.preprocessing import OneHotEncoder\n",
"encoder = OneHotEncoder()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to read the Credit Card data"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import os\n",
@@ -771,129 +795,6 @@
"logReg = LogisticRegression()\n",
"gridSearch = GridSearchCV(logReg, parameters, cv=5, scoring=scoring, refit='roc_auc')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\n",
"# \"refit\" gives the metric used deciding best model. \n",
"# See more http://scikit-learn.org/stable/auto_examples/model_selection/plot_multi_metric_evaluation.html\n",
"gridSearch.fit(XTrain, yTrain.ravel())\n",
"\n",
"def gridSearchSummary(method, scoring):\n",
" \"\"\"Prints best parameters from Grid search\n",
" and AUC with standard deviation for all \n",
" parameter combos \"\"\"\n",
" \n",
" method = eval(method)\n",
" if scoring == 'accuracy':\n",
" mean = 'mean_test_score'\n",
" sd = 'std_test_score'\n",
" elif scoring == 'auc':\n",
" mean = 'mean_test_roc_auc'\n",
" sd = 'std_test_roc_auc'\n",
" print(\"Best: %f using %s\" % (method.best_score_, method.best_params_))\n",
" means = method.cv_results_[mean]\n",
" stds = method.cv_results_[sd]\n",
" params = method.cv_results_['params']\n",
" for mean, stdev, param in zip(means, stds, params):\n",
" print(\"%f (%f) with: %r\" % (mean, stdev, param))\n",
"\n",
"def createConfusionMatrix(method, printOut=True):\n",
" \"\"\"\n",
" Computes and prints confusion matrices, accuracy scores,\n",
" and AUC for test and training sets \n",
" \"\"\"\n",
" confusionArray = np.zeros(6, dtype=object)\n",
" method = eval(method)\n",
" \n",
" # Train\n",
" yPredTrain = method.predict(XTrain)\n",
" yPredTrain = (yPredTrain > 0.5)\n",
" cm = confusion_matrix(\n",
" yTrain, yPredTrain) \n",
" cm = np.around(cm/cm.sum(axis=1)[:,None], 2)\n",
" confusionArray[0] = cm\n",
" \n",
" accScore = accuracy_score(yTrain, yPredTrain)\n",
" confusionArray[1] = accScore\n",
" \n",
" AUC = roc_auc_score(yTrain, yPredTrain)\n",
" confusionArray[2] = AUC\n",
" \n",
" if printOut:\n",
" print('\\n################### Training ###############')\n",
" print('\\nTraining Confusion matrix: \\n', cm)\n",
" print('\\nTraining Accuracy score: \\n', accScore)\n",
" print('\\nTrain AUC: \\n', AUC)\n",
" \n",
" # Test\n",
" yPred = method.predict(XTest)\n",
" yPred = (yPred > 0.5)\n",
" cm = confusion_matrix(\n",
" yTest, yPred) \n",
" cm = np.around(cm/cm.sum(axis=1)[:,None], 2)\n",
" confusionArray[3] = cm\n",
" \n",
" accScore = accuracy_score(yTest, yPred)\n",
" confusionArray[4] = accScore\n",
" \n",
" AUC = roc_auc_score(yTest, yPred)\n",
" confusionArray[5] = AUC\n",
" \n",
" if printOut:\n",
" print('\\n################### Testing ###############')\n",
" print('\\nTest Confusion matrix: \\n', cm)\n",
" print('\\nTest Accuracy score: \\n', accScore)\n",
" print('\\nTestAUC: \\n', AUC) \n",
" \n",
" return confusionArray\n",
"\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import seaborn\n",
"import scikitplot as skplt\n",
"\n",
"seaborn.set(style=\"white\", context=\"notebook\", font_scale=1.5, \n",
" rc={\"axes.grid\": True, \"legend.frameon\": False,\n",
"\"lines.markeredgewidth\": 1.4, \"lines.markersize\": 10})\n",
"seaborn.set_context(\"notebook\", font_scale=1.5, rc={\"lines.linewidth\": 4.5})\n",
"\n",
"yPred = gridSearch.predict_proba(XTest) \n",
"print(yTest.ravel().shape, yPred.shape)\n",
"\n",
"#skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred_onehot)\n",
"skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred)\n",
"\n",
"defaults = sum(yTest == 1)\n",
"total = len(yTest)\n",
"defaultRate = defaults/total\n",
"def bestCurve(defaults, total, defaultRate):\n",
" x = np.linspace(0, 1, total)\n",
" \n",
" y1 = np.linspace(0, 1, defaults)\n",
" y2 = np.ones(total-defaults)\n",
" y3 = np.concatenate([y1,y2])\n",
" return x, y3\n",
"\n",
"x, best = bestCurve(defaults=defaults, total=total, defaultRate=defaultRate) \n",
"plt.plot(x, best) \n",
"\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Analysis of the credi card example"
]
}
],
"metadata": {},
Binary file not shown.
Binary file not shown.
+13 -114
View File
@@ -451,7 +451,19 @@ if __name__ == "__main__":
!split
===== The Credit Card example =====
Here we use the the "credit card data":"https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients".
The data are from an extensive database from Taiwan and include more than ten predictors. More text and clean up of code will be added.
The data are from an extensive database from Taiwan and include more than ten predictors.
For categorical data -Scikit-Learn- provides a so-called _one-hot encoder_.
This is called one-hot
encoding, because only one attribute will be equal to 1 (hot), while the others will be 0 (cold).
_Scikit-Learn_ provides a OneHotEncoder encoder to convert integer categorical values into one-hot
!bc pycod
from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder()
!ec
!split
===== How to read the Credit Card data =====
!bc pycod
import pandas as pd
@@ -546,116 +558,3 @@ logReg = LogisticRegression()
gridSearch = GridSearchCV(logReg, parameters, cv=5, scoring=scoring, refit='roc_auc')
!ec
!bc pycod
# "refit" gives the metric used deciding best model.
# See more http://scikit-learn.org/stable/auto_examples/model_selection/plot_multi_metric_evaluation.html
gridSearch.fit(XTrain, yTrain.ravel())
def gridSearchSummary(method, scoring):
"""Prints best parameters from Grid search
and AUC with standard deviation for all
parameter combos """
method = eval(method)
if scoring == 'accuracy':
mean = 'mean_test_score'
sd = 'std_test_score'
elif scoring == 'auc':
mean = 'mean_test_roc_auc'
sd = 'std_test_roc_auc'
print("Best: %f using %s" % (method.best_score_, method.best_params_))
means = method.cv_results_[mean]
stds = method.cv_results_[sd]
params = method.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print("%f (%f) with: %r" % (mean, stdev, param))
def createConfusionMatrix(method, printOut=True):
"""
Computes and prints confusion matrices, accuracy scores,
and AUC for test and training sets
"""
confusionArray = np.zeros(6, dtype=object)
method = eval(method)
# Train
yPredTrain = method.predict(XTrain)
yPredTrain = (yPredTrain > 0.5)
cm = confusion_matrix(
yTrain, yPredTrain)
cm = np.around(cm/cm.sum(axis=1)[:,None], 2)
confusionArray[0] = cm
accScore = accuracy_score(yTrain, yPredTrain)
confusionArray[1] = accScore
AUC = roc_auc_score(yTrain, yPredTrain)
confusionArray[2] = AUC
if printOut:
print('\n################### Training ###############')
print('\nTraining Confusion matrix: \n', cm)
print('\nTraining Accuracy score: \n', accScore)
print('\nTrain AUC: \n', AUC)
# Test
yPred = method.predict(XTest)
yPred = (yPred > 0.5)
cm = confusion_matrix(
yTest, yPred)
cm = np.around(cm/cm.sum(axis=1)[:,None], 2)
confusionArray[3] = cm
accScore = accuracy_score(yTest, yPred)
confusionArray[4] = accScore
AUC = roc_auc_score(yTest, yPred)
confusionArray[5] = AUC
if printOut:
print('\n################### Testing ###############')
print('\nTest Confusion matrix: \n', cm)
print('\nTest Accuracy score: \n', accScore)
print('\nTestAUC: \n', AUC)
return confusionArray
import matplotlib.pyplot as plt
import seaborn
import scikitplot as skplt
seaborn.set(style="white", context="notebook", font_scale=1.5,
rc={"axes.grid": True, "legend.frameon": False,
"lines.markeredgewidth": 1.4, "lines.markersize": 10})
seaborn.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 4.5})
yPred = gridSearch.predict_proba(XTest)
print(yTest.ravel().shape, yPred.shape)
#skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred_onehot)
skplt.metrics.plot_cumulative_gain(yTest.ravel(), yPred)
defaults = sum(yTest == 1)
total = len(yTest)
defaultRate = defaults/total
def bestCurve(defaults, total, defaultRate):
x = np.linspace(0, 1, total)
y1 = np.linspace(0, 1, defaults)
y2 = np.ones(total-defaults)
y3 = np.concatenate([y1,y2])
return x, y3
x, best = bestCurve(defaults=defaults, total=total, defaultRate=defaultRate)
plt.plot(x, best)
plt.show()
!ec
!split
===== Analysis of the credi card example =====