Achintha Ihalage
Achintha Ihalage

Reputation: 2440

sklearn ImportError: cannot import name plot_roc_curve

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?

Upvotes: 12

Views: 42311

Answers (8)

Johan Tchassem
Johan Tchassem

Reputation: 21

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)

Upvotes: 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

Upvotes: 0

KUMAR PREM RANJAN
KUMAR PREM RANJAN

Reputation: 1

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

Upvotes: 0

Pann Vandet
Pann Vandet

Reputation: 11

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.

Upvotes: 1

Uendel Rocha
Uendel Rocha

Reputation: 301

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_)

Upvotes: 20

sangam
sangam

Reputation: 61

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.

Upvotes: 1

Talha Rifaai
Talha Rifaai

Reputation: 99

Install scikit-plot and import the metric from there:

from scikitplot.metrics import plot_roc_curve

Upvotes: 9

Related Questions