sklearn ImportError: cannot import name plot_roc_curve
Asked Answered
S

8

11

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?

Salvo answered 20/2, 2020 at 13:44 Comment(0)
K
18

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_)
Kilogram answered 12/12, 2022 at 5:1 Comment(0)
Z
9

Install scikit-plot and import the metric from there:

from scikitplot.metrics import plot_roc_curve
Zachary answered 3/5, 2020 at 19:4 Comment(0)
T
1

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.

Tilney answered 16/11, 2020 at 15:45 Comment(0)
F
1

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.

Foreclose answered 24/5, 2023 at 7:2 Comment(0)
A
0

for

ImportError: cannot import name 'plot_roc_curve' from 'sklearn.metrics'

use RocCurveDisplay instead of plot_roc_curve as in from sklearn.metrics import RocCurveDisplay

Antimere answered 3/6, 2023 at 8:51 Comment(0)
S
0

plot_roc_curve was deprecated and removed from sklearn in version 1.2. Use RocCurveDisplay: Instead of plot_roc_curve, the current method to plot ROC curves is through the RocCurveDisplay class in sklearn.metrics.

So try:

from sklearn.metrics import RocCurveDisplay
Squamation answered 9/5 at 2:12 Comment(0)
C
0

Use RocCurveDisplay instead of plot_roc_curve:

from sklearn.metrics import RocCurveDisplay

then you can use it as such:

RocCurveDisplay.from_estimator(clf, X_test, y_test)
Coremaker answered 19/6 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.