我正在尝试运行以下代码,但最后一行始终出现错误

问题描述 投票:0回答:1

这是我正在运行的代码:

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.model_selection import train_test_split, cross_val_score
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.naive_bayes import GaussianNB, MultinomialNB
    from sklearn.metrics import accuracy_score
    from sklearn.neighbors import KNeighborsClassifier   
    from sklearn.neural_network import MLPClassifier
    from sklearn.metrics import precision_score, recall_score,auc
    from sklearn.metrics import roc_curve,roc_auc_score, plot_roc_curve

这是错误:

ImportError                               Traceback (most recent call      last)
<ipython-input-3-d1b430d75826> in <cell line: 7>()
      5 from sklearn.tree import DecisionTreeClassifier
      6 from sklearn.naive_bayes import GaussianNB, MultinomialNB
----> 7 from sklearn.metrics import accuracy_score, precision_score,  recall_score, roc_curve, roc_auc_score, plot_roc_curve
      8 from sklearn.neighbors import KNeighborsClassifier
      9 from sklearn.neural_network import MLPClassifier

ImportError: cannot import name 'plot_roc_curve' from 'sklearn.metrics' (/usr/local/lib/python3.10/dist-packages/sklearn/metrics/__init__.py)

如果我的代码错误或缺少任何内容,请告诉我?

python google-colaboratory
1个回答
0
投票

发生该错误是因为

plot_roc_curve
已在 scikit-learn 版本 1.0 中弃用并在版本 1.2 中删除。您可以使用
RocCurveDisplay
模块中的
sklearn.metrics
来代替,因为 Google Colab 目前似乎正在使用 1.3.2:

from sklearn.metrics import accuracy_score, precision_score, recall_score, roc_curve, roc_auc_score, RocCurveDisplay

为了绘制 ROC 曲线,您可以像这样使用

RocCurveDisplay

RocCurveDisplay.from_estimator(your_model, X_test, y_test)
plt.show()

your_model
X_test
y_test
替换为您的模型和数据变量。这应该适用于最新版本的 scikit-learn。

© www.soinside.com 2019 - 2024. All rights reserved.