import ipywidgets as widgets
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interact
%matplotlib widget
Medical diagnosis: From patient’s perspective¶
- A test for COVID-19 has an accuracy of , i.e.,
- : Indicator of infection.
- : Diagnosis of infection.
- Suppose a person is diagnosed to have the virus, i.e., .
- Is it likely ( chance) that the person has the virus? Y/N
- Is the likelihood ? Y/N
Confusion matrix for binary classification¶
- TP (True +ve): number of +ve tuples classified as +ve.
- TN (True -ve): number of -ve tuples classified as -ve.
- FP (False +ve): number of -ve tuples classified as +ve.
(F_______ a________ / Type I error) - FN (False -ve): number of +ve tuples classified as -ve.
(M______ d________ / Type II error)
Accuracy vs Precision¶
- Accuracy is where .
- Precision is where .
- P_______________ p _______________ v _______________ (PPV)
- Is it possible that accuracy is high but precision is low?
Example¶
- Accuracy is ____________%.
- Precision is ____________%.
- When is accuracy > precision in general?
Negative predictive value (NPV)¶
- NPV is where .
- Accuracy is .
- Accuracy > precision iff NPV PPV.
- Accuracy = precision iff _________________________________________________________
Example¶
- Accuracy is _______________%.
- Precision is _______________%.
- NPV is ________________%.
Medical diagnosis: From Government’s perspective¶
- Suppose the government wants to eradicate COVID-19 as it is highly contagious.
- If a test is accurate, can the government identify of infected people? Y/N
Recall¶
- Recall is where .
- S__________ or True positive rate (TPR)
Example¶
- Accuracy is ____________%.
- Precision is ____________%.
- NPV is __________________%.
- Recall is ___________________________%.
- When is accuracy recall?
Specificity¶
- Specificity is where .
True negative rate (TNR) - Accuracy is
- Accuracy > recall iff TNR ≥ TPR.
- Accuracy = recall iff ______________________________________________________
Example¶
- Accuracy is ____________%.
- Precision is ____________%.
- NPV is __________________%.
- Recall is ___________________________%.
- Specificity is _________________________%.
Class imbalance problem¶
Happens when (or ).
If , accuracy can be dominated by ___________ over __________________.
How to evaluate the prediction of positive class?
- Cost/benefit analysis
- Different per unit cost/benefit assigned to FP, FN, TP, and TN.
- Minimize total cost or maximize total benefit.
F score¶
- Why Harmonic means instead of arithmetic mean?
- Arithmetic mean implies _____
# Code for the above plot
# Create a meshgrid for x and y in the range [0, 1]
x = np.linspace(0, 1, 100)
y = np.linspace(0, 1, 100)
X, Y = np.meshgrid(x, y)
Z = (X + Y) / 2 # Arithmetic mean
# Set up the figure with two subplots: one for 3D and one for contour
fig = plt.figure(figsize=(12, 6), num=1, clear=True)
# 3D subplot
ax1 = fig.add_subplot(121, projection='3d')
ax1.plot_surface(X, Y, Z, cmap='viridis')
ax1.set_title(r'3D Plot of Arithmetic Mean $z=\frac{x+y}{2}$')
ax1.set_xlabel(r'$x$')
ax1.set_ylabel(r'$y$')
ax1.set_zlabel(r'$z$')
ax1.zaxis.set_label_position('lower')
# Contour subplot
ax2 = fig.add_subplot(122)
contour_levels = np.linspace(0, 1, 11) # Levels: 0, 0.1, ..., 1.0
contour = ax2.contour(X, Y, Z, levels=contour_levels, cmap='viridis')
ax2.set_title('Contour Plot')
ax2.set_xlabel(r'$x$')
ax2.set_ylabel(r'$y$')
fig.colorbar(contour, ax=ax2, shrink=0.5, aspect=5)
# To write to file
fig.savefig('images/arithmetic_mean.svg')
plt.show()
- Harmonic mean implies _____
# Create a meshgrid for x and y in the range [0, 1]
x = np.linspace(0.01, 1, 100)
y = np.linspace(0.01, 1, 100)
X, Y = np.meshgrid(x, y)
Z = ((X**(-1) + Y**(-1)) / 2)**(-1) # Harmonic mean
# Set up the figure with two subplots: one for 3D and one for contour
fig = plt.figure(figsize=(12, 6), num=2, clear=True)
# 3D subplot
ax1 = fig.add_subplot(121, projection='3d')
ax1.plot_surface(X, Y, Z, cmap='viridis')
ax1.set_title(r'3D Plot of Harmonic Mean $z=\left(\frac{x^{-1}+y^{-1}}{2}\right)^{-1}$')
ax1.set_xlabel(r'$x$')
ax1.set_ylabel(r'$y$')
ax1.set_zlabel(r'$z$')
ax1.zaxis.set_label_position('lower')
# Contour subplot
ax2 = fig.add_subplot(122)
contour_levels = np.linspace(0, 1, 11) # Levels: 0, 0.1, ..., 1.0
contour = ax2.contour(X, Y, Z, levels=contour_levels, cmap='viridis')
ax2.set_title('Contour Plot')
ax2.set_xlabel(r'$x$')
ax2.set_ylabel(r'$y$')
fig.colorbar(contour, ax=ax2, shrink=0.5, aspect=5)
# To write to file
fig.savefig('images/harmonic_mean.svg')
plt.show()
F-beta score¶
- As , ____
- As , ____
def f_beta_score(precision, recall, beta):
return (1 + beta**2) * (precision * recall) / (beta**2 * precision + recall)
# Create an interactive widget to change beta on a logarithmic scale
beta_slider = widgets.FloatLogSlider(value=2, base=10, min=-2, max=2, step=0.1,
description=r'$\beta$:', continuous_update=False)
@interact
def plot_f_beta(beta=beta_slider):
x = np.linspace(0.01, 1, 100)
y = np.linspace(0.01, 1, 100)
X, Y = np.meshgrid(x, y)
Z = f_beta_score(X, Y, beta)
fig = plt.figure(figsize=(12, 6), num=3, clear=True)
ax1 = fig.add_subplot(121, projection='3d')
surf = ax1.plot_surface(X, Y, Z, cmap='viridis')
ax1.set_title(r'3D Plot of $F_{\beta}:=\left( \frac{\text{PPV}^{-1} + \beta^2 \text{TPR}^{-1}}{\beta^2 + 1} \right)^{-1}$')
ax1.set_xlabel('PPV')
ax1.set_ylabel('TPR')
ax1.set_zlabel(r'$F_{\beta}$ Score')
ax1.zaxis.set_label_position('lower')
ax2 = fig.add_subplot(122)
contour_levels = np.linspace(0, 1, 11)
contour = ax2.contour(X, Y, Z, levels=contour_levels, cmap='viridis')
ax2.set_title('Contour Plot')
ax2.set_xlabel('PPV')
ax2.set_ylabel('TPR')
fig.colorbar(contour, ax=ax2, shrink=0.5, aspect=5)
plt.show()
Threshold-moving¶
- Apply a threshold γ to the output of a probabilistic classifier.
Area under curve (AUC)¶
- Obtain the trade-offs of different performance metrics by varying the threshold.
- Receiver operation characteristics curve (ROC):
- Plot of TPR against FPR (False positive rate=1-TNR)
- AUC: ROC area
- Precision recall curve (PRC):
- Plot of precision against recall
- AUC: PRC area
- Which is better, ROC or PRC?
References¶
- 8.5.1 Metrics for Evaluating Classifier Performance
- 8.5.6 Comparing Classifiers based on Cost-Benefits and ROC Curves