要在Pandas中绘制ROC曲线,通常会使用Python的matplotlib库。以下是一个简单的示例代码,演示如何在Pandas中绘制ROC曲线:
import pandas as pdimport matplotlib.pyplot as pltfrom sklearn.metrics import roc_curve, roc_auc_score# 假设有一个包含真实标签和预测概率的数据框data = { 'true_label': [0, 1, 1, 0, 1, 0], 'prob': [0.1, 0.9, 0.8, 0.3, 0.7, 0.4]}df = pd.DataFrame(data)# 计算ROC曲线的参数fpr, tpr, thresholds = roc_curve(df['true_label'], df['prob'])roc_auc = roc_auc_score(df['true_label'], df['prob'])# 绘制ROC曲线plt.figure()plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')plt.xlim([0.0, 1.0])plt.ylim([0.0, 1.05])plt.xlabel('False Positive Rate')plt.ylabel('True Positive Rate')plt.title('Receiver Operating Characteristic')plt.legend(loc="lower right")plt.show()以上代码示例中,首先创建一个包含真实标签和预测概率的数据框。然后使用roc_curve和roc_auc_score函数计算ROC曲线的参数,最后使用matplotlib库绘制ROC曲线。您可以根据自己的数据和需求进行相应的调整和修改。


