|
116 | 116 | },
|
117 | 117 | "outputs": [],
|
118 | 118 | "source": [
|
119 |
| - "import matplotlib.pyplot as plt\nfrom sklearn.metrics import RocCurveDisplay\n\nRocCurveDisplay.from_predictions(\n y_onehot_test[:, class_id],\n y_score[:, class_id],\n name=f\"{class_of_interest} vs the rest\",\n color=\"darkorange\",\n)\nplt.plot([0, 1], [0, 1], \"k--\", label=\"chance level (AUC = 0.5)\")\nplt.axis(\"square\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"One-vs-Rest ROC curves:\\nVirginica vs (Setosa & Versicolor)\")\nplt.legend()\nplt.show()" |
| 119 | + "import matplotlib.pyplot as plt\nfrom sklearn.metrics import RocCurveDisplay\n\nRocCurveDisplay.from_predictions(\n y_onehot_test[:, class_id],\n y_score[:, class_id],\n name=f\"{class_of_interest} vs the rest\",\n color=\"darkorange\",\n plot_chance_level=True,\n)\nplt.axis(\"square\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"One-vs-Rest ROC curves:\\nVirginica vs (Setosa & Versicolor)\")\nplt.legend()\nplt.show()" |
120 | 120 | ]
|
121 | 121 | },
|
122 | 122 | {
|
|
152 | 152 | },
|
153 | 153 | "outputs": [],
|
154 | 154 | "source": [
|
155 |
| - "RocCurveDisplay.from_predictions(\n y_onehot_test.ravel(),\n y_score.ravel(),\n name=\"micro-average OvR\",\n color=\"darkorange\",\n)\nplt.plot([0, 1], [0, 1], \"k--\", label=\"chance level (AUC = 0.5)\")\nplt.axis(\"square\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"Micro-averaged One-vs-Rest\\nReceiver Operating Characteristic\")\nplt.legend()\nplt.show()" |
| 155 | + "RocCurveDisplay.from_predictions(\n y_onehot_test.ravel(),\n y_score.ravel(),\n name=\"micro-average OvR\",\n color=\"darkorange\",\n plot_chance_level=True,\n)\nplt.axis(\"square\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"Micro-averaged One-vs-Rest\\nReceiver Operating Characteristic\")\nplt.legend()\nplt.show()" |
156 | 156 | ]
|
157 | 157 | },
|
158 | 158 | {
|
|
242 | 242 | },
|
243 | 243 | "outputs": [],
|
244 | 244 | "source": [
|
245 |
| - "from itertools import cycle\n\nfig, ax = plt.subplots(figsize=(6, 6))\n\nplt.plot(\n fpr[\"micro\"],\n tpr[\"micro\"],\n label=f\"micro-average ROC curve (AUC = {roc_auc['micro']:.2f})\",\n color=\"deeppink\",\n linestyle=\":\",\n linewidth=4,\n)\n\nplt.plot(\n fpr[\"macro\"],\n tpr[\"macro\"],\n label=f\"macro-average ROC curve (AUC = {roc_auc['macro']:.2f})\",\n color=\"navy\",\n linestyle=\":\",\n linewidth=4,\n)\n\ncolors = cycle([\"aqua\", \"darkorange\", \"cornflowerblue\"])\nfor class_id, color in zip(range(n_classes), colors):\n RocCurveDisplay.from_predictions(\n y_onehot_test[:, class_id],\n y_score[:, class_id],\n name=f\"ROC curve for {target_names[class_id]}\",\n color=color,\n ax=ax,\n )\n\nplt.plot([0, 1], [0, 1], \"k--\", label=\"ROC curve for chance level (AUC = 0.5)\")\nplt.axis(\"square\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"Extension of Receiver Operating Characteristic\\nto One-vs-Rest multiclass\")\nplt.legend()\nplt.show()" |
| 245 | + "from itertools import cycle\n\nfig, ax = plt.subplots(figsize=(6, 6))\n\nplt.plot(\n fpr[\"micro\"],\n tpr[\"micro\"],\n label=f\"micro-average ROC curve (AUC = {roc_auc['micro']:.2f})\",\n color=\"deeppink\",\n linestyle=\":\",\n linewidth=4,\n)\n\nplt.plot(\n fpr[\"macro\"],\n tpr[\"macro\"],\n label=f\"macro-average ROC curve (AUC = {roc_auc['macro']:.2f})\",\n color=\"navy\",\n linestyle=\":\",\n linewidth=4,\n)\n\ncolors = cycle([\"aqua\", \"darkorange\", \"cornflowerblue\"])\nfor class_id, color in zip(range(n_classes), colors):\n RocCurveDisplay.from_predictions(\n y_onehot_test[:, class_id],\n y_score[:, class_id],\n name=f\"ROC curve for {target_names[class_id]}\",\n color=color,\n ax=ax,\n plot_chance_level=(class_id == 2),\n )\n\nplt.axis(\"square\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"Extension of Receiver Operating Characteristic\\nto One-vs-Rest multiclass\")\nplt.legend()\nplt.show()" |
246 | 246 | ]
|
247 | 247 | },
|
248 | 248 | {
|
|
271 | 271 | },
|
272 | 272 | "outputs": [],
|
273 | 273 | "source": [
|
274 |
| - "pair_scores = []\nmean_tpr = dict()\n\nfor ix, (label_a, label_b) in enumerate(pair_list):\n\n a_mask = y_test == label_a\n b_mask = y_test == label_b\n ab_mask = np.logical_or(a_mask, b_mask)\n\n a_true = a_mask[ab_mask]\n b_true = b_mask[ab_mask]\n\n idx_a = np.flatnonzero(label_binarizer.classes_ == label_a)[0]\n idx_b = np.flatnonzero(label_binarizer.classes_ == label_b)[0]\n\n fpr_a, tpr_a, _ = roc_curve(a_true, y_score[ab_mask, idx_a])\n fpr_b, tpr_b, _ = roc_curve(b_true, y_score[ab_mask, idx_b])\n\n mean_tpr[ix] = np.zeros_like(fpr_grid)\n mean_tpr[ix] += np.interp(fpr_grid, fpr_a, tpr_a)\n mean_tpr[ix] += np.interp(fpr_grid, fpr_b, tpr_b)\n mean_tpr[ix] /= 2\n mean_score = auc(fpr_grid, mean_tpr[ix])\n pair_scores.append(mean_score)\n\n fig, ax = plt.subplots(figsize=(6, 6))\n plt.plot(\n fpr_grid,\n mean_tpr[ix],\n label=f\"Mean {label_a} vs {label_b} (AUC = {mean_score :.2f})\",\n linestyle=\":\",\n linewidth=4,\n )\n RocCurveDisplay.from_predictions(\n a_true,\n y_score[ab_mask, idx_a],\n ax=ax,\n name=f\"{label_a} as positive class\",\n )\n RocCurveDisplay.from_predictions(\n b_true,\n y_score[ab_mask, idx_b],\n ax=ax,\n name=f\"{label_b} as positive class\",\n )\n plt.plot([0, 1], [0, 1], \"k--\", label=\"chance level (AUC = 0.5)\")\n plt.axis(\"square\")\n plt.xlabel(\"False Positive Rate\")\n plt.ylabel(\"True Positive Rate\")\n plt.title(f\"{target_names[idx_a]} vs {label_b} ROC curves\")\n plt.legend()\n plt.show()\n\nprint(f\"Macro-averaged One-vs-One ROC AUC score:\\n{np.average(pair_scores):.2f}\")" |
| 274 | + "pair_scores = []\nmean_tpr = dict()\n\nfor ix, (label_a, label_b) in enumerate(pair_list):\n\n a_mask = y_test == label_a\n b_mask = y_test == label_b\n ab_mask = np.logical_or(a_mask, b_mask)\n\n a_true = a_mask[ab_mask]\n b_true = b_mask[ab_mask]\n\n idx_a = np.flatnonzero(label_binarizer.classes_ == label_a)[0]\n idx_b = np.flatnonzero(label_binarizer.classes_ == label_b)[0]\n\n fpr_a, tpr_a, _ = roc_curve(a_true, y_score[ab_mask, idx_a])\n fpr_b, tpr_b, _ = roc_curve(b_true, y_score[ab_mask, idx_b])\n\n mean_tpr[ix] = np.zeros_like(fpr_grid)\n mean_tpr[ix] += np.interp(fpr_grid, fpr_a, tpr_a)\n mean_tpr[ix] += np.interp(fpr_grid, fpr_b, tpr_b)\n mean_tpr[ix] /= 2\n mean_score = auc(fpr_grid, mean_tpr[ix])\n pair_scores.append(mean_score)\n\n fig, ax = plt.subplots(figsize=(6, 6))\n plt.plot(\n fpr_grid,\n mean_tpr[ix],\n label=f\"Mean {label_a} vs {label_b} (AUC = {mean_score :.2f})\",\n linestyle=\":\",\n linewidth=4,\n )\n RocCurveDisplay.from_predictions(\n a_true,\n y_score[ab_mask, idx_a],\n ax=ax,\n name=f\"{label_a} as positive class\",\n )\n RocCurveDisplay.from_predictions(\n b_true,\n y_score[ab_mask, idx_b],\n ax=ax,\n name=f\"{label_b} as positive class\",\n plot_chance_level=True,\n )\n plt.axis(\"square\")\n plt.xlabel(\"False Positive Rate\")\n plt.ylabel(\"True Positive Rate\")\n plt.title(f\"{target_names[idx_a]} vs {label_b} ROC curves\")\n plt.legend()\n plt.show()\n\nprint(f\"Macro-averaged One-vs-One ROC AUC score:\\n{np.average(pair_scores):.2f}\")" |
275 | 275 | ]
|
276 | 276 | },
|
277 | 277 | {
|
|
307 | 307 | },
|
308 | 308 | "outputs": [],
|
309 | 309 | "source": [
|
310 |
| - "ovo_tpr = np.zeros_like(fpr_grid)\n\nfig, ax = plt.subplots(figsize=(6, 6))\nfor ix, (label_a, label_b) in enumerate(pair_list):\n ovo_tpr += mean_tpr[ix]\n plt.plot(\n fpr_grid,\n mean_tpr[ix],\n label=f\"Mean {label_a} vs {label_b} (AUC = {pair_scores[ix]:.2f})\",\n )\n\novo_tpr /= sum(1 for pair in enumerate(pair_list))\n\nplt.plot(\n fpr_grid,\n ovo_tpr,\n label=f\"One-vs-One macro-average (AUC = {macro_roc_auc_ovo:.2f})\",\n linestyle=\":\",\n linewidth=4,\n)\nplt.plot([0, 1], [0, 1], \"k--\", label=\"chance level (AUC = 0.5)\")\nplt.axis(\"square\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"Extension of Receiver Operating Characteristic\\nto One-vs-One multiclass\")\nplt.legend()\nplt.show()" |
| 310 | + "ovo_tpr = np.zeros_like(fpr_grid)\n\nfig, ax = plt.subplots(figsize=(6, 6))\nfor ix, (label_a, label_b) in enumerate(pair_list):\n ovo_tpr += mean_tpr[ix]\n plt.plot(\n fpr_grid,\n mean_tpr[ix],\n label=f\"Mean {label_a} vs {label_b} (AUC = {pair_scores[ix]:.2f})\",\n )\n\novo_tpr /= sum(1 for pair in enumerate(pair_list))\n\nplt.plot(\n fpr_grid,\n ovo_tpr,\n label=f\"One-vs-One macro-average (AUC = {macro_roc_auc_ovo:.2f})\",\n linestyle=\":\",\n linewidth=4,\n)\nplt.plot([0, 1], [0, 1], \"k--\", label=\"Chance level (AUC = 0.5)\")\nplt.axis(\"square\")\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"Extension of Receiver Operating Characteristic\\nto One-vs-One multiclass\")\nplt.legend()\nplt.show()" |
311 | 311 | ]
|
312 | 312 | },
|
313 | 313 | {
|
|
0 commit comments