corrected typos
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -32,15 +32,15 @@
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"# Overarching aims of the exercises weeks 43 and 44\n",
|
||||
"# Overarching aims of the exercises for week 43\n",
|
||||
"\n",
|
||||
"The aim of the exercises this week is to gain some confidence with\n",
|
||||
"ways to visualize the results of a classification problem. We will\n",
|
||||
"target three ways of setting up the analysis. The first and simplest\n",
|
||||
"one is the\n",
|
||||
"1. so-called confusion matrix, and the next is the\n",
|
||||
"1. so-called confusion matrix. The next one is the so-called\n",
|
||||
"\n",
|
||||
"2. ROC curve and finally the\n",
|
||||
"2. ROC curve. Finally we have the\n",
|
||||
"\n",
|
||||
"3. Cumulative gain curve.\n",
|
||||
"\n",
|
||||
@@ -446,7 +446,10 @@
|
||||
"id": "be9ff0b9",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
"editable": true,
|
||||
"jupyter": {
|
||||
"outputs_hidden": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -518,7 +521,10 @@
|
||||
"id": "d20bb8be",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
"editable": true,
|
||||
"jupyter": {
|
||||
"outputs_hidden": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -547,7 +553,10 @@
|
||||
"id": "d271f0ba",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
"editable": true,
|
||||
"jupyter": {
|
||||
"outputs_hidden": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -589,7 +598,10 @@
|
||||
"id": "3b045d56",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
"editable": true,
|
||||
"jupyter": {
|
||||
"outputs_hidden": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -611,7 +623,25 @@
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {},
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.15"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
||||
@@ -414,7 +414,7 @@ document.write(`
|
||||
<nav aria-label="Page">
|
||||
<ul class="visible nav section-nav flex-column">
|
||||
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#">Exercises week 43</a></li>
|
||||
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#overarching-aims-of-the-exercises-weeks-43-and-44">Overarching aims of the exercises weeks 43 and 44</a><ul class="visible nav section-nav flex-column">
|
||||
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#overarching-aims-of-the-exercises-for-week-43">Overarching aims of the exercises for week 43</a><ul class="visible nav section-nav flex-column">
|
||||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#confusion-matrix">Confusion Matrix</a></li>
|
||||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#roc-curve">ROC Curve</a></li>
|
||||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#cumulative-gain">Cumulative Gain</a></li>
|
||||
@@ -446,15 +446,15 @@ doconce format html exercisesweek43.do.txt -->
|
||||
<p><strong>October 20-24, 2025</strong></p>
|
||||
<p>Date: <strong>Deadline Friday October 24 at midnight</strong></p>
|
||||
</section>
|
||||
<section class="tex2jax_ignore mathjax_ignore" id="overarching-aims-of-the-exercises-weeks-43-and-44">
|
||||
<h1>Overarching aims of the exercises weeks 43 and 44<a class="headerlink" href="#overarching-aims-of-the-exercises-weeks-43-and-44" title="Link to this heading">#</a></h1>
|
||||
<section class="tex2jax_ignore mathjax_ignore" id="overarching-aims-of-the-exercises-for-week-43">
|
||||
<h1>Overarching aims of the exercises for week 43<a class="headerlink" href="#overarching-aims-of-the-exercises-for-week-43" title="Link to this heading">#</a></h1>
|
||||
<p>The aim of the exercises this week is to gain some confidence with
|
||||
ways to visualize the results of a classification problem. We will
|
||||
target three ways of setting up the analysis. The first and simplest
|
||||
one is the</p>
|
||||
<ol class="arabic simple">
|
||||
<li><p>so-called confusion matrix, and the next is the</p></li>
|
||||
<li><p>ROC curve and finally the</p></li>
|
||||
<li><p>so-called confusion matrix. The next one is the so-called</p></li>
|
||||
<li><p>ROC curve. Finally we have the</p></li>
|
||||
<li><p>Cumulative gain curve.</p></li>
|
||||
</ol>
|
||||
<p>We will use Logistic Regression as method for the classification in
|
||||
@@ -615,41 +615,41 @@ At the end it sets up the confusion matrix and the ROC and cumulative gain curve
|
||||
Feel free to use these functionalities (we don’t expect you to write your own code for say the confusion matrix).</p>
|
||||
<div class="cell docutils container">
|
||||
<div class="cell_input docutils container">
|
||||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>%matplotlib inline
|
||||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="o">%</span><span class="k">matplotlib</span> inline
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from sklearn.model_selection import train_test_split
|
||||
# from sklearn.datasets import fill in the data set
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
|
||||
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||||
<span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <span class="n">train_test_split</span>
|
||||
<span class="c1"># from sklearn.datasets import fill in the data set</span>
|
||||
<span class="kn">from</span> <span class="nn">sklearn.linear_model</span> <span class="kn">import</span> <span class="n">LogisticRegression</span>
|
||||
|
||||
# Load the data, fill inn
|
||||
mydata.data = ?
|
||||
<span class="c1"># Load the data, fill inn</span>
|
||||
<span class="n">mydata</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="o">?</span>
|
||||
|
||||
X_train, X_test, y_train, y_test = train_test_split(mydata.data,cancer.target,random_state=0)
|
||||
print(X_train.shape)
|
||||
print(X_test.shape)
|
||||
# Logistic Regression
|
||||
# define which type of problem, binary or multiclass
|
||||
logreg = LogisticRegression(solver='lbfgs')
|
||||
logreg.fit(X_train, y_train)
|
||||
<span class="n">X_train</span><span class="p">,</span> <span class="n">X_test</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">y_test</span> <span class="o">=</span> <span class="n">train_test_split</span><span class="p">(</span><span class="n">mydata</span><span class="o">.</span><span class="n">data</span><span class="p">,</span><span class="n">cancer</span><span class="o">.</span><span class="n">target</span><span class="p">,</span><span class="n">random_state</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="n">X_train</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="n">X_test</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
|
||||
<span class="c1"># Logistic Regression</span>
|
||||
<span class="c1"># define which type of problem, binary or multiclass</span>
|
||||
<span class="n">logreg</span> <span class="o">=</span> <span class="n">LogisticRegression</span><span class="p">(</span><span class="n">solver</span><span class="o">=</span><span class="s1">'lbfgs'</span><span class="p">)</span>
|
||||
<span class="n">logreg</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>
|
||||
|
||||
from sklearn.preprocessing import LabelEncoder
|
||||
from sklearn.model_selection import cross_validate
|
||||
#Cross validation
|
||||
accuracy = cross_validate(logreg,X_test,y_test,cv=10)['test_score']
|
||||
print(accuracy)
|
||||
print("Test set accuracy with Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test)))
|
||||
<span class="kn">from</span> <span class="nn">sklearn.preprocessing</span> <span class="kn">import</span> <span class="n">LabelEncoder</span>
|
||||
<span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <span class="n">cross_validate</span>
|
||||
<span class="c1">#Cross validation</span>
|
||||
<span class="n">accuracy</span> <span class="o">=</span> <span class="n">cross_validate</span><span class="p">(</span><span class="n">logreg</span><span class="p">,</span><span class="n">X_test</span><span class="p">,</span><span class="n">y_test</span><span class="p">,</span><span class="n">cv</span><span class="o">=</span><span class="mi">10</span><span class="p">)[</span><span class="s1">'test_score'</span><span class="p">]</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="n">accuracy</span><span class="p">)</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Test set accuracy with Logistic Regression: </span><span class="si">{:.2f}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">logreg</span><span class="o">.</span><span class="n">score</span><span class="p">(</span><span class="n">X_test</span><span class="p">,</span><span class="n">y_test</span><span class="p">)))</span>
|
||||
|
||||
import scikitplot as skplt
|
||||
y_pred = logreg.predict(X_test)
|
||||
skplt.metrics.plot_confusion_matrix(y_test, y_pred, normalize=True)
|
||||
plt.show()
|
||||
y_probas = logreg.predict_proba(X_test)
|
||||
skplt.metrics.plot_roc(y_test, y_probas)
|
||||
plt.show()
|
||||
skplt.metrics.plot_cumulative_gain(y_test, y_probas)
|
||||
plt.show()
|
||||
<span class="kn">import</span> <span class="nn">scikitplot</span> <span class="k">as</span> <span class="nn">skplt</span>
|
||||
<span class="n">y_pred</span> <span class="o">=</span> <span class="n">logreg</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_test</span><span class="p">)</span>
|
||||
<span class="n">skplt</span><span class="o">.</span><span class="n">metrics</span><span class="o">.</span><span class="n">plot_confusion_matrix</span><span class="p">(</span><span class="n">y_test</span><span class="p">,</span> <span class="n">y_pred</span><span class="p">,</span> <span class="n">normalize</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||||
<span class="n">y_probas</span> <span class="o">=</span> <span class="n">logreg</span><span class="o">.</span><span class="n">predict_proba</span><span class="p">(</span><span class="n">X_test</span><span class="p">)</span>
|
||||
<span class="n">skplt</span><span class="o">.</span><span class="n">metrics</span><span class="o">.</span><span class="n">plot_roc</span><span class="p">(</span><span class="n">y_test</span><span class="p">,</span> <span class="n">y_probas</span><span class="p">)</span>
|
||||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||||
<span class="n">skplt</span><span class="o">.</span><span class="n">metrics</span><span class="o">.</span><span class="n">plot_cumulative_gain</span><span class="p">(</span><span class="n">y_test</span><span class="p">,</span> <span class="n">y_probas</span><span class="p">)</span>
|
||||
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -664,9 +664,9 @@ plt.show()
|
||||
the MNIST data set and just specialize to two numbers. To do so you can use the following code lines</p>
|
||||
<div class="cell docutils container">
|
||||
<div class="cell_input docutils container">
|
||||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>from sklearn.datasets import load_digits
|
||||
digits = load_digits(n_class=2) # Load only two classes, e.g., 0 and 1
|
||||
X, y = digits.data, digits.target
|
||||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.datasets</span> <span class="kn">import</span> <span class="n">load_digits</span>
|
||||
<span class="n">digits</span> <span class="o">=</span> <span class="n">load_digits</span><span class="p">(</span><span class="n">n_class</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="c1"># Load only two classes, e.g., 0 and 1</span>
|
||||
<span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">digits</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="n">digits</span><span class="o">.</span><span class="n">target</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -678,8 +678,8 @@ n_classes=2. You can also control the number of samples, features,
|
||||
informative features, redundant features, and more.</p>
|
||||
<div class="cell docutils container">
|
||||
<div class="cell_input docutils container">
|
||||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>from sklearn.datasets import make_classification
|
||||
X, y = make_classification(n_samples=1000, n_features=20, n_informative=10, n_redundant=5, n_classes=2, random_state=42)
|
||||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.datasets</span> <span class="kn">import</span> <span class="n">make_classification</span>
|
||||
<span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">make_classification</span><span class="p">(</span><span class="n">n_samples</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="n">n_features</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">n_informative</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">n_redundant</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">n_classes</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">random_state</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -696,10 +696,10 @@ the exercises from weeks 41 and 42. This is a three-class data set and
|
||||
you can set it up using <strong>scikit-learn</strong>,</p>
|
||||
<div class="cell docutils container">
|
||||
<div class="cell_input docutils container">
|
||||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>from sklearn.datasets import load_iris
|
||||
iris = load_iris()
|
||||
X = iris.data # Features
|
||||
y = iris.target # Target labels
|
||||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.datasets</span> <span class="kn">import</span> <span class="n">load_iris</span>
|
||||
<span class="n">iris</span> <span class="o">=</span> <span class="n">load_iris</span><span class="p">()</span>
|
||||
<span class="n">X</span> <span class="o">=</span> <span class="n">iris</span><span class="o">.</span><span class="n">data</span> <span class="c1"># Features</span>
|
||||
<span class="n">y</span> <span class="o">=</span> <span class="n">iris</span><span class="o">.</span><span class="n">target</span> <span class="c1"># Target labels</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -775,7 +775,7 @@ gain curve for this (or other) multiclass data set.</p>
|
||||
<nav class="bd-toc-nav page-toc">
|
||||
<ul class="visible nav section-nav flex-column">
|
||||
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#">Exercises week 43</a></li>
|
||||
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#overarching-aims-of-the-exercises-weeks-43-and-44">Overarching aims of the exercises weeks 43 and 44</a><ul class="visible nav section-nav flex-column">
|
||||
<li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#overarching-aims-of-the-exercises-for-week-43">Overarching aims of the exercises for week 43</a><ul class="visible nav section-nav flex-column">
|
||||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#confusion-matrix">Confusion Matrix</a></li>
|
||||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#roc-curve">ROC Curve</a></li>
|
||||
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#cumulative-gain">Cumulative Gain</a></li>
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -32,15 +32,15 @@
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"# Overarching aims of the exercises weeks 43 and 44\n",
|
||||
"# Overarching aims of the exercises for week 43\n",
|
||||
"\n",
|
||||
"The aim of the exercises this week is to gain some confidence with\n",
|
||||
"ways to visualize the results of a classification problem. We will\n",
|
||||
"target three ways of setting up the analysis. The first and simplest\n",
|
||||
"one is the\n",
|
||||
"1. so-called confusion matrix, and the next is the\n",
|
||||
"1. so-called confusion matrix. The next one is the so-called\n",
|
||||
"\n",
|
||||
"2. ROC curve and finally the\n",
|
||||
"2. ROC curve. Finally we have the\n",
|
||||
"\n",
|
||||
"3. Cumulative gain curve.\n",
|
||||
"\n",
|
||||
@@ -446,7 +446,10 @@
|
||||
"id": "be9ff0b9",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
"editable": true,
|
||||
"jupyter": {
|
||||
"outputs_hidden": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -518,7 +521,10 @@
|
||||
"id": "d20bb8be",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
"editable": true,
|
||||
"jupyter": {
|
||||
"outputs_hidden": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -547,7 +553,10 @@
|
||||
"id": "d271f0ba",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
"editable": true,
|
||||
"jupyter": {
|
||||
"outputs_hidden": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -589,7 +598,10 @@
|
||||
"id": "3b045d56",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"editable": true
|
||||
"editable": true,
|
||||
"jupyter": {
|
||||
"outputs_hidden": false
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -611,7 +623,25 @@
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {},
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.15"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Reference in New Issue
Block a user