WIki上面的ROC曲线说明的很详细了。 因为要写连续值的roc曲线。需要确定阈值,wiki上面的说明如下。
当讯号侦测(或变数测量)的结果是一个连续值时,类与类的边界必须用一个阈值(英语:threshold)来界定。
Java代码如下:
public double[][] getROC(double thershold, double[] product, double[] predict) { //thershold是阈值,也就是基准
if(product.length != predict.length)
throw new IllegalStateException("长度必须一致");
else if(product.length == 0
|| predict.length == 0
|| product == null
|| predict == null)
throw new IllegalStateException("请传入有参数的数值");
log.error("origin data: "+ product.toString() + "\t" + "predict data: " + predict);
double sumTp = 0.0;
double sumFn = 0.0;
double sumFp = 0.0;
double sumTn = 0.0;
double tpr = 0.0;
double fpr = 0.0;
int len = product.length;
double[] X = new double[len];
double[] Y = new double[len];
for(int i = 0; i < len; i++) { //后面的product[i], 可以用自己想要的维度来替换
if((Math.abs(product[i]-predict[i]) /product[i] > thershold) & (product[i] > 4) == true)
sumTp += 1.0;
if((Math.abs(product[i]-predict[i]) /product[i] > thershold) & (product[i] > 4) == false)
sumFn += 1.0;
if((Math.abs(product[i]-predict[i]) /product[i] < thershold) & (product[i] > 4) == true)
sumFp += 1.0;
if((Math.abs(product[i]-predict[i]) /product[i] < thershold) & (product[i] > 4) == false)
sumTn += 1.0;
if (sumTp == 0.0)
tpr = 0.0;
else
tpr = sumTp / (sumTp + sumFn);
if (sumFp == 0.0)
fpr = 0.0;
else
fpr = sumFp / (sumFp + sumTn);
X[i] = fpr;
Y[i] = tpr;
} //为了画出一个曲线,所以我选择存入二维数组中
double[][] roc = {X,Y};
return roc;
}