【发布时间】:2014-06-01 13:18:49
【问题描述】:
如何使用 scikit learn 或任何其他 python 库为 csv 文件绘制 roc 曲线,例如:
1, 0.202
0, 0.203
0, 0.266
1, 0.264
0, 0.261
0, 0.291
.......
【问题讨论】:
标签: python machine-learning scikit-learn roc
如何使用 scikit learn 或任何其他 python 库为 csv 文件绘制 roc 曲线,例如:
1, 0.202
0, 0.203
0, 0.266
1, 0.264
0, 0.261
0, 0.291
.......
【问题讨论】:
标签: python machine-learning scikit-learn roc
import pandas as pd
import numpy as np
import pylab as pl
from sklearn.metrics import roc_curve, auc
df = pd.read_csv('filename.csv')
y_test = np.array(df)[:,0]
probas = np.array(df)[:,1]
# Compute ROC curve and area the curve
fpr, tpr, thresholds = roc_curve(y_test, probas)
roc_auc = auc(fpr, tpr)
print("Area under the ROC curve : %f" % roc_auc)
# Plot ROC curve
pl.clf()
pl.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)
pl.plot([0, 1], [0, 1], 'k--')
pl.xlim([0.0, 1.0])
pl.ylim([0.0, 1.0])
pl.xlabel('False Positive Rate')
pl.ylabel('True Positive Rate')
pl.title('Receiver operating characteristic')
pl.legend(loc="lower right")
pl.show()
【讨论】:
不是python 的答案,但如果您使用R (http://www.r-project.org/),它就像
# load data
X <- read.table("mydata.csv", sep = ",")
# create and plot RoC curve
library(ROCR)
roc <- ROCR::performance(ROCR::prediction(X[,2], X[,1]), "tpr", "fpr")
plot(roc)
(需要通过install.package("ROCR")预先安装R包ROCR)
【讨论】:
predict is not an exported object from namespace:ROCR
RORC::prediction 而不是 ROCR::predict