.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery1/plot_roc.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_gallery1_plot_roc.py: === ROC === An exemple on ROC curve. .. GENERATED FROM PYTHON SOURCE LINES 11-12 Data .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: default from sklearn import datasets iris = datasets.load_iris() X = iris.data[:, :2] y = iris.target .. GENERATED FROM PYTHON SOURCE LINES 18-21 .. code-block:: default from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) .. GENERATED FROM PYTHON SOURCE LINES 22-26 .. code-block:: default from sklearn.linear_model import LogisticRegression clf = LogisticRegression() clf.fit(X_train, y_train) .. raw:: html
LogisticRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 27-32 .. code-block:: default import numpy ypred = clf.predict(X_test) yprob = clf.predict_proba(X_test) score = numpy.array(list(yprob[i, ypred[i]] for i in range(len(ypred)))) .. GENERATED FROM PYTHON SOURCE LINES 33-38 .. code-block:: default data = numpy.zeros((len(ypred), 2)) data[:, 0] = score.ravel() data[ypred == y_test, 1] = 1 data[:5] .. rst-class:: sphx-glr-script-out .. code-block:: none array([[0.53781077, 1. ], [0.71038639, 1. ], [0.958238 , 1. ], [0.72766728, 1. ], [0.6519351 , 1. ]]) .. GENERATED FROM PYTHON SOURCE LINES 39-46 ROC - TPR / FPR * TPR = True Positive Rate * FPR = False Positive Rate You can see as TPR the distribution function of a score for a positive example and the FPR the same for a negative example. .. GENERATED FROM PYTHON SOURCE LINES 46-58 .. code-block:: default from sklearn import metrics import matplotlib.pyplot as plt dec = ypred == y_test ans = numpy.zeros(len(dec)) ans[dec] = 1 fpr, tpr, thresholds = metrics.roc_curve(ans, score) plt.figure() plt.plot(fpr, tpr, lw=2, label='ROC curve', color="red") plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--') plt.show() .. image-sg:: /gallery1/images/sphx_glr_plot_roc_001.png :alt: plot roc :srcset: /gallery1/images/sphx_glr_plot_roc_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 59-62 End ############################################################################## Nothing below. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 3.550 seconds) .. _sphx_glr_download_gallery1_plot_roc.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_roc.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_roc.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_