.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_performance_metrics/plot_02_roc_curve.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_04_performance_metrics_plot_02_roc_curve.py: ======================================= Receiver Operating Characteristic Curve ======================================= Example of plotting the ROC curve for a classification task. .. GENERATED FROM PYTHON SOURCE LINES 8-23 .. code-block:: Python import matplotlib import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import load_breast_cancer from sklearn.metrics import confusion_matrix, roc_auc_score, roc_curve from sklearn.preprocessing import StandardScaler from sklvq import GMLVQ matplotlib.rc("xtick", labelsize="small") matplotlib.rc("ytick", labelsize="small") data, labels = load_breast_cancer(return_X_y=True) .. GENERATED FROM PYTHON SOURCE LINES 24-26 Create a GMLVQ object and pass it a distance function, activation function and solver. See the API reference under documentation for defaults. .. GENERATED FROM PYTHON SOURCE LINES 26-36 .. code-block:: Python model = GMLVQ( distance_type="adaptive-squared-euclidean", activation_type="swish", activation_params={"beta": 2}, solver_type="waypoint-gradient-descent", solver_params={"max_runs": 10, "k": 3, "step_size": np.array([0.1, 0.05])}, random_state=31415, ) .. GENERATED FROM PYTHON SOURCE LINES 37-38 Fit the GMLVQ object to the data and plot the roc curve. .. GENERATED FROM PYTHON SOURCE LINES 38-76 .. code-block:: Python # Object to perform z-transform scaler = StandardScaler() # Compute (fit) and apply (transform) z-transform data = scaler.fit_transform(data) # Train the model using the scaled X and true labels model.fit(data, labels) # Get the decision values (which are used in predict) instead of the labels. The values are with # respect to the "greater" class, i.e., index 1. label_score = model.decision_function(data) # roc_curve expects the y_score to be with respect to the positive class. fpr, tpr, thresholds = roc_curve(y_true=labels, y_score=label_score, pos_label=1, drop_intermediate=True) roc_auc = roc_auc_score(y_true=labels, y_score=label_score) # Sometimes it is good to know where the Nearest prototype classifier is on this curve. This can # be computed using the confusion matrix function from sklearn. tn, fp, fn, tp = confusion_matrix(y_true=labels, y_pred=model.predict(data)).ravel() # The tpr and fpr of the npc are then given by: npc_tpr = tp / (tp + fn) npc_fpr = fp / (fp + tn) fig, ax = plt.subplots() fig.suptitle("Receiver operating characteristic ") # Plot the ROC curve ax.plot(fpr, tpr, color="darkorange", lw=2, label=f"ROC AUC = {roc_auc:.3f}") # Plot the random line ax.plot([0, 1], [0, 1], color="navy", lw=2, linestyle="--") # Plot the NPC classifier ax.plot(npc_fpr, npc_tpr, color="green", marker="o", markersize="12") ax.set_xlabel("False Positive Rate") ax.set_ylabel("True Positive Rate") ax.legend(loc="lower right") ax.grid(False) .. image-sg:: /auto_examples/04_performance_metrics/images/sphx_glr_plot_02_roc_curve_001.png :alt: Receiver operating characteristic :srcset: /auto_examples/04_performance_metrics/images/sphx_glr_plot_02_roc_curve_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.108 seconds) .. _sphx_glr_download_auto_examples_04_performance_metrics_plot_02_roc_curve.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_roc_curve.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_roc_curve.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_roc_curve.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_