Ilham Kurniawan
Ilham Kurniawan

Reputation: 11

Cannot import name 'plot_precision_recall_curve' from 'sklearn.metrics'

Hello i got trouble to import my package?cannot import name from sklearn

cannot import name 'plot_precision_recall_curve' from 'sklearn.metrics' (\Programs\Python\Python311\Lib\site-packages\sklearn\metrics_init_.py)

i try uninstall and install again pip install scikit-learn still not working at VScode

1

jupyter, python, scikit-learn

Upvotes: 1

Views: 9808

Answers (2)

Sole Galli
Sole Galli

Reputation: 1082

Following rickhg12hs comment, this is the way to plot precision recall curves from sklearn version 1.2.

First you generate some data:

import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.metrics import PrecisionRecallDisplay
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

X, y = make_classification(random_state=0)

X_train, X_test, y_train, y_test = train_test_split(
        X, y, random_state=0)

Then you train a classifier:

clf = LogisticRegression()
clf.fit(X_train, y_train)

You obtain the preditions of the classiffier and with that you can plot the curve with from_predictions:

y_pred = clf.predict_proba(X_test)[:, 1]

PrecisionRecallDisplay.from_predictions(
   y_test, y_pred)

plt.show()

Upvotes: 2

rocha-p
rocha-p

Reputation: 1441

That function is from an old version of scikit-learn.

You can try using scikit-plot

Installation

pip install scikit-plot

Import library

# Import scikit-plot
import scikitplot as skplt
import matplotlib.pyplot as plt

skplt.metrics.plot_precision_recall(y, y_pred)
plt.show()

Documentation: https://scikit-plot.readthedocs.io/en/stable/metrics.html#scikitplot.metrics.plot_precision_recall

Or you can use precision_recall_curve in the current version of sklearn as mentioned by Dr. Snoopy

from sklearn.metrics import precision_recall_curve

Upvotes: 2

Related Questions