INRIA / INRIA/scikit-learn-mooc

Put-back removed code about ipywidgets and confusiong matrix etc ...

Open
#338 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
1.4k
Forks
600
Avg merge
6d 20h
Merged PRs (30d)
2

Description

Commenting inside `.py` files does not quite work as expected so the simplest is to remove this WIP.
![image](https://user-images.githubusercontent.com/1680079/117316078-8ed45f00-ae88-11eb-83a5-563273d8cd83.png)

The code that I removed:
```py
# ## Link between confusion matrix, precision-recall curve and ROC curve
#
# TODO: ipywidgets to play with interactive curve

# %%
import matplotlib.pyplot as plt
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score

def plot_pr_curve(classifier, X_test, y_test, pos_label,
probability_threshold, ax):
y_pred = classifier.predict_proba(X_test)
precision, recall, threshold = precision_recall_curve(
y_test, y_pred[:, 0], pos_label=pos_label,
)
average_precision = average_precision_score(
y_test, y_pred[:, 0], pos_label=pos_label,
)
ax.plot(
recall, precision,
color="tab:orange", linewidth=3,
label=f"Average Precision: {average_precision:.2f}",
)
threshold_idx = np.searchsorted(
threshold, probability_threshold,
)
ax.plot(
recall[threshold_idx], precision[threshold_idx],
color="tab:blue", marker=".", markersize=10,
)
ax.plot(
[recall[threshold_idx], recall[threshold_idx]],
[0, precision[threshold_idx]],
'--', color="tab:blue",
)
ax.plot(
[0, recall[threshold_idx]],
[precision[threshold_idx], precision[threshold_idx]],
'--', color="tab:blue",
)
ax.set_xlabel(f"Recall")
ax.set_ylabel(f"Precision")
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.legend()
return ax

# %%
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve

def plot_roc_curve(classifier, X_test, y_test, pos_label,
probability_threshold, ax):
y_pred = classifier.predict_proba(X_test)
fpr, tpr, threshold = roc_curve(y_test, y_pred[:, 0], pos_label=pos_label)
roc_auc = roc_auc_score(y_test, y_pred[:, 1])
ax.plot(
fpr, tpr,
color="tab:orange", linewidth=3,
label=f"ROC-AUC: {roc_auc:.2f}"
)
ax.plot([0, 1], [0, 1], "--", color="tab:green", label="Chance")
threshold_idx = np.searchsorted(
threshold[::-1], probability_threshold,
)
threshold_idx = len(threshold) - threshold_idx - 1
ax.plot(
fpr[threshold_idx], tpr[threshold_idx],
color="tab:blue", marker=".", markersize=10,
)
ax.plot(
[fpr[threshold_idx], fpr[threshold_idx]],
[0, tpr[threshold_idx]],
'--', color="tab:blue",
)
ax.plot(
[0, fpr[threshold_idx]],
[tpr[threshold_idx], tpr[threshold_idx]],
'--', color="tab:blue",
)
ax.set_xlabel(f"1 - Specificity")
ax.set_ylabel(f"Sensitivity")
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.legend()
return ax

# %%
def plot_confusion_matrix_with_threshold(classifier, X_test, y_test, pos_label,
probability_threshold, ax):
from itertools import product
from sklearn.metrics import confusion_matrix

class_idx = np.where(classifier.classes_ == pos_label)[0][0]
n_classes = len(classifier.classes_)

y_pred = classifier.predict_proba(X_test)
y_pred = (y_pred[:, class_idx] > probability_threshold).astype(int)

cm = confusion_matrix(
(y_test == pos_label).astype(int), y_pred,
)
im_ = ax.imshow(cm, interpolation='nearest')

text_ = None
cmap_min, cmap_max = im_.cmap(0), im_.cmap(256)

text_ = np.empty_like(cm, dtype=object)

# print text with appropriate color depending on background
thresh = (cm.max() + cm.min()) / 2.0

for i, j in product(range(n_classes), range(n_classes)):
color = cmap_max if cm[i, j] < thresh else cmap_min

text_cm = format(cm[i, j], '.2g')
if cm.dtype.kind != 'f':
text_d = format(cm[i, j], 'd')
if len(text_d) < len(text_cm):
text_cm = text_d

text_[i, j] = ax.text(
j, i, text_cm, ha="center", va="center", color=color
)

ax.set(
xticks=np.arange(n_classes),
yticks=np.arange(n_classes),
xticklabels=classifier.classes_[[int(not bool(class_idx)), class_idx]],
yticklabels=classifier.classes_[[int(not bool(class_idx)), class_idx]],
ylabel="True label",
xlabel="Predicted label"
)

# %%
def plot_pr_roc(threshold):
# FIXME: we could optimize the plotting by only updating the
fig, axs = plt.subplots(ncols=3, figsize=(21, 6))
plot_pr_curve(
classifier, data_test, target_test, pos_label="donated",
probability_threshold=threshold, ax=axs[0],
)
plot_roc_curve(
classifier, data_test, target_test, pos_label="donated",
probability_threshold=threshold, ax=axs[1]
)
plot_confusion_matrix_with_threshold(
classifier, data_test, target_test, pos_label="donated",
probability_threshold=threshold, ax=axs[2]
)
fig.suptitle(
"Overall statistical performance with positive class 'donated'")

# %%
def plot_pr_roc_interactive():
from ipywidgets import interactive, FloatSlider
slider = FloatSlider(min=0, max=1, step=0.01, value=0.5)
return interactive(plot_pr_roc, threshold=slider)

# %%
plot_pr_roc_interactive()
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.