You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/chapters/03-score-based-classification.html
+14-14Lines changed: 14 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -411,21 +411,21 @@ <h3 class="anchored" data-anchor-id="to-lend-or-not-to-lend">To Lend Or Not To L
411
411
<p>Banks can try to balance these risks by controlling interest rates. Higher interest rates increase prospective profit if the loan is repaid in full, but also increase the risk that an individual may be unable to keep up with payments.</p>
412
412
<p>Historically, the judgment of whether to extend an individual a loan was handled by human experts. Recently, human experts have been seeking assistance from machine learning algorithms. As in most predictive modeling, the idea is to use the past to predict the future. Here, we’ll consider simple modeling problem in which we aim to learn patterns in when individuals are able to pay off loans, and use these patterns to make predictions.</p>
413
413
<p>We’ll first load in the <code>pandas</code> package and use the <code>read_csv</code> command to acquire our data for this problem as a <code>pd.DataFrame</code>. <ahref="https://www.kaggle.com/datasets/laotse/credit-risk-dataset">This data set was produced by Kaggle</a>; it is a <em>simulated</em> data set based on patterns in real world data, which, of course, is sensitive and confidential. For today, we are only going to focus on the first 1,000 rows of data.</p>
<spanid="cb1-4"><ahref="#cb1-4" aria-hidden="true" tabindex="-1"></a>plt.style.use(<spanclass="st">'seaborn-v0_8-whitegrid'</span>)</span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
419
419
</div>
420
420
<p>Here’s the URL where I’ve hosted the data set. We can read in the data using the <code>pd.read_csv()</code> command, and then subset to the first 1,000 rows.</p>
@@ -629,7 +629,7 @@ <h3 class="anchored" data-anchor-id="to-lend-or-not-to-lend">To Lend Or Not To L
629
629
<li><code>loan_status</code> describes whether or not the individual defaulted on the loan. This column has value <code>1</code> if the individual defaulted on the loan and value <code>0</code> if the loan was repaid in full.</li>
630
630
</ul>
631
631
<p>Our primary predictive interest is whether or not a borrower is likely to default on a loan. How common is this in our data set?</p>
@@ -699,12 +699,12 @@ <h3 class="anchored" data-anchor-id="to-lend-or-not-to-lend">To Lend Or Not To L
699
699
\end{aligned}
700
700
\tag{3.1}\]</span></span></p>
701
701
<divclass="page-columns page-full"><p>Let’s implement this score in Python. </p><divclass="no-row-height column-margin column-container"><spanclass="margin-aside">This is not the optimal implementation of the score – we’ll see a much better approach that leverages linear algebra and matrix multiplication soon.</span></div></div>
<h3class="anchored" data-anchor-id="from-scores-to-predictions">From Scores to Predictions</h3>
782
782
<p>Ok, great – we have a risk score for historical applicants. We can even compute risk scores for <em>future</em> applicants: plug their data into <ahref="#eq-risk-score" class="quarto-xref">Equation <span>3.1</span></a>. But in order to make a decision, we need to conver the score into a yes-no decision. A common way to do this is called <em>thresholding</em>: we pick a threshold <spanclass="math inline">\(t\)</span> and approve a loan to individual <spanclass="math inline">\(i\)</span> of <spanclass="math inline">\(s_i < t\)</span>.</p>
@@ -826,7 +826,7 @@ <h3 class="anchored" data-anchor-id="from-scores-to-predictions">From Scores to
826
826
</div>
827
827
<p>Something interesting to notice here is that our two step method of computing a linear score function and thresholding gave us the same <em>linear</em> classification pattern as the one we saw in <ahref="01-data-and-models.html#fig-examples-with-patterns-2" class="quarto-xref">Figure <span>1.2 (b)</span></a>.</p>
828
828
<p>Finally, once we’ve picked the weight vector <spanclass="math inline">\(\mathbf{w}\)</span> and a threshold function <spanclass="math inline">\(t\)</span>, we are ready to simulate making decisions. For example, with our current weights and threshold, we can add a column with what our retrospective “decisions” would have been on this historical data. Here is a function that generates the column.</p>
<spanid="cb14-3"><ahref="#cb14-3" aria-hidden="true" tabindex="-1"></a><spanclass="co"> make binary predictions for data df using a supplied score function with weights w and supplied threshold. </span></span>
@@ -835,11 +835,11 @@ <h3 class="anchored" data-anchor-id="from-scores-to-predictions">From Scores to
835
835
<spanid="cb14-6"><ahref="#cb14-6" aria-hidden="true" tabindex="-1"></a><spanclass="cf">return</span><spanclass="dv">1</span><spanclass="op">*</span>(scores <spanclass="op">></span> threshold)</span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
836
836
</div>
837
837
<p>Now let’s use this function to add the predictions as a new column in the data frame:</p>
<p>Would our decisions have accurately reflected who in fact defaulted? One way to address this question is by measuring the <em>accuracy</em> of our decisions, which we can compute using vectorized code:</p>
@@ -871,12 +871,12 @@ <h2 class="anchored" data-anchor-id="what-about-nonlinear-scores">What About Non
871
871
\end{aligned}
872
872
\]</span></p><divclass="no-row-height column-margin column-container"><spanclass="margin-aside">In order for this formula to make sense, we now need <spanclass="math inline">\(\mathbf{w}\in \mathbb{R}^5\)</span>.</span></div></div>
873
873
<p>Here’s an implementation of a score function with quadratic features:</p>
<p>Now we can set a new vector of weights <spanclass="math inline">\(\mathbf{w}\in \mathbb{R}^5\)</span> and a threshold <spanclass="math inline">\(t\)</span>.</p>
0 commit comments