sklearn ImportError: cannot import name plot_roc_curve

Question:

I am trying to plot a Receiver Operating Characteristics (ROC) curve with cross validation, following the example provided in sklearn’s documentation. However, the following import gives an ImportError, in both python2 and python3.

from sklearn.metrics import plot_roc_curve

Error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name plot_roc_curve

python-2.7 sklearn version: 0.20.2.

python-3.6 sklearn version: 0.21.3.

I found that the following import works fine, but it’s not quite the same as plot_roc_curve.

from sklearn.metrics import roc_curve

Is plot_roc_curve deprecated? Could somebody try the code and let me know the sklearn version if it works?

Asked By: Achintha Ihalage

||

Answers:

Install scikit-plot and import the metric from there:

from scikitplot.metrics import plot_roc_curve
Answered By: Talha Rifaai

I updated Conda with conda update --all and then updated scikit-learn to the latest version which for me was conda install scikit-learn=0.23.2 and restarted the kernel. After that my errors were gone.

Answered By: sangam

plot_roc_curve has been removed in version 1.2. From 1.2, use RocCurveDisplay instead:

Before sklearn 1.2:

from sklearn.metrics import plot_roc_curve
svc_disp = plot_roc_curve(svc, X_test, y_test)
rfc_disp = plot_roc_curve(rfc, X_test, y_test, ax=svc_disp.ax_)

From sklearn 1.2:

from sklearn.metrics import RocCurveDisplay
svc_disp = RocCurveDisplay.from_estimator(svc, X_test, y_test)
rfc_disp = RocCurveDisplay.from_estimator(rfc, X_test, y_test, ax=svc_disp.ax_)
Answered By: Uendel Rocha

As the official sklearn’s document mentioned: The function plot_roc_curve is deprecated in 1.0 and will be removed in 1.2. If you would like more detail, please refer to here.

Use one of the class methods: sklearn.metric.RocCurveDisplay.from_predictions or sklearn.metric.RocCurveDisplay.from_estimator. f you would like more detail, please refer to here.

Answered By: Pann Vandet

for ImportError: cannot import name ‘plot_roc_curve’ from ‘sklearn.metrics’

use RocCurveDisplay instead of plot_roc_curve
like :- from sklearn.metrics import RocCurveDisplay

Answered By: KUMAR PREM RANJAN