PythonでROC曲線をプロットする方法 質問する

PythonでROC曲線をプロットする方法 質問する

私は、ロジスティック回帰パッケージを使用して Python で開発した予測モデルの精度を評価するために、ROC 曲線をプロットしようとしています。真陽性率と偽陽性率を計算しましたが、matplotlibAUC 値を使用してこれらを正しくプロットして計算する方法がわかりません。どうすればよいでしょうか?

ベストアンサー1

modelSklearn 予測子であると仮定すると、試すことができる 2 つの方法があります。

import sklearn.metrics as metrics
# calculate the fpr and tpr for all thresholds of the classification
probs = model.predict_proba(X_test)
preds = probs[:,1]
fpr, tpr, threshold = metrics.roc_curve(y_test, preds)
roc_auc = metrics.auc(fpr, tpr)

# method I: plt
import matplotlib.pyplot as plt
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()

# method II: ggplot
from ggplot import *
df = pd.DataFrame(dict(fpr = fpr, tpr = tpr))
ggplot(df, aes(x = 'fpr', y = 'tpr')) + geom_line() + geom_abline(linetype = 'dashed')

または試す

ggplot(df, aes(x = 'fpr', ymin = 0, ymax = 'tpr')) + geom_line(aes(y = 'tpr')) + geom_area(alpha = 0.2) + ggtitle("ROC Curve w/ AUC = %s" % str(roc_auc)) 

おすすめ記事